[英]Using pdf.js with Qt5.8
我试图做一个PDF浏览器在Qt5.8
。我知道, poppler
是一个选择Qt
,但我想这样做使用pdf.js
。我不知道如何嵌入pdf.js
与Qt5.8
。 我看过pdf.js
的hello world
文档,但没有帮助。 请帮我。 提前致谢 。
不知道为什么要使用pdf.js,但是您可能想看看QtLabs PDF模块 。 看来是最近的事,并且与当前的Qt很好地集成在一起。 (而且我想它比JavaScript代码更有效)
如果您想尝试一下,请按以下步骤开始:
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
基本的想法是,如果您想使用pdf.js,则需要一些小部件来显示HTML-看来QWebEngineView
(利用Chromium)可以完成此工作,因为它只需最少的代码即可完成第一个实现。
在您的计算机上安装了pdf.js并通过QT Creator创建了一个简约的gui应用程序(QT Widgets项目)后,您可以使用以下代码获得基本信息:
#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的安装进行修改(如果需要)。
如果您需要在pdf.js上调用JavaScript,则可以使用视图的页面( QWebEnginePage
),该页面又可以运行runJavaScript
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.