vue實現pdf導出,解決生成canvas模糊等問題

最近公司項目須要,利用vue實現pdf導出,從而保存到本地打印出來,提及來好像也很容易,具體要怎麼實現呢?css

、咱們要添加兩個模塊

1 第一個.將頁面html轉換成圖片
2 npm install --save html2canvas 
3 第二個.將圖片生成pdf
4 npm install jspdf --save

2、定義全局函數..建立一個htmlToPdf.js文件在指定位置.我我的習慣放在('src/utils/htmlToPdf')

 1 // 導出頁面爲PDF格式
 2 import html2Canvas from 'html2canvas'
 3 import JsPDF from 'jspdf'
 4 export default{
 5   install (Vue, options) {
 6     Vue.prototype.getPdf = function () {
 7       var title = this.htmlTitle
 8       html2Canvas(document.querySelector('#pdfDom'), {
 9         allowTaint: true
10       }).then(function (canvas) {
11         let contentWidth = canvas.width
12         let contentHeight = canvas.height
13         let pageHeight = contentWidth / 592.28 * 841.89
14         let leftHeight = contentHeight
15         let position = 0
16         let imgWidth = 595.28
17         let imgHeight = 592.28 / contentWidth * contentHeight
18         let pageData = canvas.toDataURL('image/jpeg', 1.0)
19         let PDF = new JsPDF('', 'pt', 'a4')
20         if (leftHeight < pageHeight) {
21           PDF.addImage(pageData, 'JPEG', 0, 0, imgWidth, imgHeight)
22         } else {
23           while (leftHeight > 0) {
24             PDF.addImage(pageData, 'JPEG', 0, position, imgWidth, imgHeight)
25             leftHeight -= pageHeight
26             position -= 841.89
27             if (leftHeight > 0) {
28               PDF.addPage()
29             }
30           }
31         }
32         PDF.save(title + '.pdf')
33       }
34       )
35     }
36   }
37 }

3、在main.js中使用咱們定義的函數文件。

1 import htmlToPdf from '@/components/utils/htmlToPdf'
2 Vue.use(htmlToPdf)

4、在須要的導出的頁面..調用咱們的getPdf方法便可.

1 <div class="row" id="pdfDom" style="padding-top: 55px;background-color:#fff;">
2     //給本身須要導出的ui部分.定義id爲"pdfDom".此部分將就是pdf顯示的部分
3 </div>
4 <button type="button" class="btn btn-primary"v-on:click="getPdf()">導出PDF</button>
1 export default {
2   data () {
3       return {
4       htmlTitle: '頁面導出PDF文件名'
5       }
6   }
7  }

到這裏你們會發現功能是能夠實現了,可是會有個致命的問題,導出來的pdf打印出來仍是比較模糊的,那麼,針對這個問題,咱們要怎麼解決呢?html

咱們的思路是:將canvas的屬性width和height屬性放大爲2倍,最後將canvas的css樣式width和height設置爲原來1倍的大小便可,也就是,先將canvas高分辨率輸出,再來壓縮導出打印,便可,廢話很少說,完整代碼以下:vue

 1 // 導出頁面爲PDF格式
 2 import html2Canvas from 'html2canvas'
 3 import JsPDF from 'jspdf'
 4 export default{
 5   install (Vue, options) {
 6     Vue.prototype.getPdf = function (dom,title) {
 7       var title = title
 8       var c = document.createElement("canvas")
 9       var opts = {
10         scale: 2, 
11         canvas: c, 
12         logging: true, 
13         width: document.querySelector(dom).width, 
14         height: document.querySelector(dom).height 
15       };
16       c.width = document.querySelector(dom).width * 2
17       c.height = document.querySelector(dom).height * 2
18       c.getContext("2d").scale(2, 2);
19       html2Canvas(document.querySelector(dom), opts).then(function (canvas) {
20         let contentWidth = canvas.width
21         let contentHeight = canvas.height
22         let pageHeight = contentWidth / 592.28 * 841.89
23         let leftHeight = contentHeight
24         let position = 0
25         let imgWidth = 595.28
26         let imgHeight = 592.28 / contentWidth * contentHeight
27         let pageData = canvas.toDataURL('image/jpeg', 1.0)
28         let PDF = new JsPDF('', 'pt', 'a4')
29         if (leftHeight < pageHeight) {
30           PDF.addImage(pageData, 'JPEG', 0, 0, imgWidth, imgHeight)
31         } else {
32           while (leftHeight > 0) {
33             PDF.addImage(pageData, 'JPEG', 0, position, imgWidth, imgHeight)
34             leftHeight -= pageHeight
35             position -= 841.89
36             if (leftHeight > 0) {
37               PDF.addPage()
38             }
39           }
40         }
41         PDF.save(title + '.pdf')
42       }
43       )
44     }
45   }
46 }
相關文章
相關標籤/搜索