Handling PDF - QT Wiki
Handling PDF - QT Wiki
Handling PDF - QT Wiki
Handling PDF
From Qt Wiki
Jump to: navigation, search
En
Ar
Bg
De
El
Es
Fa
Fi
Fr
Hi
Hu
It
Ja
Kn
Ko
Ms
Nl
Pl
Pt
Ru
Sq
Th
Tr
Uk
Zh
The most basic (but not necessarily simplest) way of creating PDF documents with QPrinter is by manually
painting the document's content with Qt's Arthur paint system (http://doc.qt.io/qt-5/qtdesigner-arthurplugin-exa
mple.html#).
Just pass the QPrinter object as a reference to the constructor of QPainter (http://doc.qt.io/qt-5/pai
ntsystem.html.html#) (or, alternatively, to QPainter::begin() (http://doc.qt.io/qt-5/qpainter.html#begin) for an
already existing QPainter) and then perform any painting operations with that QPainter instance like you
usually would (with intermittent calls to QPrinter::newPage() (http://doc.qt.io/qt-5/qprinter.html#newPage)
whenever you want to move on to the next PDF page).
Scribe
For a more high-level API for creating structured rich-text documents, use Qt's Scribe framework (see
Handling Document Formats), which is based on a cursor interface that mimics the behavior of a cursor in a
text editor. You can export the whole document with QTextDocument::print() (http://doc.qt.io/qt-5/qtextdocume
nt.html#print), or a part of it with QTextEdit::print() (http://doc.qt.io/qt-5/qtextedit.html#print) (see Exporting a
document to PDF for more details).
Graphics View
Qt's Graphics View framework (http://doc.qt.io/qt-5/graphicsview.html#) can be a more suitable alternative for
creating PDF documents with content that is mainly based on arbitrarily positioned and transformed 2D
graphical items rather than continuous flowed rich text.
To export the content of a graphics scene or view (or a
https://wiki.qt.io/Handling_PDF 1/5
7/9/2021 Handling PDF - Qt Wiki
part of it) to PDF, you need to manually initialize a QPainter configured to paint on a PDF-creating QPrinter (as
described above), and pass it to QGraphicsScene::render() (http://doc.qt.io/qt-5/qgraphicsscene.html#render) or
QGraphicsView::render() (http://doc.qt.io/qt-5/qgraphicsview.html#render).
If you need more control over the output when creating PDF documents, or you need to parse existing PDF
documents (anything from extracting specific information to assembling a full in-memory document object
tree) and maybe even modify their structure or content before writing them back to disk, refer to third-party
PDF reading/writing libraries:
Win,
Hummus (http://pdfhummus.com/) Apache 2.0
C++ yes yes yes Mac,
[pdfhummus.com] [permissive]
Linux
Win, LGPL
PoDoFo (http://podofo.sourceforge.net)
C++ yes yes yes Mac, [weak
[podofo.sourceforge.net]
Linux copyleft]
If all else fails, there is always the option of using an existing tool to automatically convert between PDF files
and a more manageable format, and let your Qt application read/write that format instead. The conversion tool
could be bundled with your application or specified as a prerequisite, and controlled via QProcess (http://doc.q
t.io/qt-5/qprocess.html). Some possibilities are:
… to
executable names .pdf to: platforms license
.pdf
Rendering
Using QtPDF
The QtPDF module is a wrapper around PDFium which supports rendering, navigating pages, bookmarks,
links, document metadata, search, text selection and copying to the clipboard. It includes an image plugin so
that most image-viewing applications can view PDF files too (however most of those do not have multi-frame
image support, so they will only render the first page). At this point, more of the feature set is complete for Qt
Quick applications than for widget-based ones; but both are possible. It was introduced as a Qt Labs module in
this blog post (http://blog.qt.io/blog/2017/01/30/new-qtpdf-qtlabs-module/). Since then, Qt Quick support has
been added; and now the source code lives in the qtwebengine repository. It is supported on all platforms that
support Qt WebEngine, plus iOS. You can build it using these instructions. Binary builds are also provided via
Marketplace: https://marketplace.qt.io/products/qtpdf It's available under GPL, LGPL and commercial licenses.
https://wiki.qt.io/Handling_PDF 2/5
7/9/2021 Handling PDF - Qt Wiki
For rendering pages or elements from existing PDF documents to image files or in-memory pixmaps (useful
e.g. for thumbnail generation or implementing custom viewers), third-party libraries can be used:
can
API output to platforms license
render
RGBA
Win, Mac, GPL v3+ [strong
muPDF (http://mupdf.com) [mupdf.com] C pages byte
Linux, … copyleft]; or commercial
array
executable can
output to platforms license
names render
Interactive Viewing
Using QtPDF
The QtPDF module includes example PDF viewers which can be adapted and incorporated into Qt Quick or
widget-based applications.
If your application merely needs to let the user view/read certain PDF documents on demand, displaying them
within the UI of the application itself might not be necessary, and delegating the task to an existing viewer
application can be a viable option.
Many users have already chosen and installed a stand-alone PDF viewer according to their personal
preferences, so simply letting the operating system open the PDF file with whatever it considers the default
viewer for such files, might be the easiest (and potentially most user-friendly) choice.
To do so, simply pass the
PDF file's URL to QDesktopServices::openUrl (http://doc.qt.io/qt-4.8/qdesktopservices.html#openUrl). If
you're downloading the file from the Internet, store it on disk using QTemporaryFile (http://doc.qt.io/qt-5/qtem
poraryfile.html) first, since not all viewers can handle remote URLs.
https://wiki.qt.io/Handling_PDF 3/5
7/9/2021 Handling PDF - Qt Wiki
If you are exclusively targeting the Windows platform, you can embed an existing ActiveX component for
viewing PDFs in your Qt applications by instantiating it as a QAxWidget (http://doc.qt.io/qt-5/qaxwidget.html)
(see Qt's ActiveX Framework (http://doc.qt.io/qt-4.8/activeqt.html)).
In the case of the Adobe Reader control, opening a PDF file is done with:
dynamicCall("LoadFile(const
QString)", pathToFile)
A more cross-platform technology for embedding reusable components is the NPAPI (http://en.wikipedia.org/w
iki/NPAPI) [en.wikipedia.org] browser plugin architecture- which Qt's WebKit-based browser framework
happens to support (http://doc.qt.io/qt-5/qtwebkitwidgets-index.html). You'll need to set up a simple HTML
page containing appropriate embed>…</embed> tags, and let a QWebView (http://doc.qt.io/qt-5/qwebview.ht
ml) display it (with QWebSettings::PluginsEnabled (http://doc.qt.io/qt-4.8/qwebsettings.html#WebAttribute-en
um).
The following applications provide a reusable NPAPI plugin for viewing PDF:
plugin
platforms license
name
Win, Mac,
Adobe Reader (http://get.adobe.c freeware (for commercial redistribution see here (http://www.adob
nppdf (Linux)¹,
om/reader/) [get.adobe.com] e.com/products/reader/distribution.html) [adobe.com])
…
¹While in theory it should work on all Desktop platforms, application developers have "reported problems":/forums/viewthread/14055
in trying to get it to work with Qt Webkit on Linux.
As an alternative to using QWebView for running the plugin, it is possible to use a third-party solution that
allows embedding NPAPI plugins in a Qt application without the overhead of a full web browser instance:
has special
component
convenience API platforms license
type
for
See Also
Handling Document Formats
https://wiki.qt.io/Handling_PDF 4/5
7/9/2021 Handling PDF - Qt Wiki
Handling HTML
Handling Microsoft Excel file format
https://wiki.qt.io/Handling_PDF 5/5