Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I generate a PDF with "real" text content on iOS?
    primarykey
    data
    text
    <p>I want to generate a good-looking PDF in my iOS 6 app.</p> <p>I've tried:</p> <ul> <li>UIView render in context </li> <li>Using CoreText </li> <li>Using NSString drawInRect</li> <li>Using UILabel drawRect</li> </ul> <p>Here is a code example:</p> <pre><code>-(CGContextRef) createPDFContext:(CGRect)inMediaBox path:(NSString *) path { CGContextRef myOutContext = NULL; NSURL * url; url = [NSURL fileURLWithPath:path]; if (url != NULL) { myOutContext = CGPDFContextCreateWithURL ((__bridge CFURLRef) url, &amp;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); } </code></pre> <p>The UIViews that are rendered only contain a UIImageView + a bunch of UILabels (some with and some without borders).</p> <p>I also tried a suggestion found on stackoverflow: subclassing UILabel and doing this:</p> <pre><code>- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx { BOOL isPDF = !CGRectIsEmpty(UIGraphicsGetPDFContextBounds()); if (!layer.shouldRasterize &amp;&amp; isPDF) [self drawRect:self.bounds]; // draw unrasterized else [super drawLayer:layer inContext:ctx]; } </code></pre> <p>But that didn't change anything either.</p> <p>No matter what I do, when opening the PDF in Preview the text parts are selectable as a block, but not character per character, and zooming the pdf shows it is actually a bitmap image.</p> <p>Any suggestions?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload