PDF中文报表

<%@ page
import="java.io.*,java.awt.Color,com.lowagie.text.*,com.lowagie.text.pdf.*"
contentType="text/html; charset=utf-8" autoFlush="true"%>

<%
response.setContentType("application/pdf");
Document document = new Document(PageSize.A4, 10, 10, 10, 10);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
PdfWriter writer = PdfWriter.getInstance(document, buffer);
document.open();
BaseFont bfChinese = BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
Paragraph test1 = new Paragraph("PDF我们是害虫", FontChinese);
document.add(test1);
Image img = Image.getInstance(pageContext.getServletContext()
.getRealPath("/")
+ "form\\b.jpg");
document.add(img);
document.close();
DataOutput output = new DataOutputStream(response.getOutputStream());
byte[] bytes = buffer.toByteArray();
response.setContentLength(bytes.length);
response.setCharacterEncoding("GBK");
for (int i = 0; i < bytes.length; i++) {
output.writeByte(bytes[i]);
}
out.clear();
//out = pageContext.pushBody();
%>

注意:
需要添加iTextAsian.jar包,提他编码:
Chinese Simplified:
STSong-Light and STSongStd-Light with the encodings UniGB-UCS2-H and UniGB-UCS2-V
Chinese Traditional:
MHei-Medium, MSung-Light and MSungStd-Light with the encodings UniCNS-UCS2-H and UniCNS-UCS2-V
Japanese:
HeiseiMin-W3, HeiseiKakuGo-W5 and KozMinPro-Regular with the encodings UniJIS-UCS2-H, UniJIS-UCS2-V, UniJIS-UCS2-HW-H and UniJIS-UCS2-HW-V
Korean:
HYGoThic-Medium, HYSMyeongJo-Medium and HYSMyeongJoStd with the encodings UniKS-UCS2-H and UniKS-UCS2-V

你可能感兴趣的:(html)