2010-04-30 5 views
0

Est-ce que quelqu'un a déjà créé un document PDF dans une application iPad? Je vois qu'il y a de nouvelles fonctions dans l'UIKit pour le faire, mais je ne trouve aucun exemple de code pour cela.Erreurs lors de la création d'un fichier PDF de plusieurs pages

BOOL UIGraphicsBeginPDFContextToFile (
    NSString *path, 
    CGRect bounds, 
    NSDictionary *documentInfo 
); 

void UIGraphicsBeginPDFPage (
    void 
); 

Je trouve un exemple qui est censé fonctionner sur l'iPhone, mais cela me donne des erreurs:

Fri Apr 30 11:55:32 wks104.hs.local PDF[1963] <Error>: CGFont/Freetype: The function `create_subset' is currently unimplemented. 
Fri Apr 30 11:55:32 wks104.hs.local PDF[1963] <Error>: invalid Type1 font: unable to stream font. 
Fri Apr 30 11:55:32 wks104.hs.local PDF[1963] <Error>: FT_Load_Glyph failed: error 6. 
Fri Apr 30 11:55:32 wks104.hs.local PDF[1963] <Error>: FT_Load_Glyph failed: error 6. 
Fri Apr 30 11:55:32 wks104.hs.local PDF[1963] <Error>: FT_Load_Glyph failed: error 6. 
Fri Apr 30 11:55:32 wks104.hs.local PDF[1963] <Error>: FT_Load_Glyph failed: error 6. 
+0

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

Répondre

-1

Cette solution est la meilleure car elle utilise la bonne transformation de coordonnées pour imprimer le texte de haut en bas.

#define LEFT_MARGIN 25 
#define RIGHT_MARGIN 25 
#define TOP_MARGIN 35 
#define BOTTOM_MARGIN 50 
#define BOTTOM_FOOTER_MARGIN 32 
#define DOC_WIDTH 595 
#define DOC_HEIGHT 842 

- (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; 
    } 

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    UIGraphicsBeginPDFPage(); 

    CGContextSetTextDrawingMode (context, kCGTextFill); 
    CGContextSelectFont (context, [font cStringUsingEncoding:NSUTF8StringEncoding], fontSize, kCGEncodingMacRoman);             
    CGContextSetFillColorWithColor(context, [color CGColor]); 

    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); 

    // Initialize an attributed string. 
    CFMutableAttributedStringRef attrString = 
    CFAttributedStringCreateMutable(kCFAllocatorDefault, 0); 
    CFAttributedStringReplaceString (attrString, CFRangeMake(0, 0), (CFStringRef)content); 

    // Create the framesetter with the attributed string. 
    CTFramesetterRef framesetter = 
    CTFramesetterCreateWithAttributedString(attrString); 

    // Create the frame and draw it into the graphics context 
    CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL); 
    CFRange visibleRange = CTFrameGetVisibleStringRange(frame); 

    if (visibleRange.length < [content length]) { 
     NSLog(@"WARNING: Not all displayed on a single page"); 
    } 

    CFRelease(attrString); 
    CFRelease(framesetter); 

    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); 
     CFRelease(frame);  
    } 

    UIGraphicsEndPDFContext(); 

    [fileMetaData release]; 
} 
+0

La mise en forme de plusieurs pages est toujours absente. – user279778

2

Voici une méthode qui permettra de créer un nouveau document PDF. Il ne fait pas le formatage de texte cependant.

- (void) createNewPDF: (NSString *) saveFileName withContent: (NSString *) content forSize: (int) fontSize andFont:(NSString *) font andColor: (UIColor *) color 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *newFilePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:saveFileName]; 

    CGRect a4Page = CGRectMake(0, 0, 595, 842); 

    NSDictionary *fileMetaData = [[NSDictionary alloc] init]; 

    if (!UIGraphicsBeginPDFContextToFile(newFilePath, a4Page, fileMetaData)) { 
     NSLog(@"error creating PDF context"); 
     return; 
    } 

    CGContextRef mpdfContext = UIGraphicsGetCurrentContext(); 
    UIGraphicsBeginPDFPage(); 
    CGContextSetTextMatrix(mpdfContext, CGAffineTransformMake(1, 0, 0, -1, 0, 0)); 
    CGContextSetTextDrawingMode (mpdfContext, kCGTextFill); 
    CGContextSelectFont (mpdfContext, [font cStringUsingEncoding:NSUTF8StringEncoding], fontSize, kCGEncodingMacRoman);             
    CGContextSetFillColorWithColor(mpdfContext, [color CGColor]); 
    CGContextShowTextAtPoint (mpdfContext, 20, 20, [content cStringUsingEncoding:NSUTF8StringEncoding], [content length]); 
    UIGraphicsEndPDFContext(); 

} 
+0

Ces messages d'erreur apparaissent toujours, mais peuvent être ignorés . Notez également que le document PDF comporte un bogue qui n'incorpore pas la police et que le texte n'est pas visible sur Windows ou sur des applications en ligne telles que Google Docs. C'est un bug dans le SDK et il y a un radar dessus. Recherchez dans les forums des développeurs Apple pour plus d'informations. – user279778

6

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.

+0

Je reçois un mauvais accès pour CFAttributedStringReplaceString (attrString, currentRange, (CFStringRef) contenu) .Anyone toute aide plz? – Raj

2

juste pour vous faire savoir que le message d'erreur:

police Type1 invalide: impossible de flux police

et: FT_Load_Glyph a échoué: erreur 6.

disparaissaient dans iOS 4.2

Questions connexes