将Web视图内容另存为pdf文件

时间:2017-09-20 18:44:02

标签: swift macos cocoa pdf webview

注意:我使用swift 4 for osx。 我想从WebView生成一个pdf文件。

目前,我的WebView加载了一个html字符串并成功显示。 如果按下按钮,打印面板将打开,并准备以正确的格式打印我的WebView内容。

这是我的打印代码:

var webView = WebView()
var htmlString = "MY HTML STRING"

override func viewDidLoad() {
    webView.mainFrame.loadHTMLString(htmlString, baseURL: nil)
}

func webView(_ sender: WebView!, didFinishLoadFor frame: WebFrame!) {

    let printInfo = NSPrintInfo.shared
    printInfo.paperSize = NSMakeSize(595.22, 841.85)
    printInfo.isHorizontallyCentered = true
    printInfo.isVerticallyCentered = true
    printInfo.orientation = .portrait
    printInfo.topMargin = 50
    printInfo.rightMargin = 0
    printInfo.bottomMargin = 50
    printInfo.leftMargin = 0
    printInfo.verticalPagination = .autoPagination
    printInfo.horizontalPagination = .fitPagination
    //webView.mainFrame.frameView.printOperation(with: printInfo).run()

    let printOp: NSPrintOperation = NSPrintOperation(view: webView.mainFrame.frameView.documentView, printInfo: printInfo)
    printOp.showsPrintPanel = true
    printOp.showsProgressPanel = false
    printOp.run() 
}

enter image description here

现在我想要另一个按钮,它将内容直接保存为pdf文件。

我试过了:

let pdfData = webView.mainFrame.frameView.documentView.dataWithPDF(inside: webView.mainFrame.frameView.documentView.frame)
let document = PDFDocument(data: pdfData)
let fileURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent("myPDF.pdf")
document?.write(to: fileURL)

但是我的pdf结果看起来很可怕:

enter image description here

有人有想法吗?

更新 这是我的网络视图结果的一部分:

enter image description here

这是我的打印预览/ pdf文件的结果/颜色缺失,但现在无处不在。 “徽标”(图片)将显示颜色: enter image description here

1 个答案:

答案 0 :(得分:6)

问题是dataWithPDF使用视图的当前帧来决定生成的PDF应该有多宽。由于您应用中的WebView框架可能比8.5 / 11"页面,您获得的PDF不够广泛。您可以WebView的框架调整到合适的尺寸,制作PDF,然后将其调整回来,或者您可以创建新的WebView,渲染在屏幕外,将其设置为适当的大小,并创建PDF。不过,这在背后有点痛苦。如果有一种方法可以以编程的方式做什么,并不会很好。" PDF" “打印”对话框中的按钮是否可以,因为打印系统会自动为您处理所有这些内容?

好吧,事实证明你可以!但你必须深入研究记录不佳的核心印刷世界。

func makePDF(at url: URL, for webView: WebView, printInfo: NSPrintInfo) throws {
    webView.preferences.shouldPrintBackgrounds = true

    guard let printOp = webView.mainFrame.frameView.printOperation(with: printInfo) else {
        throw MyPrintError.couldntGetPrintOperation // or something like this
    }

    let session = PMPrintSession(printOp.printInfo.pmPrintSession())
    let settings = PMPrintSettings(printOp.printInfo.pmPrintSettings())

    if PMSessionSetDestination(session,
                               settings,
                               PMDestinationType(kPMDestinationFile),
                               kPMDocumentFormatPDF as CFString,
                               url as CFURL) != noErr {
        throw MyPrintError.couldntSetDestination // or something like this
    }

    printOp.showsPrintPanel = false
    printOp.run()
}

密钥是PMSessionSetDestination调用,它允许我们将打印会话配置为打印到PDF而不是实际的打印机。然后我们告诉NSPrintOperation不要显示打印面板,运行操作,然后! PDF打印。

相关问题