Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How can I print a QWebPage to a QPdfWriter?



  • Problem: I can't create multi-paged PDF documents from QWebFrame with QPdfWriter because there is no ::print() method that takes a QPagedDevice or QPdfWriter (or at least I think that's the reason)

    (context: this is for: https://github.com/ariya/phantomjs/issues/14268)

    This method is from the webpage module from PhantomJS.

    bool WebPage::renderPdf(QPdfWriter& pdfWriter)
    {
        // code removed for brevity
    
        QPainter painter(&pdfWriter);
        //m_mainFrame->render(&painter);
        QRect pageRect = pdfWriter.pageLayout().pageSize().rectPoints();
        // ===============================
        // How do I include QtPrintContext
        // ===============================
        QtPrintContext printContext(&painter, pageRect, m_mainFrame->d);
    
        for (int page = 0; page < printContext.pageCount(); page++) {
            printContext.spoolPage(page, pageRect.width());
            pdfWriter.newPage();
        }
        painter.end();
    
        return true;
    }
    

    Question: How can I #include QtPrintContext in the module above? Will this even fix the problem?

    Thanks!


Log in to reply