このプロジェクトは、PDF.jsデモ、オンラインPDFリーダープラグイン実装関数使用PDF.jsプラグインのダウンロード
測定することができます!
次のように1、SpringBootプロジェクトのディレクトリ構造を作成することです。
2、プロジェクト構成:
pom.xml:
<プロジェクトのxmlns = "http://maven.apache.org/POM/4.0.0"のxmlns:XSI = "http://www.w3.org/2001/XMLSchema-instance"
のxsi:schemaLocationの = "のhttp:/ /maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd ">
<modelVersion> 4.0.0 </ modelVersion>
<groupIdを> com.han < /のgroupId>
<たartifactId>オンライン-読み取りPDF </たartifactId>
<バージョン> 0.0.1-SNAPSHOT </バージョン>
<親>
<groupIdを> org.springframework.boot </ groupIdを>
<たartifactId>春・ブートスターター-parent </たartifactId>
<バージョン> 1.5.10.RELEASE </バージョン>
<relativePath />
</親>
<依存性>
<依存>
<のgroupId> org.springframework.boot </のgroupId>
<たartifactId>ばねブートスタータウェブ</たartifactId>
</依存>
<依存性>
<のgroupId> org.springframework.boot </のgroupId>
<たartifactId>ばねブートスタータthymeleaf </たartifactId>
</依存>
</依存関係>
</プロジェクト>
PDFApplication.java
パッケージcom.han。 輸入org.springframework.boot.SpringApplication。 輸入org.springframework.boot.autoconfigure.SpringBootApplication。 @SpringBootApplication パブリック クラスPDFApplication { 公共 静的 ボイドメイン(文字列[]引数){ SpringApplication.run(PDFApplication。クラス、引数)。 } }
IndexController.java
パッケージcom.han.controller。 インポートのjava.io.File; 輸入java.io.FileInputStream; インポートにjava.io.IOException; インポートのjavax.servlet.http.HttpServletRequest; インポートのjavax.servlet.http.HttpServletResponse; 輸入org.springframework.stereotype.Controller; 輸入org.springframework.web.bind.annotation.RequestMapping。 輸入org.springframework.web.bind.annotation.RequestMethod; @Controller パブリック クラスIndexController { @RequestMapping(値 = "/プレビュー"、メソッド= RequestMethod.GET) 公共 ボイドpdfStreamHandler(HttpServletRequestのリクエスト、HttpServletResponseの応答){ // PDF文件地址 ファイルファイル= 新しいファイル( "E:\\ PDF \\ PDF \\ GJB 8748から2015(GJB 6482-2008k).PDF" ); もし(file.exists()){ バイト []データ= NULL ; FileInputStreamの入力 = NULL ; 試す{ 入力 = 新しいFileInputStreamを(ファイル); データ = 新しい バイト[input.available()]。 input.read(データ)。 response.getOutputStream()書き込み(データ)。 }キャッチ(例外e){ System.out.printlnは( "PDF文件处理异常:" + E)。 } 最後に{ 試み{ 場合(入力!= NULL ){ input.close()。 } } キャッチ(IOExceptionを電子){ e.printStackTrace(); } } } } }
index.htmlを
<!DOCTYPE HTML > < HTML LANG = "EN" > < 頭> < メタ文字コード= "UTF-8" /> < タイトル>在线阅读PDF文件</ タイトル> </ ヘッド> < スクリプト> 関数onLineReadPDF(){ ウィンドウ。オープン(" /js/web/viewer.html?file=/preview " ); } </ スクリプト> < ボディ> <オンラインPDFファイルを読む</ H1 > </ ボディ> </ HTML >
結果のレンダリングを操作します: