How to open and display a PDF file using Qt/C++? [closed]
Asked Answered
D

6

9

I am trying to open and read a PDF file using Qt, but there is no specific way to do that.

Dagall answered 7/6, 2011 at 7:30 Comment(1)
At some point this may get added to Qt WebEngine - see bugreports.qt.io/browse/QTBUG-50556Monty
O
5

Qt itself does not include PDF reading/rendering functionality as far as I know. You might want to have a look at libpoppler which has Qt bindings.

Orangeman answered 7/6, 2011 at 7:46 Comment(1)
poppler use Xpdf, under GPL2 license. I love GPL but be care to do not use it in proprietary software.Roesch
S
3

Qt Wiki — Handling PDF discusses various available options for working with PDF documents in a Qt application. The page does not exactly show how to "open and display an existing PDF document", but it can help you deduce something useful out of all that is explained there.

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:

API parsing modifying creating platforms license
poppler-qt4 [freedesktop.org] C+/Qt yes ? ? Win, Mac?, Linux, … GPL v2+ [strong copyleft]
Hummus [pdfhummus.com] C++ yes yes yes Win, Mac, Linux Apache 2.0 [permissive]
PoDoFo [podofo.sourceforge.net] C++ yes yes yes Win, Mac, Linux LGPL [weak 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. Some possibilities are:

executable names .pdf to: … to .pdf platforms license
poppler-utils [freedesktop.org] pdftotext, pdftocairo, pdftohtml .txt .svg .html … - Win, Mac?, Linux, …
Inkscape [inkscape.org] inkscape .svg … .svg … Win, Mac, Linux, …
Sike answered 19/2, 2014 at 13:33 Comment(0)
S
2

As of Qt 5.10

Check out the QPdfDocument Class. This class can open a PDF and you can use the render function to render a page to an image. I use the QQuickPaintedItem to then "draw" this image, but I am sure there are more ways to handle the QImage output.

Prior to Qt 5.10

I used libpoppler to do a VERY similar process.

#include <poppler/qt5/poppler-qt5.h>

Use the Poppler::Document Class to load and handle the entire PDF document and look at the Poppler::Page::renderToImage function to output the page as a QImage.

Sennet answered 17/12, 2020 at 15:15 Comment(0)
L
0

It is not supported in the Qt API.

You can code it, I suggest you have a look at this Qt application which uses Ghostscript.

Lerner answered 7/6, 2011 at 7:45 Comment(0)
D
0

Open the PDF file using QProcess.

Dad answered 21/12, 2011 at 0:14 Comment(0)
M
0

You may want to use okular for pdf proccessing.

Marinetti answered 17/3, 2019 at 10:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.