vue下使用 pdf.js 預覽 和 打印 PDF文檔 兼容React

我使用前端開發框架是vue,有一個打印PDF文檔的需求.
這個需求最開始是交給後臺,可是明顯不切實際, 由於後臺服務器,根本就沒法鏈接打印機.
因此這個預覽加打印文檔的需求就交到了前端, 開始我有想過直接打開pdf文件, 但事實上直接打開pdf文件的表現不太好.若是是在 win7+ie11 的環境下會直接下載文件, 並且直接打開pdf在 ie,firefox,chrome 三大環境下的表現都不太統一.
因此根據需求, 我最開始使用了vue-pdfhtml

  1. 安裝vue-pdf

    npm install --save vue-pdf前端

  2. 新建一個vue,命名爲vpdf
<template>
  <pdf src="./static/relativity.pdf"></pdf>
</template>

<script>
import pdf from 'vue-pdf'

export default {
  components: {
    pdf
  }
}

3.引入到須要預覽的頁面使用vpdf標籤vue

import vpdf form 'vpdf';
components: {
    vpdf
}

4.npm start 啓動git

使用vue-pdf很簡單, 可是隻能預覽pdf不能打印,因此我放棄了這個思路.github

接下來我嘗試了 PDF.jsweb

1.獲取當前版本vue-router

git clone https://github.com/mozilla/pdf.js.git

2.啓動chrome

cd pdf.js
npm install -g gulp-cli
npm install
gulp server

3.訪問 http://localhost:8888/web/viewer.html 能夠預覽和打印pdf,可是這時候要加入到本身的項目,就要編譯pdf.js
4.編譯npm

build pdf.js 
gulp generic //在src文件夾獲取開發版
gulp minified //獲取生產版

5.vue-router路由gulp

在vue下直接使用使用pdf.js的viewer.html有一個路徑映射的問題,就是在服務器加載不出來,雖然能夠把html改爲vue模板加載,可是爲了省事,我直接把編譯好的min文件夾放在了static路徑

6.經過帶參數的url來訪問PDF

http://localhost:8080/generic/web/viewer.html?file=qbs.pdf

參考

http://www.cnblogs.com/kagome2014/p/kagome2014001.html
http://blog.csdn.net/hyhbyl/article/details/20994945
相關文章
相關標籤/搜索