【问题标题】:Print a QWebView to PDF将 QWebView 打印为 PDF
【发布时间】:2015-08-21 04:44:15
【问题描述】:

我想将 QWebView 打印为 PDF 并将其保存在桌面上。 为此我实现了一个函数,代码如下:

// Print to PDF
// Purpose: print incoming HTML source code to a PDF on the users desktop
// Input:   string containing the HTML source code, string with the desired filename of resulting PDF
// Output:  void
void MainWindow::printToPDF(QString htmlinput, QString filename)
{
    // Set location of resulting PDF
    QString saveLocation = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) + "/" + filename + ".pdf";

    // Initialize printer and set save location
    QPrinter printer(QPrinter::HighResolution);
    printer.setOutputFileName(saveLocation);

    // Create webview and load html source
    QWebView webview;
    webview.setHtml(htmlinput);

    // Create PDF
    webview.print(&printer);
}

现在我的问题是我的应用程序中出现以下错误:

QPainter::begin(): Returned false

我可以确认此错误是由上述函数引起的,另一方面,我仅在另一个项目中尝试了上述代码以确认它有效 - 它确实有效。

有什么建议吗?

【问题讨论】:

    标签: c++ qt pdf qwebview qprinter


    【解决方案1】:

    上面的代码可以完美运行——只要存储 PDF 的位置没有错字——在我的例子中就是这样。

    这样问题就解决了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-21
      • 2014-04-04
      • 2016-09-24
      • 2013-05-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多