从MacOS 11中的WKWebView创建PDF。+

发布于 2025-01-25 02:25:19 字数 1543 浏览 4 评论 0 原文

由于 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铃予 2025-02-01 02:25:19

wkwebview 打印是非常记录的。空白页面的原因是Webkit中的run-loop代码,所有打印都必须发生异步

这就是为什么 [printOperation runoperation] / printOperation.Runoperation()不起作用。相反,您需要调用奇怪的命名 RunoperationModalforWindow 。尽管方法名称是指模态窗口,但您不需要 显示模式,但是此方法以某种方式使打印操作异步。

// Create print operation
NSPrintOperation *printOperation = [webView printOperationWithPrintInfo:printInfo];

// Set web view bounds - without this, the operation will crash
printOperation.view.frame = NSMakeRect(0,0, printInfo.paperSize.width, printInfo.paperSize.height);

// Print it out
[printOperation runOperationModalForWindow:self.window delegate:self didRunSelector:@selector(printOperationDidRun:success:contextInfo:) contextInfo:nil];

您还需要使用处理程序方法来捕获结果:

- (void)printOperationDidRun:(id)operation success:(bool)success contextInfo:(nullable void *)contextInfo {
    // Do something with your print
}

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 named runOperationModalForWindow. 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.

// Create print operation
NSPrintOperation *printOperation = [webView printOperationWithPrintInfo:printInfo];

// Set web view bounds - without this, the operation will crash
printOperation.view.frame = NSMakeRect(0,0, printInfo.paperSize.width, printInfo.paperSize.height);

// Print it out
[printOperation runOperationModalForWindow:self.window delegate:self didRunSelector:@selector(printOperationDidRun:success:contextInfo:) contextInfo:nil];

You also need to have a handler method to catch the results:

- (void)printOperationDidRun:(id)operation success:(bool)success contextInfo:(nullable void *)contextInfo {
    // Do something with your print
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文