1、安装jsPdf
npm install jspdf
2、引入jsPdf
import jsPDF from "jspdf"
3、github下载jsPdf压缩包
4、下载后找到fontconverter.html文件
5、浏览器打开这个html文件,如下图
6、找到一个支持中文的ttf文件,没有的话在这找
7、选择一个ttf文件点击creat之后生成一个js文件,将它引入到项目中
8、开始生成并导出
const doc = new this.$jsPDF();
doc.setFont('simhei');
doc.text('测试打印pdf', 10, 10); //文本
doc.save('测试.pdf');
9、中文乱码结束
10、解决pdf文字居中问题
doc.text("内容", doc.internal.pageSize.getWidth()/2, 10, { align: "center" })
11、整体示例
const doc = new this.$jsPDF("p");
doc.setFont('simhei');
doc.setFontSize(22);
doc.text(this.particularsData[0].value, doc.internal.pageSize.getWidth()/2, 10, { align: "center" })
var splitContent = doc.splitTextToSize(this.particularsData[1].value, 280); //多少字换行
doc.setFontSize(14);
doc.text(splitContent, 15, 25);
doc.save(this.particularsData[0].value+'.pdf');