【问题标题】:UIPrintPageRenderer paper size not changingUIPrintPageRenderer 纸张大小不变
【发布时间】:2018-02-09 09:30:11
【问题描述】:

我正在尝试将一些 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")

【问题讨论】:

    标签: swift uiprintpagerenderer


    【解决方案1】:

    尝试使用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
    
    }
    

    【讨论】:

      【解决方案2】:

      设置paperRect 是正确的。但是,您可能调用了默认的UIGraphicsBeginPDFPage() 来配置每个页面,它使用的是默认值,例如 A4 纸张大小。尝试改用UIGraphicsBeginPDFPageWithInfo(paperRect, nil),因为您已经获得了paperRect

      参考:https://developer.apple.com/documentation/uikit/1623915-uigraphicsbeginpdfpagewithinfo

      【讨论】:

        猜你喜欢
        • 2014-03-04
        • 1970-01-01
        • 2020-08-02
        • 2013-05-15
        • 2017-02-21
        • 2011-01-30
        • 2015-09-09
        • 1970-01-01
        相关资源
        最近更新 更多