VUE - pdfmake的中文字库支持

news/2024/9/24 16:15:06/

前端VUE导出pdf。

        jspdf这个插件对中文支持不够友好,用html的canvas转图片后还是很模糊。最终选用了pdfmake插件。

使用

1.引入pdf

npm install pdfmake  --save

2.页面import

import pdfMake from 'pdfmake/build/pdfmake';
import pdfFonts from 'pdfmake/build/vfs_fonts';
pdfMake.vfs = pdfFonts.pdfMake.vfs;

3.简单functionDemo

      exportPdf(){var fileName = 'test.pdf'const docDefinition = {content: ['测试测试','工单号:12345',],};pdfMake.createPdf(docDefinition).download(fileName)},

4.中文字库支持

官方文档icon-default.png?t=N7T8https://pdfmake.github.io/docs/0.1/fonts/custom-fonts-client-side/vfs/

1. create a new vfs_fonts.js containing your font files 
Install pdfmake npm install pdfmake
Go to package directory ./node_modules/pdfmake/
Create the examples/fonts subdirectory in your pdfmake code directory, if it doesn’t already exist.
Copy your fonts (and other files you wish to embed) into the examples/fonts subdirectory.
Run command node build-vfs.js "./examples/fonts". Or run node build-vfs.js to show help.
Include your new build/vfs_fonts.js file in your code (in the same way you include pdfmake.js or pdfmake.min.js).
The above steps embeds all files from examples/fonts (into a local key/value variable pdfMake.vfs) - not only fonts. Which means you could put images in there, run node build-vfs.js "./examples/fonts, and reference them by filename in your doc-definition object.You don’t need to reference the files in examples/fonts anymore because all files have been copied to the vfs_fonts.js.

4.1 下载字库,我使用的是微软雅黑

4.2 打开项目的node_modules->pdfmake

      创建/examples/fonts 文件夹,插入字库文件

      cmd执行 node build-vfs.js "./examples/fonts"

4.3页面引入字体

import pdfMake from 'pdfmake/build/pdfmake';
import pdfFonts from 'pdfmake/build/vfs_fonts';
pdfMake.fonts = {msyh: {normal: 'msyh.ttf',bold: 'msyh.ttf',italics: 'msyh.ttf',bolditalics: 'msyh.ttf'}
};
pdfMake.vfs = pdfFonts.pdfMake.vfs;

4.4使用字体

      exportPdf(){var fileName = 'test.pdf'const docDefinition = {content: ['测试测试','工单号:12345',],defaultStyle: {font: 'msyh'},};pdfMake.createPdf(docDefinition).download(fileName)},

如果报错 roboto字体找不不到:

 1.打开pdfmake github 项目

GitHub - bpampuch/pdfmake: Client/server side PDF printing in pure JavaScript

    pdfmake/examples/fonts/ 下的roboto字体 跟你的字体都放在 4.2 中执行


http://www.ppmy.cn/news/1428746.html

相关文章

黑马鸿蒙学习5:LIST容器

LIST容器,其实就是如果FOREACH容器展示不全的话,会自动有滚动条了。要注意的是,LIST中必须有固定的listitem这个项,而且列表里面只能包含一个根组件。 必须把ROW容器放到listitem中,如下:

基于单目相机的标靶三维定位——编程实现

上一章内容中我们描述了基于单目相机实现标靶三维定位的原理,关键步骤为1)计算得到相机的内参和畸变系数;2)计算得到标靶角点的世界坐标和像素坐标;3)计算标靶坐标系到相机坐标系的变换矩阵。 第一点我们通过相机标定得到;第二点的核心功能我们可以借助cv::findChessboa…

vuex.esm.js:497 [vuex] unknown action type: userModule/ClearStorage

vuex.esm.js:497 [vuex] unknown action type: userModule/ClearStorage 错误解释: 这个错误表明在Vuex中尝试调用一个名为userModule/ClearStorage的action,但是在该模块的actions定义中没有找到这个名字的函数。这可能是由于拼写错误、未定义的actio…

英语写作中“原理”“准则”principle、norm、criterion、rule等的用法

一、principle、rule一般指科学原理和法则,例如: Newton’ laws of motion are the basic principle of mechanics. (牛顿运动定律是力学的基本原理。) Maxwell’ equations are the rule electromagnetic fields and electroma…

就业班 第三阶段(ansible) 2401--4.16 day2 ansible2 剧本+角色

六、Ansible playbook 简介 playbook 是 ansible 用于配置,部署,和管理被控节点的剧本。   通过 playbook 的详细描述,执行其中的一系列 tasks ,可以让远端主机达到预期的状态。playbook 就像 Ansible 控制器给被控节点列出的的…

分布移位下用于泛化的泛化的自监督测试时训练

Test-Time Training with Self-Supervision for Generalization under Distribution Shifts 论文链接 https://arxiv.org/abs/1909.13231 代码链接 Test-Time Training Project Website 发表于ICML2020 机构: UC Berkeley, UC San Diego 这张文章里的…

探索AI大模型:理论、技术与应用

引言 近年来,随着深度学习技术的迅猛发展,AI大模型已经成为人工智能领域的重要研究方向和热点话题。AI大模型,指的是拥有巨大参数规模和强大学习能力的神经网络模型,如BERT、GPT等,这些模型在自然语言处理、计算机视觉…

mklink 命令的使用(适用场景:C盘爆满,转移到其他盘)

一、背景 将Oracle数据库安装在D盘,由于磁盘爆满,需要将数据库转移到其他磁盘(如:J盘)。 在移动数据库之后,会出现数据库无法使用的情况,这时该如何解决?经了解,可以使用…