安装包:npm install pdfmake --save
调用:import * as pdfMake from ‘pdfmake/build/pdfmake.js’;
import * as pdfFonts from ‘pdfmake/build/vfs_fonts.js’;
const img = {
content: [
image:“路径”,
width:536,
height:760
],[
text:“文案”
]
}
pdfMake.fonts = {
Roboto: {
normal: ‘Roboto-Regular.ttf’,
bold: ‘Roboto-Medium.ttf’,
italics: ‘Roboto-Italic.ttf’,
bolditalics: ‘Roboto-Italic.ttf’
}
};
pdfMake.vfs = pdfFonts.pdfMake.vfs;
pdfMake.createPdf(img).download(‘crf.pdf’);
更多实例:http://pdfmake.org/playground.html
注:不兼容中文与中文符号,需要导入宋体字体包
具体方法如下:
1、在github中下载这个项目:https://github.com/bpampuch/pdfmake
有详细的介绍如何自定义安装字体替换字体
html导出pdf
最新推荐文章于 2023-12-10 22:22:48 发布
该博客介绍了如何使用pdfmake库将HTML内容转换为PDF文件。首先通过npm安装pdfmake和html2canvas,然后导入并配置字体。由于pdfmake不支持中文,所以建议使用canvas绘制HTML并转换为图片来生成PDF。具体步骤包括调用html2canvas将HTML元素转化为canvas,再将其转换为base64编码的图片,最后整合到pdfMake中创建和下载PDF。
摘要由CSDN通过智能技术生成