Generate a PDF (with text & images) from a plist file, can it be done?
Background:
My app saves user-inputted information into plist files. When a photo is taken, or selected from the library, the photo is saved as NSData to the plist as well.
I want to take the information contained in the plist files and generate a PDF document, including the image from NSData.
Question:
I've heard there is a way to generate a PDF from a UIView, is there a nice example to this, or an easy way without writing 1000 lines of C-based code?
Is it possible to design a view in Interface Builder, then use some magic code 开发者_Python百科to generate a PDF from that view, using the data from a plist file?
To generate a PDF you have to actually draw out the whole PDF, putting the graphics and text in exact places on each page. Apple's Drawing and Printing Guide explains how to go about it.
It's not easy, unfortunately, and I'm not aware of any magic code that will generate a PDF from a view.
here's some "magic code" (to quote @Matt ;-) that will write a view hierarchy into a PDF:
#import <QuartzCore/CALayer.h>
UIView *displayView = self.view;
CALayer *layer = displayView.layer;
CGRect pageRect = displayView.bounds;
if (UIGraphicsBeginPDFContextToFile(path, pageRect, [self pdfContextDictionary]) == NO) {
return nil; // error
}
UIGraphicsBeginPDFPage();
CGContextRef context = UIGraphicsGetCurrentContext();
[layer renderInContext:context];
UIGraphicsEndPDFContext();
unfortunately, it does so at screen resolution, you can create a larger-than-screen version, then it will be a better resolution,
for nice, pretty, (and painful) PDF, see the above referenced guide
精彩评论