Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QtWebEngine
  4. QWebEnginePage::printToPdf creates empy page
Qt 6.11 is out! See what's new in the release blog

QWebEnginePage::printToPdf creates empy page

Scheduled Pinned Locked Moved Solved QtWebEngine
4 Posts 3 Posters 865 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.
  • T Offline
    T Offline
    Tarae
    wrote on last edited by
    #1

    My Html2Pdf program:

    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
    
        // arguments
        QString inputPath = argv[1];
        QString outputPath = argv[2];
    
        // read input file
        QFile inputFile(inputPath);
        inputFile.open(QIODevice::ReadOnly | QIODevice::Text);
        QTextStream in(&inputFile);
        QString html = in.readAll();
    
        // get input file directory
        QFileInfo inputFileInfo(inputFile);
        QDir inputFileDir = inputFileInfo.dir();
    
        // prepare page
        QWebEnginePage page;
        QObject::connect(&page, &QWebEnginePage::pdfPrintingFinished, []{exit(0);});
        page.setHtml(html, inputFileDir.path());
    
        //print page
        page.printToPdf(outputPath);
    
        return app.exec();
    }
    

    And it doesn't work. It produces pdf file containing one empy page. What do I do wrong?

    jsulmJ JonBJ 2 Replies Last reply
    0
    • T Tarae

      My Html2Pdf program:

      int main(int argc, char *argv[])
      {
          QApplication app(argc, argv);
      
          // arguments
          QString inputPath = argv[1];
          QString outputPath = argv[2];
      
          // read input file
          QFile inputFile(inputPath);
          inputFile.open(QIODevice::ReadOnly | QIODevice::Text);
          QTextStream in(&inputFile);
          QString html = in.readAll();
      
          // get input file directory
          QFileInfo inputFileInfo(inputFile);
          QDir inputFileDir = inputFileInfo.dir();
      
          // prepare page
          QWebEnginePage page;
          QObject::connect(&page, &QWebEnginePage::pdfPrintingFinished, []{exit(0);});
          page.setHtml(html, inputFileDir.path());
      
          //print page
          page.printToPdf(outputPath);
      
          return app.exec();
      }
      

      And it doesn't work. It produces pdf file containing one empy page. What do I do wrong?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @Tarae My guess is: you need to wait until the HTML page is loaded.
      https://doc.qt.io/qt-5/qwebenginepage.html#loadFinished

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • T Tarae

        My Html2Pdf program:

        int main(int argc, char *argv[])
        {
            QApplication app(argc, argv);
        
            // arguments
            QString inputPath = argv[1];
            QString outputPath = argv[2];
        
            // read input file
            QFile inputFile(inputPath);
            inputFile.open(QIODevice::ReadOnly | QIODevice::Text);
            QTextStream in(&inputFile);
            QString html = in.readAll();
        
            // get input file directory
            QFileInfo inputFileInfo(inputFile);
            QDir inputFileDir = inputFileInfo.dir();
        
            // prepare page
            QWebEnginePage page;
            QObject::connect(&page, &QWebEnginePage::pdfPrintingFinished, []{exit(0);});
            page.setHtml(html, inputFileDir.path());
        
            //print page
            page.printToPdf(outputPath);
        
            return app.exec();
        }
        

        And it doesn't work. It produces pdf file containing one empy page. What do I do wrong?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @Tarae
        I know from experience that what @jsulm says is indeed the case! QWebEnginePage loads asynchronously, if you set off a page print before that's completed you get how the page is now --- which is empty :)

        1 Reply Last reply
        1
        • T Offline
          T Offline
          Tarae
          wrote on last edited by Tarae
          #4

          @jsulm @JonB Thank you! This works:

          QWebEnginePage page;
          Object::connect(&page, &QWebEnginePage::pdfPrintingFinished, []{exit(0);});
          QObject::connect(&page, &QWebEnginePage::loadFinished, [&page, &outputPath](bool ok){if (ok) page.printToPdf(outputPath);});
          page.setHtml(html, inputFileDir.path());
          
          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