Je avons obtenu la création multipage PDF travail en modifiant le code ci-dessus, comme:
- (void) createPDF:(NSString *)fileName withContent:(NSString *)content forSize:(int)fontSize andFont:(NSString *)font andColor:(UIColor *)color {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *newFilePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:fileName];
CGRect a4Page = CGRectMake(0, 0, DOC_WIDTH, DOC_HEIGHT);
NSDictionary *fileMetaData = [[NSDictionary alloc] init];
if (!UIGraphicsBeginPDFContextToFile(newFilePath, a4Page, fileMetaData)) {
NSLog(@"error creating PDF context");
return;
}
BOOL done = NO;
CGContextRef context = UIGraphicsGetCurrentContext();
CFRange currentRange = CFRangeMake(0, 0);
CGContextSetTextDrawingMode (context, kCGTextFill);
CGContextSelectFont (context, [font cStringUsingEncoding:NSUTF8StringEncoding], fontSize, kCGEncodingMacRoman);
CGContextSetFillColorWithColor(context, [color CGColor]);
// Initialize an attributed string.
CFMutableAttributedStringRef attrString = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
CFAttributedStringReplaceString (attrString, currentRange, (CFStringRef)content);
// Create the framesetter with the attributed string.
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString);
do {
UIGraphicsBeginPDFPage();
CGMutablePathRef path = CGPathCreateMutable();
CGRect bounds = CGRectMake(LEFT_MARGIN,
TOP_MARGIN,
DOC_WIDTH - RIGHT_MARGIN - LEFT_MARGIN,
DOC_HEIGHT - TOP_MARGIN - BOTTOM_MARGIN);
CGPathAddRect(path, NULL, bounds);
// Create the frame and draw it into the graphics context
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, currentRange, path, NULL);
if(frame) {
CGContextSaveGState(context);
CGContextTranslateCTM(context, 0, bounds.origin.y);
CGContextScaleCTM(context, 1, -1);
CGContextTranslateCTM(context, 0, -(bounds.origin.y + bounds.size.height));
CTFrameDraw(frame, context);
CGContextRestoreGState(context);
// Update the current range based on what was drawn.
currentRange = CTFrameGetVisibleStringRange(frame);
currentRange.location += currentRange.length;
currentRange.length = 0;
CFRelease(frame);
}
// If we're at the end of the text, exit the loop.
if (currentRange.location == CFAttributedStringGetLength((CFAttributedStringRef)attrString))
done = YES;
}
while(!done);
UIGraphicsEndPDFContext();
[fileMetaData release];
CFRelease(attrString);
CFRelease(framesetter);
}
Cependant, comme mentionné ci-dessus, il ne tient pas compte de la police que j'essaie de régler. J'ai toujours quelque chose qui ressemble à "Helvetica". Donc, il n'y a actuellement aucun moyen de créer un fichier PDF sur un iPad/iPhone, qui intègre la police utilisée sur l'appareil? Je peux à peine croire cela. J'aurais au moins espéré que les polices "suspects habituels", comme: Courier, Times New Roman, etc ... soient supportées.
Si quelqu'un a plus d'informations ou de conseils utiles sur les solutions de contournement, n'hésitez pas à les partager.
Merci d'avance.
UIKit a été étendu dans OS 3.2 pour prendre en charge les fonctions PDF. Donc, au lieu de pdfContext = CGPDFContextCreateWithURL (url, & pageRect, docInfo); il utilisera pdfContext = UIGraphicsBeginPDFContextToFile (chemin, & pageRect, docInfo); L'exemple que je regardais est ici: http://www.iphonedevsdk.com/forum/iphone-sdk-tutorials/10989-pdf-creation-tutorial.html – user279778