Print QWebView to PDF
I want to print a QWebView to PDF and save it to my desktop. I have implemented a function for this and here is the code:
// 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);
}
Now my problem is that I am getting the following error in my application:
QPainter::begin(): Returned false
I confirm this error is caused by the above function, on the other hand, I have tried the above code only in another project to confirm that it works - what it does.
Any suggestions?
+3
source to share