PDA

View Full Version : Printing to pdf with QWebEnginePage print() function



ce_nort
8th February 2017, 18:52
New with Qt 5.8 is the print() functionality with the QWebEnginePage class - http://doc.qt.io/qt-5/qwebenginepage.html#print . I'm trying to port some old programs that used QWebView to print to pdf to this new approach, and I'm having trouble understanding the second parameter required for the print function. The documentation says it is expecting a FunctorOrLambda resultCallback. I'm new-ish to both Qt and C++, so I have yet to fully get my head around the concept of functors or lambdas. That I can learn, but first I'm just trying to grasp the overall purpose of the second parameter of print and what exactly is expected. Based on the documentation and this (http://blog.elangroup-software.com/2016/11/new-print-option-for-qtwebengine-in-qt.html) blogpost, am I understanding correctly that the print function is looking for some reference to a function that makes sure the printer stays valid until printing is successful? If so, any hints on how I might approach that in the below code structure that I am already working with?


QWebEngineView *view = new QWebEngineView();
view->setHtml(htmlString);

QPrinter pdfPrinter(QPrinter::HighResolution);

view->page()->print(&pdfPrinter, HELP_REQUIRED_HERE);


Anything to help me understand would be greatly appreciated!

anda_skoa
9th February 2017, 08:52
This kind of callback is how the web environment handles the asynchronous nature of operations.

Basically you launch an operation, in this case print, and provide a function that is called when the operation finished, e.g. to provide you with feedback on its success or the result, etc.

In this case the function is to report success or failure, i.e. the function required here needs to have a boolen argument and it will be called with "true" if the printing succeeded.

A lambda is an inline defined piece of code that can be called like function and can take values and reference from the environment of the code where it appears.
A functor is basically the same just with different (older) language features. Lambdas require a C++11 capable build environment.

Independent of that you'll need to make sure that the printer is valid past the call to print, due to the asynchronous nature of the operations.

If you don't need explicit feedback on the operation you could look into the printToPdf method that takes a filename.

Cheers,
_

ce_nort
9th February 2017, 15:12
Thanks for that overview, it was very helpful. I spent sometime yesterday with functors and lambdas, so I think I have a basic grasp on them now. I don't really need explicit feedback on the operation, but I also encountered a lot of trouble when trying to get the functionality I wanted out of the printToPdf method (this was several months ago, so I can't recall the exact issues I encountered). Basically what I'm trying to do is save some html as a PDF and then immediately open the saved pdf so that the user may print it if desired. Maybe I'll revisit printToPdf and see if it's a better solution for my needs. Either way, thanks for the break down!