java中生成pdf文件需要用到java文件:iText 5.0.6.jar文件
只下载iText5.0.6.jar文件是不能写入中文到pdf文件,同时需要iTextAsian.jar
但是我们在使用这2个jar文件的时候仍然发现一个问题,只能设置windos自带的文字库才能把中文写入:
例子:BaseFont baseFont = BaseFont.createFont("C:/Windows/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
虽然上面的例子解决了中文问题,但是并没有世界解决问题,从路径就可以看出,很明显,这样的写法只能在windows下运行有用。
当我们改成文字库的时候,还是会报错:BaseFont baseFont = BaseFont.createFont("STSong-Light",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
找了好久,解压了iTextAsian.jar文件才发现,原来是iText5.0.6.jar文件包路径不对应。
后来找了新的jar文件替换掉了原先的版本,中文问题解决。
新jar文件下载路径:iText1.3.jar和iTextAsian.jar
pdf下载事例代码:
private void downloadPDF(HttpServletResponse response, String merno,
String filename, String date, String z_mernoshh,
String z_daterq, int z_transcnt, double z_transamt,
double z_transfee, int z_tkcnt, double z_tkamt, double z_tkfee,
double z_remark, List wxdzlist,List list){
Document doc = new Document(PageSize.A4.rotate());// 实例化文档对象; PageSize.A4.rotate();横向
ByteArrayOutputStream baos = null;
String filenamepdf = filename+".pdf";
try {
PdfWriter.getInstance(doc, new FileOutputStream(
"HelloWorld.pdf"));
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",
"UniGB-UCS2-H", false);
Font font = new Font(bfChinese, 10, Font.NORMAL);
baos = new ByteArrayOutputStream();
@SuppressWarnings("unused")
PdfWriter writer = PdfWriter.getInstance(doc, baos);// 创建
@SuppressWarnings("unused")
byte[] bt = baos.toByteArray();
doc.open();//打开文档对象
Paragraph p;
p = new Paragraph(filename+"\n", font);
p.setAlignment(1);
doc.add(p);
p=new Paragraph("===========================================================================================\n",font);
p.setAlignment(1);
doc.add(p);
if(merno.equals("No")){
merno="全部";
p=new Paragraph("商户名称:" + merno + " 制表日期:"+date+"\n",font);
}else{
p=new Paragraph("商户名称:" + merno + " 制表日期:"+date+"\n",font);
}
doc.add(p);
// doc.add(new Paragraph(" \n",font));------为了空行
p=new Paragraph(" \n");
doc.add(p);
PdfPTable table = createPDF(font,wxdzlist,list,z_mernoshh,
z_daterq,z_transcnt,z_transamt,
z_transfee,z_tkcnt,z_tkamt,z_tkfee,
z_remark);//创建表格并写入数据
doc.add(table);
p=new Paragraph(" \n");
doc.add(p);
doc.add(new Paragraph("会计: 复核: 制表:",font));
} catch (DocumentException de) {
System.err.println(de.getMessage());
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
}
// 关闭打开的文档
doc.close();
ServletOutputStream out;
try {
out = response.getOutputStream();
response.setContentType("application/pdf");
response.addHeader("Content-Disposition", "attachment;filename="
+ java.net.URLEncoder.encode(filenamepdf, "utf-8"));
response.setContentLength(baos.size());
baos.writeTo(out);
out.flush();
out.close();
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private PdfPTable createPDF(Font font, List wxdzlist,List list,String z_mernoshh,
String z_daterq, int z_transcnt, double z_transamt,
double z_transfee, int z_tkcnt, double z_tkamt, double z_tkfee,
double z_remark) throws DocumentException,IOException{
PdfPTable table = new PdfPTable(10);// 创建一个多少列的表格
table.getDefaultCell().setMinimumHeight(50); // 设置默认的表格高度
int[] widths = new int[10];
for (int i = 0; i < 10; i++) widths[i] = 1;
widths[0] = 2;
// widths[1] = 2;
// widths[2] = 1;
// widths[date.size()-1] =2;
table.setWidths(widths);// 设置所有宽度
table.setWidthPercentage(100f);
// PdfPCell cell;
// Phrase ph;
// cell = new PdfPCell(ph);
// cell.setColspan(date.size());//合并列
String[] tableName=new String[]{"商户名称","交易类型","交易日期","交易笔数","交易金额","手续费","退款笔数","退款金额","退款手续费","划账金额"};
for(int i=0;i wxd = wxdzlist.get(i).getList();
for(int j=0;j