• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 在 Xcode 中生成多页 PDF 文件

[复制链接]
菜鸟教程小白 发表于 2022-12-12 09:46:53 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在尝试生成一个包含多个页面的 PDF 文件。我正在使用此代码:

-(void)createPDFfromUIUIWebView *)aView saveToDocumentsWithFileNameNSString*)aFilename
{
    // Creates a mutable data object for updating with binary data, like a byte array
    NSMutableData *pdfData = [NSMutableData data];

    // Points the pdf converter to the mutable data object and to the UIView to be converted

    UIWebView *myView = [[UIWebView alloc] init];

    [myView setBounds:CGRectMake(0,0, 320, 480)];

    UIGraphicsBeginPDFContextToData(pdfData, myView.bounds, nil);

    CGContextRef pdfContext = UIGraphicsGetCurrentContext();

    for (int i = 0; i < 4; i++) {

        UIGraphicsBeginPDFPage();

        aView.layer.bounds = CGRectMake(0,480* -i , 320, 480);

        [aView.layer renderInContext:pdfContext];

    }

    [aView.layer renderInContext:pdfContext];

    // remove PDF rendering context
    UIGraphicsEndPDFContext();

    // Retrieves the document directories from the iOS device
    NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

    NSString* documentDirectory = [documentDirectories objectAtIndex:0];
    NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];

    // instructs the mutable data object to write its context to a file on disk
    [pdfData writeToFile:documentDirectoryFilename atomically:YES]; 
}

这段代码的问题是,它只生成一个包含 4 个相似页面的 PDF 文件,它们都是我原始页面的最后 480 像素。 (aView) 当我移动 `[aView.layer renderInContext:pdfContext];在 FOR 循环之外,它会生成一个包含 4 页的 PDF 文件,但前 3 页将为空白,最后一页有一些数据。

你们知道我该如何解决这个问题吗?!



Best Answer-推荐答案


bounds 表示层在其父层中的位置。所以当你渲染一个层时,它使用它的 frame 来确定它的内容。 您必须将 View 作为 subview 放置在另一个 View 中,例如 aView2。然后像你已经做的那样设置 aView 的边界,然后渲染 aView2

它应该可以工作。

关于ios - 在 Xcode 中生成多页 PDF 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15499859/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap