Handling PDF - QT Wiki

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

7/9/2021 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

This page discusses various available options for


working with Portable Document Format (PDF) (ht
tp://en.wikipedia.org/wiki/Portable_Document_For Contents
mat) [en.wikipedia.org] documents in your Qt
application. Please also read the general 1 Reading / Writing
considerations outlined on the Handling Document 1.1 Using QPrinter
Formats page. 1.2 Using third-party libraries
1.3 Using batch conversion tools
2 Rendering
Reading / Writing 2.1 Using QtPDF
2.2 Using third-party libraries/tools
Using QPrinter 3 Interactive Viewing
3.1 Using QtPDF
For creating PDF documents from scratch, you can 3.2 Calling an external viewer application
use Qt's built-in print support which also allows 3.3 Using a third-party Qt widget
"printing" to PDF files. To do so you can set up a 3.4 Embedding a third-party ActiveX control
3.5 Embedding a third-party browser plugin
QPrinter (http://doc.qt.io/qt-5/qprinter.html#)
4 See Also
instance like this: QPrinter
printer(QPrinter::HighResolution);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName("path/to/file.pdf"); Since QPrinter inherits QPaintDevice (http://doc.qt.io/
qt-5/qpaintdevice.html#), anything that supports outputting graphical content to a QPaintDevice (or has
convenience API for printing with QPrinter) can thus be used for generating PDFs:

manual QPainter painting

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).

Using third-party libraries

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:

API parsing modifying creating platforms license

Win, GPL v2+


poppler-qt4 (http://freedesktop.org/wiki/Software/po
C+/Qt yes ? ? Mac?, [strong
ppler) [freedesktop.org]
Linux, … copyleft]

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]

Using batch conversion tools

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

pdftotext, GPL v2+


poppler-utils (http://freedesktop.org/wiki/Softwar .txt .svg Win, Mac?,
pdftocairo, - [strong
e/poppler/) [freedesktop.org] .html … Linux, …
pdftohtml copyleft]

.svg Win, Mac, GPL v2 [strong


Inkscape (http://inkscape.org/) [inkscape.org] inkscape .svg …
… Linux, … copyleft]

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

Using third-party libraries/tools

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

poppler-qt4 (http://freedesktop.org/wiki/Software/ pages, Win, Mac?,


C++/Qt QImage GPL v2 [strong copyleft]
poppler) [freedesktop.org] …? Linux, …

poppler-qt5 (http://freedesktop.org/wiki/Software/ pages, Win, Mac?,


C++/Qt QImage GPL v2 [strong copyleft]
poppler) [freedesktop.org] …? Linux, …

RGBA
Win, Mac, GPL v3+ [strong
muPDF (http://mupdf.com) [mupdf.com] C pages byte
Linux, … copyleft]; or commercial
array

Alternatively, the task can be delegated to existing command-line tools:

executable can
output to platforms license
names render

pdftocairo, pages, Win,


poppler-utils (http://freedesktop.org/wi .png .jpg .svg GPL v2+ [strong
pdftoppm, image Mac?,
ki/Software/poppler) [freedesktop.org] .ppm … copyleft]
pdfimages elements Linux, …

.png, .ppm, Win, GPL v3+ [strong


muPDF (http://mupdf.com)
pdfdraw pages .pgm, .pam, Mac, copyleft]; or
[mupdf.com]
.pbm Linux, … commercial

Interactive Viewing
Using QtPDF

The QtPDF module includes example PDF viewers which can be adapted and incorporated into Qt Quick or
widget-based applications.

Calling an external viewer application

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.

Using a third-party Qt widget

The following widgets provide native PDF viewing for Qt applications:

https://wiki.qt.io/Handling_PDF 3/5
7/9/2021 Handling PDF - Qt Wiki

class name platforms license

XpdfWidget/Qt (http://www.glyphandcog.com/XpdfWidgetQt.html) Win, Mac, Linux,


XpdfWidget commercial
[glyphandcog.com] …

Embedding a third-party ActiveX control

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)).

The following PDF viewers provide such an ActiveX control:

DLL file ActiveX control name platforms license

freeware (for commercial


Adobe Reader (http://get.ado Win,
redistribution see here (http://www.
be.com/reader/) Acropdf.dll AxAcroPDFLib.AxAcroPDF Mac,
adobe.com/products/reader/distribu
[get.adobe.com] Linux, …
tion.html)) [adobe.com])

In the case of the Adobe Reader control, opening a PDF file is done with:
dynamicCall("LoadFile(const
QString)", pathToFile)

Embedding a third-party browser plugin

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

QtitanMultimedia (http://www.devmachines.com/products/qtita Win,


QWidget Adobe Reader, … commercial
nmultimedia-overview.html) [devmachines.com] Linux

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

Retrieved from "https://wiki.qt.io/index.php?title=Handling_PDF&oldid=37043"

This page was last edited on 28 May 2020, at 13:41.

https://wiki.qt.io/Handling_PDF 5/5

You might also like