简体   繁体   English

如何将 QWebEngineView 渲染到打印机?

[英]How to render QWebEngineView to a printer?

My goal: I would like to be able to store a HTML page taken from the qrc as a PDF file using qt 5.6.1.我的目标:我希望能够使用 qt 5.6.1 将从 qrc 中获取的 HTML 页面存储为 PDF 文件。

Limitations:限制:

  • I can't use the QWebEnginePage::print nor QWebEnginePage::printToPdf methods since they have been added in versions 5.8 and 5.7 respectively我不能使用QWebEnginePage::printQWebEnginePage::printToPdf方法,因为它们已分别添加到版本 5.8 和 5.7
  • Using QTextDocument instead is not an option, since I need a full HTML support, not just the limited subset the QTextDocument offers使用QTextDocument不是一种选择,因为我需要完整的 HTML 支持,而不仅仅是QTextDocument提供的有限子集

The problem: The code below indeed shows the page on the screen, then the doc.pdf is created, which means that the page has been loaded without errors, but when I open the created PDF file a blank page is shown.问题:下面的代码确实在屏幕上显示了该页面,然后创建了 doc.pdf,这意味着该页面已正确加载,但是当我打开创建的 PDF 文件时,会显示一个空白页面。

Any ideas what I am doing wrong or missing?任何想法我做错了什么或错过了什么?

#include "MainWindow.h"
#include "ui_MainWindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QWebEngineView *view = new QWebEngineView(this);

    setCentralWidget(view);

    QFile file(":/print.htm");
    QString str;

    if (file.open(QFile::ReadOnly | QFile::Text))
    {
        str.append(file.readAll());
        view->setHtml(str);
        file.close();
    }

    connect(view, &QWebEngineView::loadFinished, this, &MainWindow::on_loadFinished);
}

void MainWindow::on_loadFinished(bool ok)
{
    if (ok)
    {
        QPrinter printer(QPrinter::HighResolution);

        printer.setOutputFormat(QPrinter::PdfFormat);
        printer.setPageSize(QPageSize(QPageSize::A4));
        printer.setPageOrientation(QPageLayout::Portrait);
        printer.setColorMode(QPrinter::GrayScale);
        printer.setOutputFileName("doc.pdf");

        static_cast<QWebEngineView *>(sender())->render(&printer);
    }
}

As mentioned in my comment, it seems to me that updating is the only/best solution, because Qt 5.6 does not support printing a QWebEngineView :正如我在评论中提到的,在我看来,更新是唯一/最好的解决方案,因为 Qt 5.6 不支持打印QWebEngineView

References:参考资料:

  • Qt mailing list : Qt 邮件列表

    1. How can I print a QWebEngineView content ??如何打印 QWebEngineView 内容? with QWebView I only need to call the print method使用 QWebView 我只需要调用打印方法

    Printing with Chromium is surprisingly difficult to implement, but we aim to support printing to PDF in Qt 5.7:使用 Chromium 打印非常难以实现,但我们的目标是在 Qt 5.7 中支持打印为 PDF:

  • Qt forum : Qt论坛

    Looking at this and this it seems we wont have it in Qt 5.5 too.看看这个这个,我们似乎也不会在 Qt 5.5 中拥有它。

Alternative: using the old QtWebKit替代方案:使用旧的 QtWebKit

One can still use the print method from the deprecated QWebView class (which is replaced by QWebEngineView ) if you build Qt from source (or use an older Qt version, for example Qt 5.5) as mentioned in the Release Notes of Qt 5.6 :如果您从源代码构建 Qt(或使用较旧的 Qt 版本,例如 Qt 5.5),则仍然可以使用已弃用的QWebView类(由QWebEngineView替换)中的print方法,如Qt 5.6发行说明中所述:

With Qt 5.6 the following modules are no longer part of the release packages, but users can still build them from source:在 Qt 5.6 中,以下模块不再是发布包的一部分,但用户仍然可以从源代码构建它们:

  • Qt WebKit Qt WebKit

Note that I do not recommend this alternative, only use it if you are not able to update to the latest Qt version请注意,我不推荐此替代方案,仅当您无法更新到最新的 Qt 版本时才使用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM