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
QtWS25 Last Chance

[ Solved] Print QWebView to PDF

Scheduled Pinned Locked Moved General and Desktop
qwebviewpdf
5 Posts 2 Posters 3.4k Views
  • 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 6 Jun 2015, 07:13 last edited by ASSeeger 6 Jul 2015, 10:15
    #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!

    P 1 Reply Last reply 6 Jun 2015, 08:23
    0
    • A ASSeeger
      6 Jun 2015, 07:13

      [ 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!

      P Offline
      P Offline
      p3c0
      Moderators
      wrote on 6 Jun 2015, 08:23 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 6 Jun 2015, 15:02 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 7 Jun 2015, 10:13 last edited by
          #4

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

          P 1 Reply Last reply 7 Jun 2015, 10:30
          0
          • A ASSeeger
            7 Jun 2015, 10:13

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

            P Offline
            P Offline
            p3c0
            Moderators
            wrote on 7 Jun 2015, 10:30 last edited by
            #5

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

            157

            1 Reply Last reply
            1

            1/5

            6 Jun 2015, 07:13

            • Login

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