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

    @Shidharth
    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
    QtWebEngine:

    using QtWebKit (deprecated):

    QWebElement elem = myWebView->page()->mainFrame()->findFirstElement("#id");
    if( !elem.isNull() )
    {
         QPixmap pix( elem.geometry().size() );
         QPainter p( &pix );
         elem.render( &p );
         p.end();
    
        // 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

    @Shidharth
    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?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.