QWebEngineView之加载html打印PDF

QWebEngineView

通过QWebEngineView加载html文件打印pdf相比自己手动绘制pdf简单的多,制作好对应的html文件,然后Qfile读取html文件模板,替换函数需要替换的value,存成html文件,然后QWebEngineView加载,就直接能够打印成对应格式的pdf了。

//读取html文件
    QFile outFile(QStringLiteral("C://Users//JWGB//Desktop//出厂检验报告.html"));
    if(!outFile.open(QIODevice::ReadWrite))
    {
        qDebug()<<outFile.errorString();
    }
    QByteArray read = outFile.readAll();
    //替换对应的value
    read.replace("#value1#","aaaa");
    read.replace("#value1#","bbbb");
    read.replace("#value1#","CCCCC");
        outFile.close();
    outFile.setFileName(QStringLiteral("C://Users//JWGB//Desktop//出厂检验报告Temp.html");
            if(!outFile.open(QIODevice::ReadWrite ))
            {
                qDebug()<<outFile.errorString();
            }
    outFile.write(read);
    outFile.close();

    ui->textBrowser->setHtml(read);
    m_pView->load(QUrl(QStringLiteral("C://Users//JWGB//Desktop//出厂检验报告Temp.html"));

  //需要等QWebEngineView加载完成之后才能进行打印
  QPageLayout layout(QPageSize(QPageSize::A4), QPageLayout::Portrait, QMarginsF());
  m_pView->page()->printToPdf(QStringLiteral("C://Users//JWGB//Desktop//3p出厂检验报告.pdf"),layout);

上一篇:Qt QWebEngineView加载静态html页面


下一篇:c-Qt 5.8和Pdf.js错误