簡體   English   中英

將Web視圖內容另存為pdf文件

[英]save a web view content as pdf file

注意:我使用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() 
}

在此輸入圖像描述

現在我想要另一個按鈕,它將內容直接保存為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結果看起來很糟糕:

在此輸入圖像描述

有人有想法嗎?

更新這是我的網絡視圖結果的一部分:

在此輸入圖像描述

這是我的打印預覽/ pdf文件的結果/缺少顏色,但現在無處不在。 “徽標”(圖片)將顯示顏色: 在此輸入圖像描述

問題是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不要顯示打印面板,運行操作和presto! PDF打印。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM