Important: Please read the Qt Code of Conduct -

[ Solved] Print QWebView to PDF

  • [ Solved] Had as typo in the location string - sorry for bothering!

    I want to print an already loaded string containing HTML source code to PDF.
    So here's the function I implemented:

    // 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);
        // Create webview and load html source
        QWebView webview;
        // Create PDF

    Unfortunately it results in the following error and no PDF is created:

    QPainter::begin(): Returned false

    By the way, the newby I am I confirmed the techniques used in the function in another project and it worked fine there.
    Any suggestions? Thanks for all your efforts in advance!

  • Moderators

    Hi @ASSeeger, Just a shot in he dark, does it work if you create QWebView on heap rather than stack ? Because it gets destroyed as soon as functions exits.

  • @p3c0 Unfortunately it doesn't help – I now tried it within the calling method itself without creating a specific function and the result is the same...

  • Problem solved: I obviously had as typo in the file location string- that was the reason it didn't work.

  • Moderators

    @ASSeeger Glad that you found the problem, Happy Coding ..

Log in to reply