标签:
使用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 }
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 = "中文test123"; 7 InputStream streamTemp = new ByteArrayInputStream(is.getBytes()); 8 XMLWorkerHelper.getInstance().parseXHtml(writer, document, 9 streamTemp, null,Charset.forName("UTF-8"),new PdfFont());
标签:
原文地址:http://www.cnblogs.com/joann/p/4292862.html