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 .

- 949
- 1
- 12
- 27
-
pdf.js is JavaScript. Qt is C++. How do you want to mix two different programming languages? – Dmitry Sazonov Apr 04 '17 at 14:44
-
@DmitrySazonov No, I don't wanna mix them I just want to know If there is a way if I can use pdf.js with Qt – Anmol Gautam Apr 04 '17 at 14:47
-
You can use a browser (`QWebEngineView`). Once again: what you mean by "use pdf.js with Qt"?? It is different programming languages. – Dmitry Sazonov Apr 04 '17 at 14:52
-
1@DmitrySazonov one very important part of Qt is QML which allows to [include JavaScript code and use it](http://doc.qt.io/qt-5/qtqml-javascript-imports.html). Please stop shooting ghosts if you don't know what you're talking about. – Massimo Callegari Apr 04 '17 at 15:33
-
1QML is not a pure JS. And topic stater doesn't talk anything about QML. I beleive that you can't simply include pdf.js into QML and have a rendering feature. JS in QML is used for logic. – Dmitry Sazonov Apr 05 '17 at 08:18
2 Answers
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
.

- 179
- 7
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

- 2,099
- 1
- 26
- 39