jsPdf插件/html转pdf解决中文乱码问题以及jspdf文字居中显示

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');

好的,关于使用iText7将HTML换为PDF解决中文乱码问题,我可以给你一些思路和代码示例。 首先,我们需要使用iText7的Core和pdfHTML模块,你可以在pom.xml中添加以下依赖: ```xml <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext7-core</artifactId> <version>7.1.16</version> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>html2pdf</artifactId> <version>3.0.2</version> </dependency> ``` 然后,我们需要使用pdfHTML模块提供的`HtmlConverter`类将HTML换为PDF。在换之前,我们需要设置一些PDF文档的属性,例如页边距和字体。 下面是一个简单的代码示例,可以将HTML字符串换为PDF文件并解决中文乱码问题: ```java import com.itextpdf.html2pdf.ConverterProperties; import com.itextpdf.html2pdf.HtmlConverter; import com.itextpdf.kernel.font.PdfFontFactory; import com.itextpdf.kernel.font.PdfFont; import com.itextpdf.kernel.geom.PageSize; import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfWriter; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class HtmlToPdfConverter { public static void main(String[] args) throws IOException { // HTML字符串 String html = "<html><head><meta charset='utf-8'></head><body><p>你好,世界!</p></body></html>"; // 输出文件 File outputFile = new File("output.pdf"); FileOutputStream outputStream = new FileOutputStream(outputFile); // 创建PDF文档 PdfWriter writer = new PdfWriter(outputStream); PdfDocument pdfDocument = new PdfDocument(writer); pdfDocument.setDefaultPageSize(PageSize.A4); // 设置字体 PdfFont font = PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H", false); ConverterProperties converterProperties = new ConverterProperties(); converterProperties.setFontProvider((familyName, encoding, size, style, color) -> font); // 将HTML换为PDF HtmlConverter.convertToPdf(new ByteArrayInputStream(html.getBytes()), pdfDocument, converterProperties); // 关闭流 outputStream.close(); } } ``` 在上面的代码中,我们使用了`PdfFontFactory`来创建指定字体的PDF字体对象。这里使用了华文宋体字体和UniGB-UCS2-H编码,你可以根据自己的需求进行调整。 另外,需要注意的是,如果你的HTML中包含中文字符,一定要在`<head>`标签中设置`<meta charset='utf-8'>`,这样就可以正确处理中文字符了。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值