在Qt5.8中使用pdf.js [英] Using pdf.js with Qt5.8
问题描述
我正在尝试在Qt5.8
中创建一个pdf查看器.我知道poppler
是Qt
的选择,但是我想使用pdf.js
做到这一点.我不知道如何在pdf.js
中嵌入pdf.js
Qt5.8
.我看过pdf.js
的hello world
文档,但没有帮助.请帮我.
预先感谢.
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 .
推荐答案
如果要使用pdf.js,基本思想是要有一些小部件来显示HTML-似乎QWebEngineView
(使用了Chromium)可以完成此工作,因为它只需最少的代码即可完成您的第一个实现.
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.
在您的计算机上已经安装了pdf.js并通过QT Creator创建了一个简约的gui应用程序(QT Widgets项目),您可以使用以下代码获得基本信息:
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();
}
从那里开始,您可以在用户界面中添加其他功能. 您甚至可以对pdf.js的安装进行修改(如果需要).
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).
如果您需要在pdf.js上调用JavaScript,则可以使用视图的页面(a QWebEnginePage
),而页面又可以runJavaScript
.
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
.
这篇关于在Qt5.8中使用pdf.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!