OGeek|极客世界-中国程序员成长平台

标题: ios - 在 Swift 中创建 PDF : Create New Page Automatically [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:48
标题: ios - 在 Swift 中创建 PDF : Create New Page Automatically

我想在我的应用程序中包含一个(非常长的)表格的 PDF 创建。

我使用本教程从 HTML 模板创建 PDF 文件:https://www.appcoda.com/pdf-generation-ios/

这很好用,但是,当我有一个很长的表格(不同大小的行)时,我想自动开始一个新页面。这怎么可能(检测当前 PDF 页面是否已满)?

示例:项目太多,没有打印页脚的空间并且缺少一些项目

Example



Best Answer-推荐答案


由于页面高度最小,页脚与项目重叠。所以用下面的代码解决这个问题:

在 InvoiceComposer.swift 类中

func drawPDFUsingPrintPageRenderer(printPageRenderer: UIPrintPageRenderer) -> NSData! {
    let data = NSMutableData()
//UIGraphicsBeginPDFContextToData(data, CGRect.zero, nil)
    UIGraphicsBeginPDFContextToData(data, CGRect(x:0,y:0,width:612,height:822), nil)// Edit CGRect.zero with CGRect(x:0,y:0,width:612,height:822) makes space for footer
    for i in 0..<printPageRenderer.numberOfPages {
        UIGraphicsBeginPDFPage()
        printPageRenderer.drawPage(at: i, in: UIGraphicsGetPDFContextBounds())//Print(0.0, 0.0, 612.0, 792.0) for UIGraphicsGetPDFContextBounds()
    }

    UIGraphicsEndPDFContext()

    return data
}

结果图片 enter image description here 快乐编码

关于ios - 在 Swift 中创建 PDF : Create New Page Automatically,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44374171/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4