我想将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
我可以确认这个错误是由上述函数引起的,另一方面我在另一个项目中单独尝试了上述代码以确认它是有效的。
有什么建议吗?