How to use poppler lib in Qt 5
Poppler is a C++ library that is useful to load and acess PDF file into your program.
Here is how you can use it in Qt 5.
download from poppler.freedesktop.org
By the time of writing this article the link is https://poppler.freedesktop.org/poppler-0.83.0.tar.xz
build with your Qt installation path
1
2cmake .. -DCMAKE_PREFIX_PATH=~/Qt5.12.3/5.12.3/clang_64/lib/cmake/
make
install the poppler libs
1
make install
add poppler to your Qt project file
1
2INCLUDEPATH += /usr/local/include/poppler/qt5
LIBS += -L/usr/local/lib/ -lpoppler-qt5now you can
#include
it in your C++ file!1
#include <poppler-qt5.h>
try this lines copied from example from poppler site.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33Poppler::Document* document = Poppler::Document::load(fileName);
if (!document || document->isLocked()) {
// ... error message ....
delete document;
return;
}
// Paranoid safety check
if (document == 0) {
// ... error message ...
return;
}
// Access page of the PDF file
Poppler::Page* pdfPage = document->page(0); // Document starts at page 0
if (pdfPage == 0) {
// ... error message ...
return;
}
// Generate a QImage of the rendered page
QImage image = pdfPage->renderToImage();
if (image.isNull()) {
// ... error message ...
return;
}
// ... use image ...
// this is how usually we do it with Qt
ui->imageLabel->setPixmap(QPixmap::fromImage(image));
// after the usage, the page must be deleted
delete pdfPage;
delete document;
I hope this useful for those who might get stuck wondering how to do it.
> Bye!