2012-09-23 10 views
7

Quiero generar un PDF atractivo en mi aplicación iOS 6.¿Cómo puedo generar un PDF con contenido de texto "real" en iOS?

He intentado:

  • UIView render en su contexto
  • Usando CoreText
  • Usando NSString drawInRect
  • Usando UILabel drawRect

Aquí está un ejemplo de código:

-(CGContextRef) createPDFContext:(CGRect)inMediaBox path:(NSString *) path 
{ 
    CGContextRef myOutContext = NULL; 
    NSURL * url; 

    url = [NSURL fileURLWithPath:path]; 
    if (url != NULL) { 
     myOutContext = CGPDFContextCreateWithURL ((__bridge CFURLRef) url, 
                &inMediaBox, 
                NULL); 
    } 


    return myOutContext; 
} 

-(void)savePdf:(NSString *)outputPath 
{ 
    if (!pageViews.count) 
     return; 

    UIView * first = [pageViews objectAtIndex:0]; 

    CGContextRef pdfContext = [self createPDFContext:CGRectMake(0, 0, first.frame.size.width, first.frame.size.height) path:outputPath]; 

    for(UIView * v in pageViews) 
    { 
     CGContextBeginPage (pdfContext,nil); 
     CGAffineTransform transform = CGAffineTransformIdentity; 
     transform = CGAffineTransformMakeTranslation(0, (int)(v.frame.size.height)); 
     transform = CGAffineTransformScale(transform, 1, -1); 
     CGContextConcatCTM(pdfContext, transform); 

     CGContextSetFillColorWithColor(pdfContext, [UIColor whiteColor].CGColor); 
     CGContextFillRect(pdfContext, v.frame); 

     [v.layer renderInContext:pdfContext]; 

     CGContextEndPage (pdfContext); 
    } 

    CGContextRelease (pdfContext); 
} 

Los UIViews que se representan sólo contienen un UIImageView + un montón de UILabels (algunos con y algunos sin fronteras).

También probé una sugerencia que se encuentra en stackoverflow: la subclasificación UILabel y hacer esto:

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx { 
    BOOL isPDF = !CGRectIsEmpty(UIGraphicsGetPDFContextBounds()); 
    if (!layer.shouldRasterize && isPDF) 
     [self drawRect:self.bounds]; // draw unrasterized 
    else 
     [super drawLayer:layer inContext:ctx]; 
} 

Pero eso no cambia nada.

No importa lo que haga, al abrir el PDF en Vista previa las partes de texto se pueden seleccionar como un bloque, pero no el carácter por carácter, y al hacer zoom, el pdf muestra que en realidad es una imagen de mapa de bits.

¿Alguna sugerencia?

+0

Se puede publicar un fragmento de código de muestra y el archivo PDF de salida para que pueda echar un vistazo a ellos? – iPDFdev

+0

¿Alguna vez resolvió esto? Experimentando el problema de "selección de bloque". – JeremyDay

+0

no parece suceder ahora con: 'UIGraphicsBeginPDFContextToFile (OutputPath, view.bounds, nil); UIGraphicsBeginPDFPage(); CGContextRef pdfContext = UIGraphicsGetCurrentContext(); [view.layer renderInContext: pdfContext]; UIGraphicsEndPDFContext(); ' – JeremyDay

Respuesta

2
+1

que hacer algo similar, y el PDF se ve bien, pero el texto no es el texto real, sino una imagen renderizada. Me gustaría generar un pdf con texto seleccionable –

+0

Pero utilicé el mismo tutorial y es perfecto para mí en Zooming también. –

+0

Quiere decir seleccionable, como con el cursor en una Mac (copiar, pegar, etc.) –

1

Mi experiencia cuando hice esto el año pasado fue que Apple no proporcionó ninguna biblioteca para hacerlo. Terminé importando una biblioteca C de código abierto (libHaru). Luego agregué una función para mostrarlo en cada clase en mi jerarquía de vistas. Cualquier vista con subvistas llamaría a render en sus subvistas. Mis UILabels, UITextFields, UIImageViews, UISwitches, etc. mostrarían su contenido como texto o gráficos, así como también colores de fondo para algunas vistas.

No era muy desalentador, pero libHaru me dio algunos problemas con las fuentes de modo IIRC Acabé usando la fuente y tamaño de fuente por defecto.

+0

Echaré un vistazo a eso. Gracias –

Cuestiones relacionadas