前言
jspdf插件用于生成pdf文件,功能非常强大,但是美中不足是没有中文字体,使用中文的话需要单独下载
引用官方文档的话:
The 14 standard fonts in PDF are limited to the ASCII-codepage. If you want to use UTF-8 you have to integrate a custom font, which provides the needed glyphs. jsPDF supports .ttf-files. So if you want to have for example Chinese text in your pdf, your font has to have the necessary Chinese glyphs. So, check if your font supports the wanted glyphs or else it will show garbled characters instead of the right text.
一.下载中文字体
1.寻找字体
网络很多推荐思源中文字体,但是那个字体文件太大了,几十M,放到项目可能打不开页面,这里推荐一个免费商用字体网站:字体天下,里面字体很多,而且可以寻找自己喜欢的字体

2.下载字体及js文件转换
下载完成解压后看到一个ttf文件

打开js转换工具

输入字体名称,选择文件点击生成即可,这里字体名称就是后边doc.setFont("jianbaosong");括号中的名称
二.使用字体
把生成的js文件放入项目中,然后pdf对象引用即可
const doc = new jsPDF();
doc.setFont("jianbaosong");
三. 最后
jsPDF官方文档:https://artskydj.github.io/jsPDF/docs/jsPDF.html#line
本文介绍了如何在使用jspdf生成PDF时解决中文显示问题,包括下载合适的中文字体如'jianbaosong',通过js转换工具将其转换为js文件,并在项目中正确引用。
605

被折叠的 条评论
为什么被折叠?



