Ipad 创建多页PDF时出错

Ipad 创建多页PDF时出错,ipad,pdf-generation,multipage,Ipad,Pdf Generation,Multipage,是否有人已经在iPad应用程序中创建了PDF文档?我看到UIKit中有新的函数来实现这一点,但我找不到任何代码示例 BOOL UIGraphicsBeginPDFContextToFile ( NSString *path, CGRect bounds, NSDictionary *documentInfo ); void UIGraphicsBeginPDFPage ( void ); 我发现了一个应该在iPhone上运行的示例,但这给了我错误: Fri Apr 3

是否有人已经在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.
Fri Apr 30 11:55:32 wks104.hs.local PDF[1963]:CGFont/Freetype:函数“create_subset”目前尚未实现。
4月30日星期五11:55:32 wks104.hs.local PDF[1963]:无效的Type1字体:无法流式显示字体。
4月30日星期五11:55:32 wks104.hs.local PDF[1963]:FT_Load_Glyph失败:错误6。
4月30日星期五11:55:32 wks104.hs.local PDF[1963]:FT_Load_Glyph失败:错误6。
4月30日星期五11:55:32 wks104.hs.local PDF[1963]:FT_Load_Glyph失败:错误6。
4月30日星期五11:55:32 wks104.hs.local PDF[1963]:FT_Load_Glyph失败:错误6。

以下是一种创建新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();

}

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

#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];
}
#定义左边距25
#定义右边距25
#定义上边缘35
#定义下_边距50
#定义底部页脚页边32
#定义文档宽度595
#定义文档高度842
-(void)createPDF:(NSString*)文件名和内容:(NSString*)内容大小:(int)字体大小和字体:(NSString*)字体和颜色:(UIColor*)颜色
{
NSArray*Path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,是);
NSString*newFilePath=[[paths objectAtIndex:0]stringByAppendingPathComponent:fileName];
CGRect A4页面=CGRectMake(0,0,文档宽度,文档高度);
NSDictionary*fileMetaData=[[NSDictionary alloc]init];
if(!UIGraphicsBeginPDFContextToFile(newFilePath,a4页,fileMetaData)){
NSLog(@“创建PDF上下文时出错”);
回来
}
CGContextRef context=UIGraphicsGetCurrentContext();
UIGraphicsBeginPDFPage();
CGContextSetTextDrawingMode(上下文,kCGTextFill);
CGContextSelectFont(上下文,[font cStringUsingEncoding:NSUTF8StringEncoding]、fontSize、kCGEncodingMacRoman);
CGContextSetFillColorWithColor(上下文,[color CGColor]);
CGMutablePathRef path=CGPathCreateMutable();
CGRect bounds=CGRectMake(左边距,
上边距,
单据宽度-右边距-左边距,
单据高度-上边距-下边距
);
CGPathAddRect(路径,空,边界);
//初始化属性化字符串。
CFMutableAttributedStringRef属性字符串=
CFAttributedStringCreateMutable(kCFAllocatorDefault,0);
CFAttributedStringReplaceString(attrString,CFRangeMake(0,0),(CFStringRef)内容);
//使用属性化字符串创建框架设置器。
CTFramesetterRef帧设置器=
CTFramesetterCreateWithAttributedString(attrString);
//创建框架并将其绘制到图形上下文中
CTFrameRef frame=CTFramesetterCreateFrame(framesetter,CFRangeMake(0,0),path,NULL);
CFRange visibleRange=CTFrameGetVisibleStringRange(帧);
如果(visibleRange.length<[内容长度]){
NSLog(@“警告:并非全部显示在单个页面上”);
}
CFRelease(attrString);
CFRelease(框架设置器);
如果(帧){
CGContextSaveGState(上下文);
CGContextTranslateCm(上下文,0,bounds.origin.y);
CGContextScaleCTM(上下文,1,-1);
CGContextTranslateCm(上下文,0,-(bounds.origin.y+bounds.size.height));
CTFrameDraw(框架、上下文);
CGContextRestoreGState(上下文);
碳纤维板(框架);
}
UIGraphicsSendPdfContext();
[文件元数据发布];
}

通过修改上面的代码,我实际上可以创建多页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文件?我简直不敢相信。我至少希望那些“常见的嫌疑犯”字体,如:信使,泰晤士报新罗马,等等。。。有待支持

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


提前感谢。

只是想让您知道错误消息:

无效的Type1字体:无法流式显示字体

以及: FT_加载_图示符失败:错误6


在iOS 4.2中消失

UIKit已在OS 3.2中扩展以支持PDF功能。因此,不使用pdfContext=cgpdcontextcreatewithurl(url,&pageRect,docInfo);它将使用pdfContext=uigraphicsbeginpdfcontextofile(path,&pageRect,docInfo);我刚才看到的示例如下:这些错误消息仍然出现,但可以忽略。还要注意的是,PDF文档中有一个bug,它没有嵌入字体,并且文本在Windows或Google Docs等在线应用程序中不可见。这是SDK中的一个bug,上面有一个雷达。在Apple开发者论坛上搜索更多信息。我无法访问CFAttributedStringReplaceString(attrString,currentRange,(CFStringRef)内容)。有任何帮助吗?