我正在尝试将一些HTML转换为PDF文档,以保存在我的后端。但是,我无法设置打印渲染器的纸张大小。即使我更改了“paperRect”,纸张大小也始终保持为A4 -我需要将纸张大小设置为A6。有什么想法吗?
let pageFrame = CGRect(x: 0.0, y: 0.0, width: 595.2, height: 600)
// Set the page frame.
self.setValue(NSValue(cgRect: pageFrame), forKey: "paperRect")发布于 2018-02-09 17:48:36
尝试使用UIGraphicsPDFRenderer而不是UIPrintPageRenderer。您可以使用函数beginPage(withBounds:pageInfo:)制作具有自定义大小的页面
let pdf = renderer.pdfData { (context) in
context.beginPage(withBounds: CGRect(x: 0.0, y: 0.0, width: 595.2, height: 600), pageInfo: [:])
drawYourHTML(on: context) // Implement this function like what you want
}发布于 2020-07-18 15:08:49
通过设置paperRect,这样做是正确的。但是,您可能调用了默认的UIGraphicsBeginPDFPage()来配置每个页面,它使用诸如A4纸张大小之类的默认值。既然您已经获得了paperRect,请尝试使用UIGraphicsBeginPDFPageWithInfo(paperRect, nil)。
参考:https://developer.apple.com/documentation/uikit/1623915-uigraphicsbeginpdfpagewithinfo
https://stackoverflow.com/questions/48702659
复制相似问题