Qt Webengine Render for print

Is there a way to render HTML / SVG for a printer, PDF, and bitmap using QtWebEngine?

We want to switch from WebKit to WebEngine, so using WebKit QWebView is no longer an option.

+4
source share
2 answers

It is reported that Qt Web Engine will support PDF printing in Qt 5.7 , which is now in beta.

In Qt 5.7, two overloads of the printToPdf () function were added for the QWebEnginePage class .

We have an example of how to use these new features in our company blog .

Qt Web Engine:

QWebEngine: ?

+1

, , :

void printToPDF(const QString& html, const QString& fileName)
{
    #if QT_VERSION >= 0x057000
    QtWebEngine::initialize();
    QWebEnginePage page;
    QEventLoop loop;
    loop.connect(&page, &QWebEnginePage::loadFinished, [&page, &loop, &fileName]() {
        page.printToPdf([&loop, &fileName] (QByteArray ba) {
            QFile f(fileName);
            if (f.open(QIODevice::WriteOnly))
            {
                f.write(ba);
                f.close();
            } else {
                qDebug() << "Error opening file for writing" << fileName << f.errorString();
            }
            loop.exit();
        });
    });
    page.setHtml(html);
    loop.exec();
    #endif
}
+1

All Articles