由于 wkwebview
最终可以在MacOS 11+中打印出内容,因此我试图替换旧的 WebView
依赖项。
但是,使用 wkwebview
正确加载的HTML,使用 nsprintJobDisPosition创建PDF文件:NSPRINTSAVEJOB
生成空白,空文档。他们具有正确的页面计数和尺寸,但要保持空。使用没有 nsprintsavejob
的相同代码效果很好。
这就是我打印内容的方式:
NSPrintInfo *printInfo = NSPrintInfo.sharedPrintInfo;
[printInfo.dictionary addEntriesFromDictionary:@{
NSPrintJobDisposition: NSPrintSaveJob,
NSPrintJobSavingURL: url
}];
NSPrintOperation *printOperation =[(WKWebView*)_webView printOperationWithPrintInfo:printInfo];
printOperation.view.frame = NSMakeRect(0,0, printInfo.paperSize.width, printInfo.paperSize.height);
printOperation.showsPrintPanel = NO;
printOperation.showsProgressPanel = YES;
[printOperation runOperation];
设置 show printpanel
a a true
和不计算字典将显示一个正常的打印对话框,结果看起来完全正常。
我对自己做错了什么感到困惑,或者是从 wkwebview
stall 中打印的?
样本项目:
创建pdf 和 print 按钮共享相同的代码(在 previeviewView
)中,但另一个产生一个空文件,然后将其加载到 pdfview
中。
我在网上收集的东西进一步发现
,这可能是一个长期的错误。
看来 wkwebview
在背景中创建PDF文件时使用其 createpdf
方法,但是该方法呈现 上显示的内容,而不是带有页面中断和打印样式的实际文档内容。它似乎也忽略了一些 nsprintinfo
设置。
As WKWebView
contents can finally be printed out in macOS 11+, I'm trying to replace old WebView
dependencies.
However, with the HTML correctly loaded in WKWebView
, creating PDF files using NSPrintJobDisposition: NSPrintSaveJob
produces blank, empty documents. They have the correct page count and sizing, but remain empty. Using the same code without NSPrintSaveJob
works just fine.
This is how I'm printing the contents:
NSPrintInfo *printInfo = NSPrintInfo.sharedPrintInfo;
[printInfo.dictionary addEntriesFromDictionary:@{
NSPrintJobDisposition: NSPrintSaveJob,
NSPrintJobSavingURL: url
}];
NSPrintOperation *printOperation =[(WKWebView*)_webView printOperationWithPrintInfo:printInfo];
printOperation.view.frame = NSMakeRect(0,0, printInfo.paperSize.width, printInfo.paperSize.height);
printOperation.showsPrintPanel = NO;
printOperation.showsProgressPanel = YES;
[printOperation runOperation];
Setting showsPrintPanel
as true
and uncommenting the dictionary will display a normal print dialog, and the result looks completely normal there.
I'm confused about what I am doing wrong, or is printing from WKWebView
still this buggy?
Sample project:
https://www.dropbox.com/s/t220cn8orooorwb/WebkitPDFTest.zip?dl=1
Create PDF and Print buttons share the same code (found in PreviewView
), but the other produces an empty file, which is then loaded into the PDFView
.
Further findings
By what I've gathered online, this could be a long-running bug.
It seems that WKWebView
prefers using its createPDF
method when creating PDF files in the background, but that method renders what is displayed on screen, not the actual document content with page breaks and print stylization. It also appears to ignore some NSPrintInfo
settings.
发布评论
评论(1)
wkwebview
打印是非常记录的。空白页面的原因是Webkit中的run-loop代码,所有打印都必须发生异步。这就是为什么
[printOperation runoperation]
/printOperation.Runoperation()
不起作用。相反,您需要调用奇怪的命名RunoperationModalforWindow
。尽管方法名称是指模态窗口,但您不需要 显示模式,但是此方法以某种方式使打印操作异步。您还需要使用处理程序方法来捕获结果:
WKWebView
printing is very badly documented. The reason for the blank pages is the run-loop code in WebKit, and all printing must happen asynchronously.This is why
[printOperation runOperation]
/printOperation.runOperation()
does not work. Instead, you need to call the strangely namedrunOperationModalForWindow
. Although the method name refers to a modal window, you don't need to display the modal, but this method somehow makes the print operation asynchronous.You also need to have a handler method to catch the results: