vue实现pdf导出,解决生成canvas模糊等问题
最近公司项目需要,利用vue实现pdf导出,从而保存到本地打印出来,说起来好像也很容易,具体要怎么实现呢?
1 、我们要添加两个模块
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打印出来还是比较模糊的,那么,针对这个问题,我们要怎么解决呢?
我们的思路是:将canvas的属性width和height属性放大为2倍,最后将canvas的css样式width和height设置为原来1倍的大小即可,也就是,先将canvas高分辨率输出,再来压缩导出打印,即可,废话不多说,完整代码如下:
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 }