2017-04-04 30 views
0

使用pdf.js我試圖做一個PDF瀏覽器在Qt5.8。我知道popplerQt一個選擇,但我想這樣做使用pdf.js。我不知道如何與Qt5.8嵌入pdf.js。我已經看過pdf.jshello world文檔,但它沒有幫助。請幫幫我。 在此先感謝。與Qt5.8

+0

pdf.js是JavaScript。 Qt是C++。你想如何混合兩種不同的編程語言? –

+0

@DmitrySazonov不,我不想混合我只是想知道如果我可以使用pdf.js與Qt – Tarptaeya

+0

你可以使用瀏覽器('QWebEngineView')。再次說明:「使用Qt使用pdf.js」是什麼意思?它是不同的編程語言。 –

回答

2

不知道爲什麼你要使用pdf.js,但你可能想看看QtLabs PDF module。它看起來很新,並與當前的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 
2

的基本想法是有一些小工具,如果你要顯示HTML想要使用pdf.js - 似乎QWebEngineView(使用Chromium)可以完成這項工作,因爲它需要最少的代碼才能完成第一個實現。

有您的計算機和一個簡約的GUI應用程序與你的Qt Creator的準備(Qt控件項目)上pdf.js的安裝,你可以使用下面的代碼有基礎:

#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

Proof-of-Concept QtPDFjs