Qt Quick views 2.0 and PDF printing
-
In fact my issue is very simple, and very complex in the same time. I need to draw the content of a view, which represent a chat and contains a random number of messages, in a PDF file. I included below a screenshot showing my sample application and the kind of PDF export I expect.
An important constraint is that the text must be editable in the PDF file, i.e I should be able to select it and copy it in the clipboard, as well as modify it with an application like e.g Adobe Acrobat.
The interface is drawn with the Qt Quick Controls 2.0. With the Quick Controls 1.0, and as far as I know, there was a way to connect the PDF printer to the painter of the control, and thus print the content of the view inside the PDF without efforts. Unfortunately this feature was removed from the Qt Quick Controls 2.0, and is no longer available, as well as very important features like column and row headers, and several properties, which is a shame. Now the only way to print a PDF is either to grab its content in an image, which is then printed in the PDF, or to write a custom paint function for the item in the c++ code, which may then be shared with the PDF printer. The first solution isn't acceptable for me, because the ability to edit, copy and modify the text is lost, the second is a very hard and painful way to implement, because no info may be get back from the item style delegate declared in the qt quick view.
It's strange for me that Qt provides a so complete system to export to PDF, but misses a so important feature. However I heard that Qt 6 will greatly improve the qt quick views, so can I hope that all these missing functionalities will be added back?
And here are my questions
- I'm developing with Qt 5.14. Is there a simple, friendly and ready-to-use way to print the content of a qt quick view in a PDF file respecting the above mentioned constraints?
- A new Qt 6.0 version is about to be released. Will the PDF support be improved in this new version for the Qt Quick controls?
- If not, is the Qt team considering to improve the PDF support in a next version?
- Where can I find the documentation and samples about qt quick (controls 2.0) and PDF printing, showing among others how to achieve multiple pages management, page splitting, headers and footers, ...?
-
@jeanmilost said in Qt Quick views 2.0 and PDF printing:
It's strange for me that Qt provides a so complete system to export to PDF, but misses a so important feature. However I heard that Qt 6 will greatly improve the qt quick views, so can I hope that all these missing functionalities will be added back?
And here are my questions
- I'm developing with Qt 5.14. Is there a simple, friendly and ready-to-use way to print the content of a qt quick view in a PDF file respecting the above mentioned constraints?
Unfortunately not, QPdfWriter will help with the generation of the PDF; but you will need to render it yourself using QPainter in that case so it is not going to be straight forward. One other option is to go via QTextDocument which has a convenience print function too which can take a QPdfWriter.
- A new Qt 6.0 version is about to be released. Will the PDF support be improved in this new version for the Qt Quick controls?
There are no plans for anything in this regard at all I am afraid.
- If not, is the Qt team considering to improve the PDF support in a next version?
Not that I am aware of, though there may be some things coming, but in terms of writing PDFs I don't think there is anything planned, but you can always check JIRA to see if there is anything.
- Where can I find the documentation and samples about qt quick (controls 2.0) and PDF printing, showing among others how to achieve multiple pages management, page splitting, headers and footers, ...?
There is no examples or documentation specifically for this, basically you will need to do it as part of the PDF generation yourself, calling newPage() when necessary and generating the headers/footers accordingly. You might find something out there if you search for Qt + PDF + header for instance.
-
Hi.
Recently, I developed an application which generates a report in PDF format. At first glance, Qt's PDF support is disappointing :) and it is ;)
But you can always use QtWebEngine (on desktop, I mean). Just create an html page with proper css and js, needed for beautifying and styling, setting page-break, margin etc. in css, and then Chromium will do the rest :)
What you need to do is to create something similar to your application UI in html, but without interaction. You can even show that html page to the user before printing, just use@media
in your css for better hendling of styles.
Here's the routine:- create a bunch of html, css and js files that you need, and put them in resources.
- when you want to generate that html, copy the whole folder in some temp folder. (that's because you can use relative addresses in files) [*]
- fill placeholders which you already put in html file with your data.
- optionally show your html to the user, or directly generate a PDF from that html (see html2pdf command line Qt example).
- delete the whole folder from that temp folder
That's it :)
[*]: copied files from resources are read-only (at least in linux), you need to set write permission before changing them.
This way, your text will be editable, too.
You can also use some qt report library out there, BUT they do not produce beautiful reports actually :D
-
Thank you for the answers, they provide very valuable info. So there is no real way to have a generic PDF printer, and each developer have to write its own, by writing its own Paint() function. That was that I though, unfortunately.
Unfortunately, because writing my own drawing function brings another issue in my case: I cannot get back the content of the delegate property encapsulated inside my TableView object, in my qml interface file from the c++ code. More exactly, I can, in the best situation, retrieve a part of these info, by getting back the items already drawn on the user interface, from the TableView cache. But as not all the items may be available in this case, I may miss important info in various scenarios, e.g.for still not painted items. Or I may also hardcode the values to use during the PDF export inside the c++ code to reflect what was did in the qml file.
These solutions may resolve my issue in a certain manner, but are weak in terms of generating good and reusable code, especially when designers are intended to often modify the qml interface during the development cycles.
I perhaps will explore the QTextDocument object, and the way it handles the PDF printing. However I don't know if I really can use this component to replace my qml TableView object, as its organization was exactly what I needed to show my data to the user. I will also take a look in the QtWebEngine solution, but this may work only if the text remains editable in the generated PDF, as I said in my original post above.