Search code examples
qtqmlpoppler

How i can use Poppler-QML-plugin in my application?


Hello guys i have found into this repository

https://github.com/dept2/Poppler-QML-plugin

a qml plugin for show a pdf file into a qml file, but i don't know how i can use it someone can help me?

In ubuntu 18.04 version i have found this plugin with command line sudo apt-cache search poppler and i have installed the package but i have the same problem , how i can use it ?

Thanks in advance


Solution

  • There are 2 possible methods:

    1. Compile and install the plugin:

    To install this package I must install the plugin for it first download the project, open a terminal in the project directory and execute the following:

    qmake
    make
    sudo make install
    

    Then in the .qml import the module, the Poppler item provides an imageProvider so you should use an Image as I show below:

    import QtQuick 2.9
    import QtQuick.Window 2.2
    import QtQuick.Layouts 1.11
    import org.docviewer.poppler 1.0 // <--- import
    import QtQuick.Dialogs 1.3
    
    Window {
        id: win
        visible: true
        width: 640
        height: 480
        title: qsTr("Poppler Example")
    
        function urlToPath(urlString) {
            var s
            if (urlString.startsWith("file:///")) {
                var k = urlString.charAt(9) === ':' ? 8 : 7
                s = urlString.substring(k)
            } else {
                s = urlString
            }
            return decodeURIComponent(s);
        }
    
        FileDialog {
            id: fileDialog
            title: "Please choose a file"
            folder: shortcuts.home
            nameFilters: ["PDF files (*.pdf)", "All files (*)"]
            onAccepted: timer.running = true
            Component.onCompleted: visible = true
        }
    
        Timer {
            id: timer
            interval: 100; repeat: false
            onTriggered: {
                poppler.path = urlToPath(""+fileDialog.fileUrl)
                view.focus = true
            }
        }
    
        Poppler{
            id: poppler
        }
    
        ListView{
            id: view
            height: parent.height
            width: 100
            model: poppler.numPages
            delegate:  Image{
                id: image
                width: parent.width
                source: poppler.loaded? "image://poppler/page/" + (modelData+1): ""
                sourceSize.width: width
                MouseArea{
                    anchors.fill: parent
                    onClicked: {
                        image.ListView.view.currentIndex = index
                        image.ListView.view.focus = true
                    }
                }
            }
        }
        Flickable {
            height: parent.height
            anchors.left: view.right
            anchors.right: parent.right
            contentWidth: bigImage.width;
            contentHeight: bigImage.height
            boundsBehavior: Flickable.StopAtBounds
            Image{
                id: bigImage
                sourceSize.width: win.width - view.width
                source: (poppler.loaded && view.currentIndex >= 0)?  "image://poppler/page/"+(view.currentIndex+1): ""
            }
        }
    }
    

    Output:

    enter image description here


    2. Create .pri

    I have created a .pri that is a simple way to attach the files to the project:

    poppler-qml.pri:

    INCLUDEPATH += $$PWD
    
    SOURCES += \
        $$PWD/pdfModel.cpp \
        $$PWD/pageImageProvider.cpp
    
    HEADERS += \
        $$PWD/pdfModel.h \
        $$PWD/pageImageProvider.h
    
    
    unix|win32: LIBS += -lpoppler-qt5
    

    The files must have the following structure:

    poppler-qml
        ├── pageImageProvider.cpp
        ├── pageImageProvider.h
        ├── pdfModel.cpp
        ├── pdfModel.h
        └── poppler-qml.pri
    

    And then add it to your .pro:

    ...
    include(poppler-qml/poppler-qml.pri)
    

    and main.cpp:

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    
    #include <pdfModel.h>
    
    int main(int argc, char *argv[])
    {
        qputenv("POPPLERPLUGIN_DEBUG", "1"); /// enable debug
        qmlRegisterType<PdfModel>("org.docviewer.poppler", 1, 0, "Poppler");
        ...
    

    For example in the following link you can find an example.

    Note:

    The initial code of the plugin has a bug because if it asks for a page that does not exist the application should return a null QImage but as it does not do the verification the application can be broken

    page = document->page(numPage -1);
    if(!page)
        return result;