Browse PDFs in a Qt Widgets application
Some months ago Shawn Rutledge blogged about the new QtPdf module, a Qt wrapper around the PDFium library, which allows you to render PDF documents to QImages. Since that blog post we have invested some more work into the module to make it more useful in your day-to-day projects.
One long-standing bug report against Qt is QTBUG-28886, which is about adding a widget to Qt that can easily render a PDF document. That does not only involve rendering of single pages, but also to provide ways to navigate through the document, so say hello to…
QPdfView
QPdfView is a widget that renders a PDF document that has been loaded through QPdfDocument. It offers a single page and a continuous multi page mode and the user can choose between fit-to-width, fit-in-view or a custom zoom level.
To avoid the UI getting blocked while the PDF document is rendered, the rendering of the single pages is off-loaded to a worker thread.
Getting the code
To test the new features, just clone the QtPdf repository, install it and compile the examples like this:
git clone git://code.qt.io/qt-labs/qtpdf cd qtpdf git submodule update --init --recursive qmake make cd examples/pdf/pdfviewer qmake make ./pdfviewer
Usage
Using QPdfView is quite simple – loading a PDF document and showing it on screen can be done in a couple of lines now:
#include <QPdfDocument> #include <QPdfView> QPdfDocument *document = new QPdfDocument; document->load("document.pdf"); QPdfView *view = new QPdfView; view->setDocument(document); view->show();
If you want to see a more extensive example, have a look at the examples/pdf/pdfviewer directory in the QtPdf module.
Next steps
At the moment the PDFium version which is shipped with QtPdf is rather outdated, and therefore does not include the latest security fixes from upstream. The blocking factor from upgrading it is PDFium’s build system, which has changed from gyp to gn; the build system integration inside QtPdf hasn’t followed up yet.
Thankfully, Michal Klocek from The Qt Company provided the first patches that allow us to implement a proper integration between qmake and gn, so that compiling the QtPdf module with an up-to-date PDFium version should be much easier in the future.
On the UI side there are many things to do as well, here just a short selection from the top of my head:
- extend navigation possibilities (e.g. follow links, navigate to sections)
- add custom QtQuick item to render PDF pages (patches in progress already)
- extend and improve documentation.
Nonetheless, I hope this module will be useful to the many users who are looking for an easy solution to render PDF documents in their Qt applications!
Populating a Combobox from Qt Designer
Using Clang-Format to Ensure Style Guidelines
Un-sorting Headers in QTableView
Using GammaRay to Find the Class of an UI Element
Understanding Contents Margins in Qt Layouts
Starting the Application in GammaRay from Qt Creator
Communicating between a View/Delegate and a Model
Visualizing the Model Stack in GammaRay
Traversing Proxy Models to Get to the Source Model at the Bottom
Changing the Font to Jetbrains Mono in Qt Creator
Using Custom Types with Model/View
Top 7 Shortcuts in Qt Creator
Avoiding QVariant::fromValue around your Own Types
Document Templates in Qt Creator - Part 1
Document Templates in Qt Creator - Part 2
Document Templates in Qt Creator - Part 3
Document Templates in Qt Creator - Part 4
Adapting Column Content to Size
Converting Enums to and from Strings
Qt Designer Plugins (Part 1) - Widgets Promotion
Qt Designer Plugins (Part 2) - Initializing Classes when Using Qt Designer
About KDAB
KDAB is a consulting company offering a wide variety of expert services in Qt, C++ and 3D/OpenGL and providing training courses in:
KDAB believes that it is critical for our business to contribute to the Qt framework and C++ thinking, to keep pushing these technologies forward to ensure they remain competitive.
Great news! Thanks Tobias (and everyone who worked on this).
Just tip for someone who met issue (error during building) like below:
../3rdparty/pdfium/third_party/freetype/src/base/ftbase.c:19:10: fatal error: ft2build.h: No such file or directory
#include
This happens only when you use python3. If your main python is python2 you should be able to build QtPdf.
For all who use python3 workaround is making link to python2 (just temporary relink /usr/bin/python to python2).
Sad is that, I reported this issue over one year ago (January 2017) and nobody fixed this yet :(.
Check: http://blog.qt.io/blog/2017/01/30/new-qtpdf-qtlabs-module/
Hej Peter,
thank you very much for the hint, I haven’t seen your comment on Shawns blog post, so I missed the fact that the current build system does not work with Python 3. Anyway, since the next step will be to update the build system to use gn (to be compatible with upstream PDFium), this Python script will be replaced and therefore the problem hopefully be solved soon.
A little bit late, but for those who are trying to build using python3,
Modify qtpdf/src/3rdparty/gyp2pri.py:121 to
for key, value in variables.items():
then you can build pdf and pdfwidget usin python3
Now I run make install, but qmake does not recognize module pdf.
¿What am I doing wrong?
Isn’t better just to use this library “poppler” (exists poppler-qt5 which is Qt5 bindings) instead of build qtpdf?
Hej Peter,
the Poppler library is distributed under the GPL license, so it cannot (easily) be used in proprietary projects.
Furthermore the PDFium library is actively maintained by Google (since it is used by Chromium to render PDFs) and for graphic intensive documents it renders the pages much faster than Poppler.
Whats wrong about popler or whats the advantage here?
Please see my response to Peter’s question
Is there any possibility of something like a QPDFGraphicsItem that could be used in a QGraphicsView rather than requiring a dedicated view widget for just this type of object? Or does the underlying PDF library make it impractical to provide that kind of deeper Qt integration beyond a widget to view what the library spits out?
Hej Will,
the QPdfDocument class allows you to render a specific page (or part of a page) into a QImage, so it shouldn’t be too difficult to reimplement a QGraphicsItem subclass which utilizes this function to display parts of a PDF document. However, at the moment the QtPdf module doesn’t provide a convenience class for it, but upstream contributions are always welcome 🙂
On openSuse I get the following errors:
cd lib/ && ( test -e Makefile || /usr/bin/qmake-qt5 -o Makefile /home/PACKAGES/qt/5.9.2/src/qtpdf/src/lib/lib.pro ) && make -f Makefile
Traceback (most recent call last):
File “/home/PACKAGES/qt/5.9.2/src/qtpdf/src/3rdparty/gyp2pri.py”, line 162, in
gyp = Gyp(config.input, variables)
File “/home/PACKAGES/qt/5.9.2/src/qtpdf/src/3rdparty/gyp2pri.py”, line 7, in __init__
with open(fileName, “r”) as f:
IOError: [Errno 2] No such file or directory: ‘/home/PACKAGES/qt/5.9.2/src/qtpdf/src/3rdparty/pdfium/pdfium.gyp’
Traceback (most recent call last):
File “/home/PACKAGES/qt/5.9.2/src/qtpdf/src/3rdparty/gyp2pri.py”, line 162, in
gyp = Gyp(config.input, variables)
File “/home/PACKAGES/qt/5.9.2/src/qtpdf/src/3rdparty/gyp2pri.py”, line 7, in __init__
with open(fileName, “r”) as f:
IOError: [Errno 2] No such file or directory: ‘/home/PACKAGES/qt/5.9.2/src/qtpdf/src/3rdparty/pdfium/third_party/third_party.gyp’
Cannot read /home/PACKAGES/qt/5.9.2/src/qtpdf/src/lib/pdfium.pri: No such file or directory
Cannot read /home/PACKAGES/qt/5.9.2/src/qtpdf/src/lib/freetype.pri: No such file or directory
and later also
jsbridge.cpp:37:50: fatal error: fpdfsdk/javascript/JS_Runtime_Stub.cpp: No such file or directory
Hej Martin,
have you initialized the submodules correctly (e.g. called ‘git submodule update –init –recursive’)? Looks like your ‘qtpdf/src/3rdparty/pdfium/’ directory is empty, which indicates that the pdfium submodule has not been cloned.
Stupid me … Sorry for the noise. I overlooked this step.
Sorry for the noise, I have overlooked this step.
Works now. Thanks.
Thanks Tobias, It’s been a long time since my original request of having a PDF viewer (QTBUG-28886) and I am glad to see it starting to happen. As soon as the new build system is in place, I plan on dedicating time on adding form editing to your QtPDF module wrapper to take advantage of these features in PDFium.
Can you also use QPdfDocument to create a PDF by code and save it? QPdfWriter unfortunately has one severe restriction – no bookmarks and links.
Hej Robert,
QPdfDocument only provides API do render PDF documents, but not to assemble them.
Links are ‘kind of’ supported by QPdfWriter, the QPdfEngine class has a member drawHyperlink(QRect, QUrl), so when you use a QPainter that draws on a QPrinter in PDF output mode and include the private header qpdf_p.h, you could do
dynamic_cast<QPdfEngine*>(painter.paintEngine())->drawHyperlink(...)
. So it is private API which can be accessed on your own risk. 😉That’s too bad. Thanks for the hint, though!
Hi, QPdfView is great and easy to implement.
I quick got an lightweight pdf viewer.
I also implemented a search toolbox to search the pdf for an specific text string.
But now I want to highlight the text where I found the string. Is there a method to highlight some text?
Hej,
unfortunately there is no public API available yet to highlight the text.
Is it possible to highlight text in a QPdfView?