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. QwebView Qprinter and pdf
Qt 6.11 is out! See what's new in the release blog

QwebView Qprinter and pdf

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 3.2k Views 1 Watching
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    I am trying to create a pdf out of my webview, even for simplest html something like this, printer does not save pdf with full page having blue bg color, only hello world has bg color blue rest of the page is white. I tried every thing i could think of but no good, Looks like I am missing something very small , can you please help.
    <aa>
    <body style=background-color: yellow>
    hello world.
    </body>
    </aa>

    @
    #include <QApplication>
    #include <QtPrintSupport>
    #include <QtWebKitWidgets>
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    QWebView *web = new QWebView();
    //web->settings()->setAttribute(QWebSettings::LocalContentCanAccessFileUrls,true);
    web->load(QUrl(myHtmlString))

          //  QDialog dlg;
           //  dlg.exec&#40;&#41;; little delay so tht webpage gets loaded    
    QPrinter printer;
    //printer.setPrinterName("Print to File (PDF&#41;"&#41;;
    printer.setOutputFormat(QPrinter::PdfFormat&#41;;
    printer.setPrintRange(QPrinter::AllPages&#41;;
    printer.setOrientation(QPrinter::Portrait&#41;;
    

    // printer.setPaperSize(QSize(400,800),QPrinter::DevicePixel);
    printer.setPaperSize(QPrinter::A4);
    printer.setResolution(QPrinter::HighResolution);
    printer.setFullPage(true);
    //printer.setFromTo(0,0);

    printer.setOutputFileName("myPdfFile.pdf");
    

    // web->page()->setViewportSize(QSize(400,600));
    // QWebFrame *frame= web->page()->mainFrame();
    //web->page()->setViewportSize(web->page()->mainFrame()->contentsSize());
    //web->settings()->setAttribute(QWebSettings::PrintElementBackgrounds, true);

    // web->resize(frame->contentsSize());

    //frame->print(&printer);
    web->print(&printer);
    
    web->show();
    return a.exec(&#41;;
    

    }
    @

    1 Reply Last reply
    0

    • Login

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