IOS - Generating PDF from UIView by rendering loses quality

I used the following methods to create a PDF file from a UIView. All of them create PDF, but lose quality:

Method 1:

@implementation UIView(PDFWritingAdditions) - (void)renderInPDFFile:(NSString*)path { CGRect mediaBox = self.bounds; CGContextRef ctx = CGPDFContextCreateWithURL((__bridge_retained CFURLRef)[NSURL fileURLWithPath:path], &mediaBox, NULL); CGPDFPageRef page; CGContextDrawPDFPage(ctx, page); CGPDFContextBeginPage(ctx, NULL); // Also tried following commented lines but no luck // CGContextSetFlatness(ctx, 0.1); // CGContextSetAllowsAntialiasing(ctx, YES); // CGContextSetAllowsFontSmoothing(ctx, YES); // CGContextSetInterpolationQuality(ctx, kCGInterpolationHigh); CGContextScaleCTM(ctx, 1, -1); CGContextTranslateCTM(ctx, 0, -mediaBox.size.height); [self.layer renderInContext:ctx]; CGPDFContextEndPage(ctx); CFRelease(ctx); } @end 

Method 2:

 - (void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename { // Creates a mutable data object for updating with binary data, like a byte array NSMutableData *pdfData = [NSMutableData data]; // Points the pdf converter to the mutable data object and to the UIView to be converted UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil); UIGraphicsBeginPDFPage(); CGContextRef pdfContext = UIGraphicsGetCurrentContext(); // draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData [aView.layer renderInContext:pdfContext]; // remove PDF rendering context UIGraphicsEndPDFContext(); // Retrieves the document directories from the iOS device NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES); NSString* documentDirectory = [documentDirectories objectAtIndex:0]; NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename]; // instructs the mutable data object to write its context to a file on disk [pdfData writeToFile:documentDirectoryFilename atomically:YES]; NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename); } 

I think that maybe I am missing some settings for the pdf context, not sure. I also create a png image from UIView with the following method, which is exactly the same quality:

 - (UIImage *)imageFromView:(UIView *)view { UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0); [view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage * img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); // Retrieves the document directories from the iOS device NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES); NSString* documentDirectory = [documentDirectories objectAtIndex:0]; NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:@"pdf.png"]; // instructs the mutable data object to write its context to a file on disk [UIImagePNGRepresentation(img) writeToFile:documentDirectoryFilename atomically:YES]; NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename); return img; } 

Can anyone see what I'm doing wrong? Thanks.

+8
ios iphone pdf ipad render
source share
3 answers

Is this a retina screen used? It could be a contentsScale thing. Check out this SO post

EDIT So now that you have placed the photos before and after, I see that you want the UIView texture to be displayed as good, high-quality, printable vector information in your PDF file.

I am sure that [CALayer renderInContext] always creates raster data, so you are probably better off creating PDFs manually. And if you're talking about tax bills, do you really want to change the layout with the next iOS update?

However, you can look at this answer , which reluctantly suggests using UIView as a templating system for your PDF layout.

+2
source share

Looking at your images, Iโ€™m not sure that your expectations are true ... The preview window is partially enlarged, while you draw a bitmap image into your PDF file, so you will see โ€œjagged edgesโ€ when you zoom in. If you are viewing the actual size (from the preview menu in Preview or Cmd-0), you should see a more or less identical image in the screenshot.

However, what you really want to do is not just convert your raster UIView into a PDF context, but rather use CoreGraphics' drawing and layout directly into a PDF context. Check out this tutorial http://www.raywenderlich.com/6818/how-to-create-a-pdf-with-quartz-2d-in-ios-5-tutorial-part-2 for an example. This ensures that your text and lines remain clear when you enlarge the image and guarantee the quality of the product if it is printed on paper. Quartz2D vector graphics are your friend here, as you will never get high quality with a bitmap, at least without creating a huge PDF file ...

0
source share

edit ok, this is not exactly the same problem, but I think the solution will be similar

I think this is a hoax High quality UIImage from PDF

See my answer there:

stack overflow

When you request a PDF file for this page size, you get the width / height for 72 PPI. You can try to create a context that has been expanded using scale transformation. For example, if you want to render at 300 dpi, add a scale that scales to 300.0/72.0 .

If you export as TIFF, you can encapsulate the final PPI (300) of the generated image.

0
source share

All Articles