Qt Webengine 渲染打印 [英] Qt Webengine Render to Print

查看:89
本文介绍了Qt Webengine 渲染打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法用 QtWebEngine 将 HTML/SVG 渲染到打印机、PDF 和光栅图像?

Is there any way to render HTML/SVG to printer, PDF, and raster images with QtWebEngine?

我们想从 WebKit 切换到 WebEngine,所以不再使用 WebKit 的 QWebView.

We want to switch from WebKit to WebEngine, so using WebKit's QWebView is not an option anymore.

推荐答案

从工作线程中获得可调用的打印需要一些修补:

It took a bit of tinkering to get printing callable from a worker-thread:

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
}

这篇关于Qt Webengine 渲染打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆