IText实现url转pdf, 解决中文字体问题

IText实现url转pdf

入门文章:http://www.micmiu.com/opensource/expdoc/itext-pdf-demo/
进阶文章:http://www.micmiu.com/opensource/expdoc/itext-url-pdf/

实现中文的两种方法

1、

BaseFont base = BaseFont.createFont("fonts\\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font fontCN = new Font(base, 12, Font.NORMAL, BaseColor.BLUE);
document.add(new Paragraph("这是中文:欢迎来到iText世界。", fontCN));

或者:

BaseFont base = BaseFont.createFont("fonts\\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font fontCN = new Font(base, 12, Font.NORMAL, BaseColor.BLUE);
Paragraph p = new Paragraph();
p.setFont(font);
p.add("这是一个段落 hello world");
document.add(p);

2、

Font f1 = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
document.add(new Paragraph("hello world, 你好a世界", f1));

==================================

写文章记录一下

import java.io.FileOutputStream;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;

public class IText2PDF {

    public static void main(String[] args) throws Exception {
        String pdfPath = "E:\\Desktop/iText.pdf";
        createFirstPDF(pdfPath);
    }

    public static void createFirstPDF(String pdfPath) throws Exception {
        // 第一步: Create a Document
        Document document = new Document(PageSize.A4);
        // 第二 步: Get a PdfWriter instance.
        PdfWriter.getInstance(document, new FileOutputStream(pdfPath));
        // 第三步:Open the Document.
        document.open();

        // 添加Meta信息
        document.addAuthor("Dong Hao");
        document.addCreator("Dong Hao");
        document.addTitle("URL转换PDF");
        document.addSubject("COSMOPlat");
        document.addCreationDate();
        document.addKeywords("Java,iText");

        // 添加Header信息
        document.addHeader("blog", "https://blog.csdn.net/weixin_39715012");
        document.addHeader("mail", "donghao0205@163.coom");

        // 第四步:添加内容

        // 添加 Paragraph
        document.add(new Paragraph("Hello iText."));
        document.add(Chunk.NEWLINE);

        // 添加 中文信息
        BaseFont base = BaseFont.createFont("fonts\\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        Font fontCN = new Font(base, 12, Font.NORMAL, BaseColor.BLUE);
        document.add(new Paragraph("这是中文:欢迎来到iText世界。", fontCN));

        // 第五步:Close the Document.
        document.close();
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于使用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'>`,这样就可以正确处理中文字符了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值