Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [ Solved] Print QWebView to PDF
Forum Updated to NodeBB v4.3 + New Features

[ Solved] Print QWebView to PDF

Scheduled Pinned Locked Moved General and Desktop
qwebviewpdf
5 Posts 2 Posters 3.8k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ASSeeger
    wrote on last edited by ASSeeger
    #1

    [ 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);
        printer.setOutputFileName(saveLocation);
    
        // Create webview and load html source
        QWebView webview;
        webview.setHtml(htmlinput);
    
        // Create PDF
        webview.print(&printer);
    }
    

    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!

    p3c0P 1 Reply Last reply
    0
    • A ASSeeger

      [ 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);
          printer.setOutputFileName(saveLocation);
      
          // Create webview and load html source
          QWebView webview;
          webview.setHtml(htmlinput);
      
          // Create PDF
          webview.print(&printer);
      }
      

      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!

      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      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.

      157

      1 Reply Last reply
      0
      • A Offline
        A Offline
        ASSeeger
        wrote on last edited by
        #3

        @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...

        1 Reply Last reply
        0
        • A Offline
          A Offline
          ASSeeger
          wrote on last edited by
          #4

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

          p3c0P 1 Reply Last reply
          0
          • A ASSeeger

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

            p3c0P Offline
            p3c0P Offline
            p3c0
            Moderators
            wrote on last edited by
            #5

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

            157

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved