2017-05-29 90 views
0

我有一个pdf.js和Qt 5.8的问题,我试图在这个链接Using pdf.js with Qt5.8在我的应用程序中做同样的代码,但他不工作,我不知道为什么,qt显示我此消息对JS:Qt 5.8和Pdf.js错误

"js: Uncaught TypeError: Cannot read property 'PDFJS' of undefined".

这是我的代码在主窗口:

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(); 

回答

5

我会建议从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

输出:

enter image description 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'); 
    });*/ 
+0

感谢它现在问题是,我忘了在像你这样的参数中添加(file:///)。我有另一个问题如何隐藏打印按钮和打开按钮你有一个想法吗? –

+0

这些更改应该在.js文件中给出,我会检查它。其他,如果我的答案可以帮助您将其标记为正确。 – eyllanesc

+0

@Qtfirst我已经添加了一个选项来隐藏这些按钮。 – eyllanesc