Among new features in Qt 5.7 for QtWebEngine there is printing to PDF. There are several samples which are available for QtWebEngine you can start from this folder in your Qt installation:
c:\Qt\Qt5.7.0\Examples\Qt-5.7\qtwebengine\webenginewidgets\
We will look at demobrowser sample. Let's start from BrowserMainWindow::slotFilePrintToPDF(). Unfortunately Qt still has problem with QPrintDialog not working on Windows described here:
https://bugreports.qt.io/browse/QTBUG-28822
http://www.qtcentre.org/threads/56992-Printer-problem
So for sample purposes we will remove QPrintDialog and just hardcode filename:
void BrowserMainWindow::slotFilePrintToPDF() { #ifndef QT_NO_PRINTER if (!currentTab()) return; QPrinter printer; printer.setOutputFileName("test.pdf"); if (printer.outputFileName().isEmpty() || !m_printerOutputFileName.isEmpty()) return; m_printerOutputFileName = printer.outputFileName(); currentTab()->page()->printToPdf(printer.pageLayout(), invoke(this, &BrowserMainWindow::slotHandlePdfPrinted)); #endif // QT_NO_PRINTER } void BrowserMainWindow::slotHandlePdfPrinted(const QByteArray& result) { if (!result.size()) return; QFile file(m_printerOutputFileName); m_printerOutputFileName.clear(); if (!file.open(QFile::WriteOnly)) return; file.write(result.data(), result.size()); file.close(); }
So what was actually added in Qt 5.7?
Two overloads of printToPdf() function for QWebEnginePage are here.
They allow to render page content into a PDF document.
Turkish to me, but it will come in handy, tnahks
ReplyDelete