Important: Please read the Qt Code of Conduct -

Need to partially convert HTML to PDF document

  • Hi All,

    i am using qtwebkit 5.5 for convert HTML to PDF in that i want to partially convert HTML page to PDF by specifying HTML elements (div,Table,chart,image etc) instead of convert hole page for example i am having HTML page with more then one <div> content from that i want to convert any one of the div content as PDF by specifying its ID . please share your views .

    Thanks in advance.

  • Moderators

    The simplest solution would be to simply render the web widget (using QWidget::render() ) and print the resulting image. There is a lot of material about printing to PDF using Qt available on the web so i am sure you will find it yourself.

    QtWebKit: QWebView

    using QtWebKit (deprecated):

    QWebElement elem = myWebView->page()->mainFrame()->findFirstElement("#id");
    if( !elem.isNull() )
         QPixmap pix( elem.geometry().size() );
         QPainter p( &pix );
         elem.render( &p );
        // print pixmap to pdf

    using QtWebEngine:

    QWebEngineView* myWebView = new QWebEngineView;
    myWebView->load( url );
    // when loaded:
    QPixmap pix( myWebView->size() );
    myWebView->render( &pix );
    //print pixmap to pdf

    honestly i don't know if there is not a faster/better way using QtWebEngine though

  • @raven-worx

    Thanks for your replay

    is possible to convert particular HTML element to PDF without using pixmap because PDF text should be Selectable .

  • Moderators

    Maybe you can extract a QWebElement and insert it into a separate QWebView and use print(). But i think it may not look the same as in the origin QWebView.

    Also maybe there is a possible JavaScript way?