ios - 在 Swift 中创建 PDF : Create New Page Automatically

标签 ios objective-c swift pdf

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

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

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

示例:项目太多,没有空间打印页脚,有些项目丢失了

Example

最佳答案

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

在 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/

相关文章:

ios - 自定义部分标题 View 时部分标题标题丢失

ios - 部分显示 UITableViewController 就像 Facebook iPhone 应用程序

iphone - 应用内购买 : Stuck at paymentWithProductIdentifiers - which is deprecated

iOS10.3.x 无法在 MPMusicPlayer 上为 Apple Music 轨道设置 nowPlayingItem

ios - 警告 : Attempt to present <UIImagePickerController> on <MFSideMenuContainerViewController> whose view is not in the window hierarchy

objective-c - 通过搜索单词的一部分来获取单词

iphone - 正确使用 transitionFromViewController :toViewController:duration:options:animations:completion:

json - 参数 Encoder Failed because invalidRootObject?

ios - 使用 SwiftUI 成功登录后导航

ios - iPhone 弹出窗口上的导航栏