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.

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

  2. build with your Qt installation path

    1
    2
    cmake .. -DCMAKE_PREFIX_PATH=~/Qt5.12.3/5.12.3/clang_64/lib/cmake/
    make
  1. install the poppler libs

    1
    make install
  1. add poppler to your Qt project file

    1
    2
    INCLUDEPATH += /usr/local/include/poppler/qt5
    LIBS += -L/usr/local/lib/ -lpoppler-qt5
  2. now you can #include it in your C++ file!

    1
    #include <poppler-qt5.h>
  3. 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
    33
    Poppler::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!