Qt 5.8和Pdf.js错误

问题描述 投票:1回答:1

我有pdf.js和Qt 5.8的问题,我试图在我的应用程序中的链接Using pdf.js with Qt5.8做相同的代码,但他不工作,我不知道为什么,qt告诉我这个关于JS的消息:

“js:Uncaught TypeError:无法读取未定义的属性'PDFJS'”。

这是我在mainwindow中的代码:

QWebEngineView *view;
QString pdfFileURL;

QString pathToPDFjs = QString("file:///"+qApp->applicationDirPath()+"/libraries/PDF/viewer.html");

pdfFileURL = "file:///C:/Users/Administrateur/Desktop/CV.pdf";

view = new QWebEngineView();
this->setCentralWidget(view);

view->load(QUrl::fromUserInput(pathToPDFjs + QString("?file=") + pdfFileURL));
view->show();
c++ qt qt5 pdf.js qwebengineview
1个回答
8
投票

我建议从here下载源代码。

然后将整个文件复制到项目中的文件夹中(在我的案例中为3rdParty):

.
├── 3rdParty
│   └── pdfjs-1.7.225-dist
│       ├── build
│       │   ├── pdf.js
│       │   └── pdf.worker.js
│       ├── LICENSE
│       └── web
│           ├── cmaps
│           ├── {another files}
│           ├── viewer.css
│           ├── viewer.html
│           └── viewer.js
├── CV.pdf
├── main.cpp
├── mainwindow.cpp
├── mainwindow.h
├── mainwindow.ui
└── pdfjsExample.pro

另一个建议是在.pro中创建一个命令,这样你就可以将库复制到可执行文件的一边,并且没有文件夹位置的问题(其中CV.pdf是我用来进行测试的pdf)。

COPY_CONFIG = 3rdParty CV.pdf
copy_cmd.input = COPY_CONFIG
copy_cmd.output = ${QMAKE_FILE_IN_BASE}${QMAKE_FILE_EXT}
copy_cmd.commands = $$QMAKE_COPY_DIR ${QMAKE_FILE_IN} ${QMAKE_FILE_OUT}
copy_cmd.CONFIG += no_link_no_clean
copy_cmd.variable_out = PRE_TARGETDEPS
QMAKE_EXTRA_COMPILERS += copy_cmd

代码看起来像这样:

QWebEngineView *view;
QString pdfFileURL;

QString pathToPDFjs = QString("file:///%1/%2")
        .arg(QDir::currentPath())
        .arg("3rdParty/pdfjs-1.7.225-dist/web/viewer.html");

pdfFileURL = QString("file:///%1/%2").arg(QDir::currentPath()).arg("CV.pdf");

view = new QWebEngineView();
setCentralWidget(view);

QUrl url = QUrl::fromUserInput(pathToPDFjs + QString("?file=") + pdfFileURL);

view->load(url);

注意:将applicationDirPath修改为CurrentPath,这样如果我将可执行文件移动到另一个位置,我就不会产生问题,因为应用程序可以正常工作3rdParty文件夹,我们的可执行文件必须在一起。

完整的代码是here

如果要隐藏打印按钮和打开按钮,则应注释以下行:

viewer.html [第178行]

<!--button id="openFile" class="toolbarButton openFile hiddenLargeView" title="Open File" tabindex="32" data-l10n-id="open_file">
  <span data-l10n-id="open_file_label">Open</span>
</button>

<button id="print" class="toolbarButton print hiddenMediumView" title="Print" tabindex="33" data-l10n-id="print">
  <span data-l10n-id="print_label">Print</span>
</button-->

viewer.js [第3058行]

  /*items.openFile.addEventListener('click', function (e) {
    eventBus.dispatch('openfile');
  });
  items.print.addEventListener('click', function (e) {
   eventBus.dispatch('print');
  });*/
© www.soinside.com 2019 - 2024. All rights reserved.