Using pdf.js with Qt5.8

情到浓时终转凉″ 提交于 2019-12-06 15:54:19

问题


I am trying to make a pdf viewer in Qt5.8 .I know that poppler is a choice for Qt but I want to do this using pdf.js .I dont know how to embed pdf.js with Qt5.8. I have seen the hello world documentation of pdf.js but it didn't helped. Please help me. Thanks in advance .


回答1:


The basic idea would be to have some widget to display HTML if you want to make use of pdf.js - it seems that QWebEngineView (makes use of Chromium) could do the job as it takes a minimum of code to get your first implementation done.

Having an installation of pdf.js on your computer and a minimalistic gui application (QT Widgets project) prepared with your QT Creator, you could use the following code to have the basics:

#include "mainwindow.h"
#include <QApplication>
#include <QWebEngineView>

static QString pathToPDFjs = QString("file:///path-to/pdfjs-1.8.170-dist/web/viewer.html");

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MainWindow win;
    QWebEngineView *view;
    QString pdfFileURL;

    //you could parse a widget to get the file name
    pdfFileURL = QString("file:///path-to-your/file.pdf");

    //init the view and attach it to the ui
    view = new QWebEngineView();
    win.setCentralWidget(view);
    win.show();

    //auto-load feature in pdf.js, pass file=filename as parameter
    view->load(QUrl::fromUserInput(pathToPDFjs + QString("?file=") + pdfFileURL));
    view->show();

    return app.exec();
}

From there on you can add extra functionality to your user interface. You could even add modifications to your installation of pdf.js (if needed).

If you'd need to call JavaScript on your pdf.js, you can make use of the view's page (a QWebEnginePage) which in turn can runJavaScript.




回答2:


No idea why you want to use pdf.js, but you might want to have a look at QtLabs PDF module. It seems pretty recent and well integrated with current Qt. (and I guess it's more efficient than JavaScript code)

If you want to try it out, here’s how to get started:

git clone git://code.qt.io/qt-labs/qtpdf
cd qtpdf
git submodule update --init --recursive
qmake
make
cd examples/pdf/pdfviewer
qmake
make
./pdfviewer /path/to/my/file.pdf


来源:https://stackoverflow.com/questions/43206263/using-pdf-js-with-qt5-8

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!