Search code examples
c++qtqt5qtwebengineqwebenginepage

Qt WebEngine incorrect page margins when printing


I implement the ability to print reports in my project. Reports are presented as HTML content. There is an instance of QPrinter with custom fields:

printer = new QPrinter(QPrinter::ScreenResolution);
qreal topMargin = 15;
qreal bottomMargin = 15;
qreal leftMargin = 20;
qreal rightMargin = 15;
QPrinter::Unit units = QPrinter::Millimeter;
printer->setPageMargins(leftMargin,topMargin,rightMargin,bottomMargin,units);

When printing in PDF, everything is fine

view->printToPdf([=] (QByteArray bd) {
            //Запись файла
        }, printer->pageLayout());

But when printing with the "print" function, the fields are set incorrectly:

QWebEnginePage *page = new QWebEnginePage;
    page->setHtml(currentForPrint);

    connect(page, &QWebEnginePage::loadFinished, [page, printer] (bool ok) {
        if (!ok) {
            qDebug() << "error"; return;
        }
        page->print(printer, [=] (bool ok) {
            if (ok)
                qDebug() << "success";
            else
                qDebug() << "error 2";
        });
    });

image

Qt Version - 5.9.3.


Solution

  • This is a bug in WebEngine. I have reported it and will fix it as soon as it's possible. As a workaround you could enable full page printing in your QPrinter:

    printer->setFullPage(true);
    

    This works for me.