驾校微信小程序_vue中将网页打印成pdf实例代码

vue中将网页打印成pdf实例代码       本篇文章主要介绍了vue中将网页打印成pdf实例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
script type="text/ecmascript-6" import html2Canvas from '@/components/html2canvas.js' import JsPDF from '@/components/jsPdf.debug.js' export default { methods: { getPdf: function () { let _this = this let pdfDom = document.querySelector('#pdfDom') html2Canvas(pdfDom, { onrendered: function(canvas) { let contentWidth = canvas.width let contentHeight = canvas.height let pageHeight = contentWidth / 592.28 * 841.89 let leftHeight = contentHeight let position = 0 let imgWidth = 595.28 let imgHeight = 592.28 / contentWidth * contentHeight let pageData = canvas.toDataURL('image/jpeg', 1.0) let PDF = new JsPDF('', 'pt', 'a4') if (leftHeight pageHeight) { PDF.addImage(pageData, 'JPEG', 0, 0, imgWidth, imgHeight) } else { while (leftHeight 0) { PDF.addImage(pageData, 'JPEG', 0, position, imgWidth, imgHeight) leftHeight -= pageHeight position -= 841.89 if (leftHeight 0) { PDF.addPage() PDF.save(_this.pdfData.title + '.pdf') html2Canvas() /script

需要引入

html2canvas.js'

jsPdf.debug.js

这两个插件库可轻松百度到。如果eslint验证报错,可在eslintignore中设置不对这两个文件进行验证。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持凡科。




扫描二维码分享到微信