当前位置:  开发笔记 > IOS > 正文

创建多页PDF时出错

如何解决《创建多页PDF时出错》经验,为你挑选了1个好方法。

有人已经在iPad应用程序中创建了PDF文档吗?我看到UIKit中有新功能来执行此操作,但我找不到任何代码示例.

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

void UIGraphicsBeginPDFPage (
   void
);

我找到了一个可以在iPhone上运行的例子,但这给了我错误:

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

Friendlydeve.. 6

我实际上通过修改上面的代码来创建多页PDF创建,例如:

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

但是,如上所述,它忽略了我尝试设置的字体.我总是得到一些东西,看起来像"Helvetica".

那么,目前无法在iPad/iPhone上创建PDF文件,嵌入设备上使用的字体?我简直不敢相信.我至少希望那些"通常的嫌疑人"字体,如:Courier,Times New Roman等......得到支持.

如果有任何人有关于变通方法的更多信息或有用的提示,欢迎分享.

提前致谢.



1> Friendlydeve..:

我实际上通过修改上面的代码来创建多页PDF创建,例如:

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

但是,如上所述,它忽略了我尝试设置的字体.我总是得到一些东西,看起来像"Helvetica".

那么,目前无法在iPad/iPhone上创建PDF文件,嵌入设备上使用的字体?我简直不敢相信.我至少希望那些"通常的嫌疑人"字体,如:Courier,Times New Roman等......得到支持.

如果有任何人有关于变通方法的更多信息或有用的提示,欢迎分享.

提前致谢.

推荐阅读
殉情放开那只小兔子
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有