I am trying to somehow enable printing through printJS library with QWebEngineView. Everything is working perfectly when opening it through the web browser, and I can't make it work with QWebEngineView.
I have tried catching a signal QWebEnginePage::printRequested and it works only with a simple window.print() JS call. I have also already tried setting all the flags to true on the QWebEngineSettings without success. My code (mostly from tutorial as I was trying to make it as simple as possible) is:
main.cpp:
#include <QApplication>
#include <QWebEngineView>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWebEngineView view;
view.setUrl(QUrl("http://localhost:8080/"));
view.resize(1024, 750);
view.show();
//PrintHandler handler;
//handler.setPage(view.page());
return a.exec();
}
PrintHandler is taken from Qt5 tutorial 'PrintMe example' (it also didn't work to catch printJS signal, therefore I left it commented)
My HTML:
<html lang="en">
<head>
<meta charset="utf-8">
<title>PrintMe</title>
<script src="print.min.js"></script>
<link rel="stylesheet" href="print.min.css">
<script>
function printNow() {
printJS('http://localhost:8080/dummy.pdf');
}
</script>
</head>
<body>
<form class="form">
<div class="header">
<h2>Click the button to print</h2>
<button type="button" onclick="printNow()">Print Now</button>
</form>
</body>
</html>
And printJS library is downloaded from official source.
Is there a way for QWebEngineView to react on printJS call?
I found a solution/workaround.
To have a signal in QT from printJS call it is enough to connect signal from
QWebEngineProfile::downloadRequested. QWebEngineProfile can be got from(QWebEngineProfile::page())::profile(), I am doing it like this:The file is downloaded in downloads/file.pdf (default) - of course, have to wait for
QWebEngineDownloadItem::finishedsignal.It is possible then to open the file in newly-created QWebEnginePage, set URL to a downloaded file and print this page. All of this can be done in background - without showing the page. Of course, some type of state machine is required to not print not loaded pages.