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