创建多页 PDF 时出错

发布于 2024-08-31 05:14:47 字数 921 浏览 13 评论 0原文

有人已经在 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] <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.

Has anyone already created a PDF document in an iPad app? I see that there are new functions in the UIKit to do this, but I can't find any code example for it.

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

void UIGraphicsBeginPDFPage (
   void
);

I found an example that is supposed to work on the iPhone, but this gives me errors:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

窗影残 2024-09-07 05:14:47

实际上,我通过修改上面的代码来创建多页 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 等。

如果有人有更多信息或有关解决方法的有用提示,欢迎分享。

提前致谢。

I actually got multi page PDF creation working by modifying the code above, like:

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

However, as mentioned above, it ignores the font I try to set. I always get something, looking like "Helvetica".

So, there is currently no way to create a PDF file on an iPad/iPhone, that embeds the font used on the device? I can hardly believe that. I would have at least hoped for those "usual suspects" fonts , like: Courier, Times New Roman, etc... to be supported.

If anyone out there has more information or useful tips on workarounds, please feel welcome to share.

Thanks in advance.

尴尬癌患者 2024-09-07 05:14:47

这是创建新 PDF 文档的方法。但它不进行文本格式化。

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

}

Here is a method that will create a new PDF document. It does not do text formatting though.

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

}
故事和酒 2024-09-07 05:14:47

只是为了让您知道错误消息:

无效的 Type1 字体:无法流式传输字体

以及:
FT_Load_Glyph 失败:错误 6.

在 iOS 4.2 中消失

just to let you know that the error message:

invalid Type1 font: unable to stream font

and:
FT_Load_Glyph failed: error 6.

disappeared in iOS 4.2

栖迟 2024-09-07 05:14:47

这是一个更好的解决方案,因为它使用正确的坐标变换来使文本从上到下打印。

#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];
}

This is a better solution as it uses the correct coordinate transform to get the text to print from the top down.

#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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文