将QWebView打印成PDF格式

3

我想将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

我可以确认这个错误是由上述函数引起的,另一方面我在另一个项目中单独尝试了上述代码以确认它是有效的。
有什么建议吗?
1个回答

1
上述代码完美运行 - 只要存储PDF的位置没有拼写错误 - 在我的情况下就有。因此问题得到解决。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接