我正在尝试在Qt5.8
.I 中制作一个 pdf 查看器。我知道这poppler
是一个选择,Qt
但我想使用 .I 来做到这一点。pdf.js
我不知道如何嵌入pdf.js
. Qt5.8
我看过hello world
文档,pdf.js
但没有帮助。请帮我。提前致谢 。
问问题
2270 次
2 回答
3
如果你想使用 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,你可以使用视图的页面 (a QWebEnginePage
),它反过来可以runJavaScript
.
于 2017-04-20T20:29:50.153 回答
2
不知道为什么要使用 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
于 2017-04-04T12:19:39.413 回答