linux下使用itext输出pdf

来源:互联网 发布:淘宝盗图怎么电话投诉 编辑:程序博客网 时间:2024/04/29 02:46
 
还不错的open的pdf生成包,我用redhat EL5, tomcat5做到的试验
把下载来的 iTextAsian.jar 和 itext-2.0.4.jar放到tomcat的ROOT/WEB-INF/lib/下
然后用下面的jsp测试。就可以成功了输出日文的pdf了。
<%@
page import="java.io.*,
    com.lowagie.text.*,
    com.lowagie.text.pdf.*,
    java.io.RandomAccessFile,
    com.lowagie.text.Image"
%><%
//
// Template JSP file for iText
// by Tal Liron
//
response.setContentType( "application/pdf" );
// step 1: creation of a document-object
Document document = new Document();
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a temporary buffer
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
PdfWriter.getInstance( document, buffer );
// step 3: we open the document
document.open();
// step 4: we add a paragraph to the document
String japanese = "/u5317/u672c/u3078/u884c/u304d/u307e/u3059/uff01";
BaseFont bfjapanese = BaseFont.createFont("HeiseiMin-W3","UniJIS-UCS2-H",BaseFont.NOT_EMBEDDED);
Font Fontjapanese = new Font(bfjapanese,12,Font.NORMAL);
Paragraph p = new Paragraph(japanese,Fontjapanese);
document.add(p);
// step 5: we close the document
document.close();
// step 6: we output the writer as bytes to the response output
DataOutput output = new DataOutputStream( response.getOutputStream() );
byte[] bytes = buffer.toByteArray();
response.setContentLength(bytes.length);
for( int i = 0; i < bytes.length; i++ ) { output.writeByte( bytes[i] ); }
out.clear();
out = pageContext.pushBody();
%>