zoukankan      html  css  js  c++  java
  • 生成PDF,中文失踪问题

    使用itext生成pdf,在linux环境下,中文全部失踪,因为itext要在linux下支持中文字体需要引入itext-asian,并添加一个字体类。

    字体类

     1 import com.itextpdf.text.Font;
     2 import com.itextpdf.text.pdf.BaseFont;
     3 import com.itextpdf.tool.xml.XMLWorkerFontProvider;
     4 
     5 public class PdfFont extends XMLWorkerFontProvider{
     6     public PdfFont(){
     7         super(null,null);
     8     }
     9     @Override
    10     public Font getFont(final String fontname, String encoding, float size, final int style) {
    11                                                                                            
    12         Font FontChinese = null;
    13         try {
    14             BaseFont bfChinese = BaseFont.createFont("STSong-Light",
    15                     "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
    16             FontChinese = new Font(bfChinese, 12, Font.NORMAL);
    17         } catch (Exception e) {
    18             e.printStackTrace();
    19         }
    20         if(FontChinese==null)
    21             FontChinese = super.getFont(fontname, encoding, size, style);
    22         return FontChinese;
    23     }
    24 }

    生成pdf

    1 Document document = new Document(PageSize.A4, 50, 50, 50, 50);
    2             PdfWriter writer = PdfWriter.getInstance(document, bos);
    3             document.open();
    4 
    5             String is = VelocityUtil
    6                     .getVResult(htmlfile.getAbsolutePath(), map);
    7             InputStream streamTemp = new ByteArrayInputStream(is.getBytes());
    8             XMLWorkerHelper.getInstance().parseXHtml(writer, document,
    9                     streamTemp, null,Charset.forName("UTF-8"),new PdfFont());
  • 相关阅读:
    base64编码是什么1
    base64编码是什么
    算法
    spring 的 ApplicationContext.getBean(type) 无法获取bean,报错
    多个tomcat shutdown.sh 导致无法正常关闭的问题
    springboot server.address 配置问题
    jsp页面错误的全局处理
    ASP.NET MVC三层关系
    ASP.NET指定页面转PDF、JPG(插件)
    Java理论知识及面试题
  • 原文地址:https://www.cnblogs.com/joann/p/4292862.html
Copyright © 2011-2022 走看看