我會建議從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 [line 3058]
/*items.openFile.addEventListener('click', function (e) {
eventBus.dispatch('openfile');
});
items.print.addEventListener('click', function (e) {
eventBus.dispatch('print');
});*/
感謝它現在問題是,我忘了在像你這樣的參數中添加(file:///)。我有另一個問題如何隱藏打印按鈕和打開按鈕你有一個想法嗎? –
這些更改應該在.js文件中給出,我會檢查它。其他,如果我的答案可以幫助您將其標記爲正確。 – eyllanesc
@Qtfirst我已經添加了一個選項來隱藏這些按鈕。 – eyllanesc