NSPrintOperation - 保存到位置而不提示

时间:2022-03-10 21:10:18

I have a GitHub repository that allows the user to save a paginated PDF on Mac from some HTML by loading it into a WebView and using an NSPrintOperation (specifically, an NSPrintSaveJob with showsPrintPanel set to false) lets the user save that PDF to any location on their Mac with a save panel similar to the default NSSavePanel. However, I'm experimenting with the code and I'd like to instead save the created PDF to a particular folder (/Users/owlswipe/Downloads/) without the save panel.

我有一个GitHub存储库,允许用户通过将某个HTML加载到WebView中来保存Mac上的分页PDF,并使用NSPrintOperation(具体来说,将showPrintPanel设置为false的NSPrintSaveJob)允许用户将该PDF保存到任何位置。他们的Mac有一个类似于默认NSSavePanel的保存面板。但是,我正在尝试使用代码,而我想将创建的PDF保存到特定文件夹(/ Users / owlswipe / Downloads /)而不使用保存面板。

My code to save a PDF from a WebView (with a save panel) is currently this:

我从WebView(带有保存面板)保存PDF的代码目前是这样的:

let printOpts: [String : AnyObject] = [NSPrintJobDisposition:NSPrintSaveJob as AnyObject]
let printInfo: NSPrintInfo = NSPrintInfo(dictionary: printOpts)
printInfo.paperSize = NSMakeSize(595.22, 841.85)
let printOp: NSPrintOperation = NSPrintOperation(view: webView.mainFrame.frameView.documentView, printInfo: printInfo)
printOp.showsPrintPanel = false
printOp.showsProgressPanel = false
printOp.run()

How can I adapt that code to save the PDF to a preset folder instead of to the user's choice of folders from a save panel?

如何调整该代码以将PDF保存到预设文件夹而不是用户从保存面板中选择的文件夹?

1 个解决方案

#1


1  

This piece of code is working for me in Swift 4/Cocoa to accomplish what you want, however there's a bit more code in there as it is rendering the contents of a WKWebView into 8.5" x 11" pages for a PDF.

这段代码在Swift 4 / Cocoa中为我工作,以实现你想要的,但是那里有更多的代码,因为它将WKWebView的内容渲染为8.5“x 11”页面的PDF。

So for your app, there would be appropriate adjustments for your content stream / print object, but the configuration of the print operation will be the same to get the "no dialog" results you're wanting.

因此,对于您的应用程序,将对您的内容流/打印对象进行适当的调整,但打印操作的配置将相同,以获得您想要的“无对话”结果。

However, to test it out you could just dump your string into the webview and use the code as is. Generated files appear in the root of your "user/documents" directory.

但是,要测试它,您可以将您的字符串转储到webview中并按原样使用代码。生成的文件显示在“user / documents”目录的根目录中。

static func createPDF(htmlString: String, streamId: String = "someStream") {

    let webView = WebView()
    webView.mainFrame.loadHTMLString(htmlString, baseURL: nil)
    let when = DispatchTime.now() + 1

    DispatchQueue.main.asyncAfter(deadline: when) {

       let directoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
       let directoryURLStr = directoryURL.absoluteString+"\(streamId).pdf"

       let outputFilePath = URL(string: directoryURLStr)

       let printOpts: [NSPrintInfo.AttributeKey : Any] = [
         NSPrintInfo.AttributeKey.jobDisposition : NSPrintInfo.JobDisposition.save,
         NSPrintInfo.AttributeKey.jobSavingURL   : outputFilePath
       ]

       let printInfo: NSPrintInfo = NSPrintInfo(dictionary: printOpts)
       let baseMargin: CGFloat    = 9.0; // .125"

       printInfo.paperSize    = NSMakeSize(612, 792); // 8.5" x 11/2"
       printInfo.topMargin    = baseMargin
       printInfo.leftMargin   = baseMargin
       printInfo.rightMargin  = baseMargin
       printInfo.bottomMargin = baseMargin

       let printOp: NSPrintOperation = NSPrintOperation(view: webView.mainFrame.frameView.documentView, printInfo: printInfo)

       printOp.showsPrintPanel = false
       printOp.showsProgressPanel = false
       printOp.run()

       Swift.print("Document complete: \(outputFilePath!.absoluteString)")
   }
 }

#1


1  

This piece of code is working for me in Swift 4/Cocoa to accomplish what you want, however there's a bit more code in there as it is rendering the contents of a WKWebView into 8.5" x 11" pages for a PDF.

这段代码在Swift 4 / Cocoa中为我工作,以实现你想要的,但是那里有更多的代码,因为它将WKWebView的内容渲染为8.5“x 11”页面的PDF。

So for your app, there would be appropriate adjustments for your content stream / print object, but the configuration of the print operation will be the same to get the "no dialog" results you're wanting.

因此,对于您的应用程序,将对您的内容流/打印对象进行适当的调整,但打印操作的配置将相同,以获得您想要的“无对话”结果。

However, to test it out you could just dump your string into the webview and use the code as is. Generated files appear in the root of your "user/documents" directory.

但是,要测试它,您可以将您的字符串转储到webview中并按原样使用代码。生成的文件显示在“user / documents”目录的根目录中。

static func createPDF(htmlString: String, streamId: String = "someStream") {

    let webView = WebView()
    webView.mainFrame.loadHTMLString(htmlString, baseURL: nil)
    let when = DispatchTime.now() + 1

    DispatchQueue.main.asyncAfter(deadline: when) {

       let directoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
       let directoryURLStr = directoryURL.absoluteString+"\(streamId).pdf"

       let outputFilePath = URL(string: directoryURLStr)

       let printOpts: [NSPrintInfo.AttributeKey : Any] = [
         NSPrintInfo.AttributeKey.jobDisposition : NSPrintInfo.JobDisposition.save,
         NSPrintInfo.AttributeKey.jobSavingURL   : outputFilePath
       ]

       let printInfo: NSPrintInfo = NSPrintInfo(dictionary: printOpts)
       let baseMargin: CGFloat    = 9.0; // .125"

       printInfo.paperSize    = NSMakeSize(612, 792); // 8.5" x 11/2"
       printInfo.topMargin    = baseMargin
       printInfo.leftMargin   = baseMargin
       printInfo.rightMargin  = baseMargin
       printInfo.bottomMargin = baseMargin

       let printOp: NSPrintOperation = NSPrintOperation(view: webView.mainFrame.frameView.documentView, printInfo: printInfo)

       printOp.showsPrintPanel = false
       printOp.showsProgressPanel = false
       printOp.run()

       Swift.print("Document complete: \(outputFilePath!.absoluteString)")
   }
 }