结合html2canvasjspdf实现html页面转pdf

 

by:授客 qq103355122

 

实践环境

win10

 

vue 2.9.6

 

axios 0.18.0

 

html2canvas 1.0.0-rc.3

 

jspdf 1.5.3

 

安装 html2canvas

进入vue项目所在目录,然后执行以下安装命令

cd /d e:\myprojects\tmp\frontend

e:\myprojects\tmp\frontend>npm install html2canvas

 

安装jspdf

进入vue项目所在目录,然后执行以下安装命令

cd /d e:\myprojects\tmp\frontend

e:\myprojects\tmp\frontend>npm install jspdf

 

编写htmltopdf.js

htmltopdf.js文件路径,例中为src/common/utils/htmltopdf.js

import html2canvas from “html2canvas”

import jspd ffrom “jspdf”

 

/**

 * @param ele要生成 pdf 的dom元素(容器)

 * @param padfname     pdf文件生成后的文件名字

 * */

 

export default {

    install(vue, options) {

        vue.prototype.getpdffromhtml = function(ele, pdffilename) {

            let elew = ele.offsetwidth// 获得该容器的宽

            //   let eleh = ele.offsetheight // 获得该容器的高

            let eleh = ele.scrollheight// 获得该容器的高

 

            let eleoffsettop = ele.offsettop// 获得该容器到文档顶部的距离

            let eleoffsetleft = ele.offsetleft// 获得该容器到文档最左的距离

 

            var canvas = document.createelement(“canvas”)

            var abs = 0

 

            let win_in = document.documentelement.clientwidth ||         document.body.clientwidth// 获得当前可视窗口的宽度(不包含滚动条)

            let win_out = window.innerwidth// 获得当前窗口的宽度(包含滚动条)

 

            if (win_out>win_in) {

                // abs = (win_o – win_i)/2;    // 获得滚动条长度的一半

                abs = (win_out – win_in) / 2// 获得滚动条宽度的一半

            }

            canvas.width = elew * 2// 将画布宽&&高放大两倍

            canvas.height = eleh * 2

 

            var context = canvas.getcontext(“2d”)

            context.scale(2, 2) // 增强图片清晰度

            context.translate(-eleoffsetleft – abs, -eleoffsettop)

 

 

            html2canvas(ele, {

               dpi:300,

               usecors:true//允许canvas画布内可以跨域请求外部链接图片, 允许跨域请求。

            }).then(canvas=> {

                var contentwidth = canvas.width

                var contentheight = canvas.height

                //一页pdf显示html页面生成的canvas高度;

               var pageheight = (contentwidth / 592.28) * 841.89 // 这样写的目的在于保持宽高比例一致 pageheight/canvas.width = a4纸高度/a4纸宽度// 宽度和canvas.width保持一致

               //未生成pdf的html页面高度

               var leftheight = contentheight

               //页面偏移

               var position = 0

 

               //a4纸的尺寸[595.28,841.89],单位像素,html页面生成的canvas在pdf中图片的宽高

               var imgwidth = 595.28

               var imgheight = (595.28 / contentwidth) * contentheight

               var pagedata = canvas.todataurl(“image/jpeg”, 1.0)

               var pdf = newjspdf(“”, “pt”, “a4”)

               //有两个高度需要区分,一个是html页面的实际高度,和生成pdf的页面高度(841.89)

               //当内容未超过pdf一页显示的范围,无需分页

               if (leftheight<pageheight) {

                   //在pdf.addimage(pagedata, ‘jpeg’, 左,上,宽度,高度)设置在pdf中显示;

                   pdf.addimage(pagedata, “jpeg”, 5, 0, imgwidth, imgheight)

                   // pdf.addimage(pagedata, ‘jpeg’, 20, 40, imgwidth, imgheight);

               } else {

                   // 分页

                   while (leftheight>0) {

                       pdf.addimage(pagedata, “jpeg”, 5, position, imgwidth, imgheight)

                       leftheight -= pageheight

                       position -= 841.89

                       //避免添加空白页

                       if (leftheight>0) {

                           pdf.addpage()

                       }

                   }

               }

 

               pdf.save(pdffilename + “.pdf”)

           })

        }

    }

}

 

 

修改main.js

 

如下,main.js中增加以下代码

import htmltopdf from “@/common/utils/htmltopdf”

vue.use(htmltopdf)

 

 

 

.vue组件中使用

 

 

 

 

 

对应代码片段

<template>

    …略

    <el-button size=”mini” type=”primary” @click=”downloadsprinttestreport”>下载报告  </el-button>

    …略

    <!– 迭代测试报告 –>

    <div class=”sprint-test-report-content” v-if = “showreport”  ref=”sprintreport”>

    <!– 标题 –>

    …略

 

</template>

 

<script>

…略

 

exportdefault {

    …略

    methods: {

    …略

        // 下载报告

        downloadsprinttestreport() {

this.getpdffromhtml(this.$refs.sprintreport, this.reportinfo.title)

            …略

 

</script>

下载结果展示

 

 

 

 

存在问题

只能获取浏览器可视区域的内容,类似截图,默认情况下,依赖翻页的些数据都无法直接获取到

参考链接

https://github.com/mrrio/jspdf