纯前端html导出pdf--分页+不分页--html2canvas+jsPDF
前言
最近在项目中,有一个导出pdf功能,需要纯前端来实现,调研了多种pdf导出方式,最终决定使用html2canvas+jsPDF来实现需求。
本文就简单介绍一下html2canvas+jsPDF导出pdf的实现,网上大部分实现导出pdf都是以分页为主的,本文还将附上不分页导出pdf的实现方法。(只附js代码)
html2canvas+jsPDF导出pdf原理:通过html2canvas将遍历页面元素,并渲染生成canvas,然后将canvas图片格式添加到jsPDF实例,生成pdf。
安装:
npm install html2canvas --save
npm install jsPDF --save
配置:
main.js文件里面配置(引入、挂载)
import html2canvas from 'html2canvas'
import jsPDF from 'jsPDF '
Vue.prototype.html2canvas = html2canvas
Vue.prototype.jsPDF = jsPDF
或者--------------------------------------------------------------------------------
index.html页面直接引入js文件:
<script src="https://cdn.bootcss.com/html2canvas/0.5.0-beta4/html2canvas.js"></script>
<script src="https://cdn.bootcss.com/jspdf/1.3.4/jspdf.debug.js"></script>
导出pdf按a4纸大小分页处理:// 下载pdf完整方法
downPdf () {
注意:生成的pdf只有页面窗口可见的区域,有滚动条的下面没有生成出来(要注意这里是一个坑)
坑:如果截取是body的这个层级,而刚好body设置了overflow: hidden;那超出的部分是永远截取不到的,因为这个节点的dom高就是窗口可见的高度,并不包含滚动条多出来的部分。
解决办法:只需要在导出之前将overflow:auto设置成visible(默认即可);
导出pdf后再设置回去。
// 导出之前将dom的overflow:auto设置成visible
this.$('#boardPdf').css({'overflow-y': 'visible', 'height': 'auto'})
this.$('#app').css({'overflow-y': 'visible', 'height': 'auto'})
this.$('body').css({'overflow-y': 'visible', 'height': 'auto'}) // eslint-disable-next-line
html2canvas(document.querySelector('#boardPdf'), {
scale: 2,
onrendered: function (canvas) {
var contentWidth = canvas.width
var contentHeight = canvas.height
// 一页pdf显示html页面生成的canvas高度;
var pageHeight = contentWidth / 592.28 * 841.89
// 未生成pdf的html页面高度
var leftHeight = contentHeight
// pdf页面偏移
var position = 0
// a4纸的尺寸[595.28,841.89],html页面生成的canvas在pdf中图片的宽高
var imgWidth = 595.28
var imgHeight = 592.28 / contentWidth * contentHeight
var pageData = canvas.toDataURL('image/jpeg', 1.0)
// eslint-disable-next-line
var pdf = new jsPDF('', 'pt', 'a4')
// 有两个高度需要区分,一个是html页面的实际高度,和生成pdf的页面高度(841.89)
// 当内容未超过pdf一页显示的范围,无需分页
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文件命名
pdf.save('report_pdf_' + new Date().getTime() + '.pdf')
},
background: '#0B1A48'
})
// 导出pdf后,将dom原本的属性设置回去
this.$('#boardPdf').css({'overflow-y': 'auto', 'height': '100%'})
this.$('#app').css({'overflow-y': 'auto', 'height': '100%'})
this.$('body').css({'overflow-y': 'auto', 'height': '100%'})
}
导出pdf自适应大小不分页处理:
// 下载pdf完整方法
downPdf () {
// 生成的pdf只有页面窗口可见的区域,有滚动条的下面没有生成出来,需在生成PDF前,改overflow属性auto为visible
this.$('#boardPdf').css({'overflow-y': 'visible', 'height': 'auto'})
this.$('#app').css({'overflow-y': 'visible', 'height': 'auto'})
this.$('body').css({'overflow-y': 'visible', 'height': 'auto'})
// 获取dom高度、宽度
var shareContent = document.querySelector('#boardPdf')
var width = shareContent.offsetWidth / 4
var height = shareContent.offsetHeight / 4
let _this = this
// eslint-disable-next-line
html2canvas(document.querySelector('#boardPdf'), {
onrendered: function (canvas) {
var context = canvas.getContext('2d')
context.mozImageSmoothingEnabled = false
context.webkitImageSmoothingEnabled = false
context.msImageSmoothingEnabled = false
context.imageSmoothingEnabled = false
var pageData = canvas.toDataURL('image/jpeg', 1.0)
var img = new Image()
img.src = pageData
img.onload = function () {
// 获取dom高度、宽度
img.width = img.width / 2
img.height = img.height / 2
img.style.transform = 'scale(0.5)'
if (width > height) {
// 此可以根据打印的大小进行自动调节
// eslint-disable-next-line
var pdf = new jsPDF('l', 'mm', [
width * 0.505,
height * 0.545
])
} else {
// eslint-disable-next-line
var pdf = new jsPDF('p', 'mm', [
width * 0.505,
height * 0.545
])
}
pdf.addImage(
pageData,
'jpeg',
0,
0,
width * 0.505,
height * 0.545
)
pdf.save(_this.boardNameTitle + '.pdf')
}
},
background: '#0B1A48'
})
// 导出pdf后,将dom原本属性设置回去
this.$('#boardPdf').css({'overflow-y': 'auto', 'height': '100%'})
this.$('#app').css({'overflow-y': 'auto', 'height': '100%'})
this.$('body').css({'overflow-y': 'auto', 'height': '100%'})
}
纯前端html导出pdf--分页+不分页--html2canvas+jsPDF的更多相关文章
- c# 通过html导出pdf,带分页
通过NuGet安装 PechkinPechkin.Synchronized 一下示例是控制台应用程序 static void btnCreate() { SynchronizedPechkin sc ...
- 前端 / JavaScript 导出PDF的实践
1.库:jspdf,自己定义一个高宽,如A4的210mm×297mm 2.让设计给背景图(包括:页眉页脚),水印图(背景透明,高宽和你的PDF单页一致)以及很多,能设计给的设计要给,因为在pdf上,排 ...
- 纯前端导出pdf文件
纯前端js导出pdf,已经用于生产环境. 工具: 1.html2canvas,一种让html转换为图片的工具. 2.pdfmake或者jspdf ,一种生成.编辑pdf,并且导出pdf的工具. pdf ...
- 前端导出pdf
html2canvas文档地址 http://html2canvas.hertzen.com/configuration 方式一:使用html2canvas和jspdf插件实现 该方式是通过html2 ...
- JS导出PDF插件(支持中文、图片使用路径)
在WEB上想做一个导出PDF的功能,发现jsPDF比较多人推荐,遗憾的是不支持中文,最后找到pdfmake,很好地解决了此问题.它的效果可以先到http://pdfmake.org/playgroun ...
- 纯前端下载pdf链接文件,而不是打开预览的解决方案
纯前端下载pdf链接文件,而不是打开预览的解决方案 一,介绍与需求 1.1,介绍 XMLHttpRequest 用于在后台与服务器交换数据.这意味着可以在不重新加载整个网页的情况下,对网页的某部分进行 ...
- vue后台_纯前端实现excel导出/csv导出
之前的文件下载功能一般是由前后端配合实现,由于项目需要,纯前端实现了一把excel的导出功能: 一.excel导出 1.安装依赖库 xlsx:这是一个功能强大的excel处理库,但是上手难度也很大,还 ...
- java根据模板导出PDF详细教程
原文:https://blog.csdn.net/pengyufight/article/details/75305128 题记:由于业务的需要,需要根据模板定制pdf文档,经测试根据模板导出word ...
- JSP页面导出PDF格式文件
JSP页面导出PDF格式文件基本在前端页面可以全部完成 <script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/ ...
随机推荐
- Spring Bean 的生命周期,如何被管理的
1. 实例化一个Bean,也就是我们通常说的new 2. 按照Spring上下文对实例化的Bean进行配置,也就是IOC注入 3. 如果这个Bean实现了BeanNameAware接口,会调用它实现的 ...
- Java网络编程:OSI七层模型和TCP/IP模型介绍
OSI(Open System Interconnection),开放式系统互联参考模型 .是一个逻辑上的定义,一个规范,它把网络协议从逻辑上分为了7层.每一层都有相关.相对应的物理设备,比如常规的路 ...
- css实现下拉框导航条
html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <ti ...
- Math.random()详解
Math.random()是令系统随机选取大于等于 0.0 且小于 1.0 的伪随机 double 值,是Java语言常用代码.例如:double a=Math.random()*(3-1)+1,设置 ...
- Mysql学习笔记(003)-案例讲解基础查询
案例讲解基础查询 #.下面的语句是否可以执行成功 SELECT last_name, first_name, salary AS sal FROM employees; #.下面的语句是否可以执行成功 ...
- java基础学习笔记二(接口、super、this)
一.super 和 this的用法 主要解释一下引用构造函数的用法 super(参数):调用父类中的某一个构造函数(应该为构造函数中的第一条语句) this(参数):调用本类中另一种形式的构造函数(应 ...
- Bazel: Ubuntu安装bazel-version-installer-os.sh失败的解决方法。
使用 bash ./bazel-version-installer-os.sh from: http://www.cnblogs.com/Need4Speak/p/5438240.html
- Service系统服务(二):补充应用技巧、软连接与硬连接、man手册、zip备份、vim效率操作、自定义yum软件仓库、发布及测试yum仓库、编译安装软件包
一.补充应用技巧 目标: 本例要求掌握在运维中比较常用的一些扩展命令技巧的使用,完成下列小技巧操作: 1> 采用数值形式将目录/root的权限调整为 rwx------ 2> 将记录的 ...
- shell(计算机壳层)(一)
在计算机科学中,Shell俗称壳(用来区别于核),是指“提供使用者使用界面”的软件(命令解析器).它类似于DOS下的command和后来的cmd.exe.它接收用户命令,然后调用相应的应用程序. wi ...
- KEIL Code RO-data RW-data ZI-data 【转】
来自:http://jinyong314.blog.163.com/blog/static/30165742201052225415901/ 字节 8位半字 16位字 32位 Code, RO-d ...