Java生成PDF、クライアントページダウンロード
PDFの生成には2つのjarが必要です
itext2_rq.jar iTextAsian.jar
効果はtomcatプロジェクトの下のPDFフォルダの下に生産されたPDFファイルがあります.
図:
JSPページダウンロード.PDFファイルをダウンロードするには、パスを選択できます.
itext2_rq.jar iTextAsian.jar
/*
* @(#)demo02
* @(#)createPDF.java 2014-7-3
*
* Copyright © 2001-2010, All rights reserved.
* iSoftStone Information Technology ( Group ) Co., Ltd.
*/
package demo02;
import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.lowagie2.text.Cell;
import com.lowagie2.text.Document;
import com.lowagie2.text.Element;
import com.lowagie2.text.Font;
import com.lowagie2.text.PageSize;
import com.lowagie2.text.Paragraph;
import com.lowagie2.text.Phrase;
import com.lowagie2.text.Rectangle;
import com.lowagie2.text.Table;
import com.lowagie2.text.pdf.BaseFont;
import com.lowagie2.text.pdf.PdfWriter;
public class createPDF {
public static void main(String[] args) {
try {
new createPDF().exprotPdf();
} catch (Exception e) {
e.printStackTrace();
}
}
// PDF
public String exprotPdf() throws Exception {
// pdf ,
//PDF
String pdfName = getNowTime().substring(0, 10) + ".pdf";
System.out.println(pdfName);
String PDF_PATH = "PDF/";
// document
Document document = new Document(PageSize.A3);
File file;
try {
file = new File(PDF_PATH);
if (!file.exists()) {
file.mkdirs();
}
System.out.println(file.getAbsolutePath());
PdfWriter.getInstance(document, new FileOutputStream(file + "/" + pdfName));
// pdf ,
document.open();
// 6
int row = 6;
Table datatable = new Table(row);
//
datatable.setWidth(100);
// ( )
datatable.setBorder(0);
//
datatable.setPadding(3);
// , iText , iTextAsian.jar
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
//
Font FontChinese = new Font(bfChinese, 10, Font.BOLD);
document.add(new Paragraph("yzc PDF ", FontChinese));
//
Cell cell = new Cell(new Phrase("students_table", FontChinese));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setLeading(30);
cell.setColspan(row);
cell.setBorder(Rectangle.NO_BORDER);
cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
datatable.addCell(cell);
datatable.setBorderWidth(2);
datatable.setAlignment(1);
//
Cell cell2 = new Cell(new Phrase(" ", FontChinese));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);//
cell2.setLeading(30);
cell2.setColspan(1);
datatable.addCell(cell2);
datatable.setBorderWidth(2);
datatable.setAlignment(1);
//
Cell cell3 = new Cell(new Phrase(" ", FontChinese));
cell3.setHorizontalAlignment(Element.ALIGN_CENTER);//
cell3.setLeading(30);
cell3.setColspan(1);
datatable.addCell(cell3);
datatable.setBorderWidth(2);
datatable.setAlignment(1);
//
Cell cell4 = new Cell(new Phrase(" ", FontChinese));
cell4.setHorizontalAlignment(Element.ALIGN_CENTER);//
cell4.setLeading(30);
cell4.setColspan(1);
datatable.addCell(cell4);
datatable.setBorderWidth(2);
datatable.setAlignment(1);
//
Cell cell5 = new Cell(new Phrase(" ", FontChinese));
cell5.setHorizontalAlignment(Element.ALIGN_CENTER);//
cell5.setLeading(30);
cell5.setColspan(1);
datatable.addCell(cell5);
datatable.setBorderWidth(2);
datatable.setAlignment(1);
//
Cell cell6 = new Cell(new Phrase(" ", FontChinese));
cell6.setHorizontalAlignment(Element.ALIGN_CENTER);//
cell6.setLeading(30);
cell6.setColspan(1);
datatable.addCell(cell6);
datatable.setBorderWidth(2);
datatable.setAlignment(1);
//
Cell cell7 = new Cell(new Phrase("ID", FontChinese));
cell7.setHorizontalAlignment(Element.ALIGN_CENTER);//
cell7.setLeading(30);
cell7.setColspan(1);
datatable.addCell(cell7);
datatable.setBorderWidth(2);
datatable.setAlignment(1);
// , ( )
FontChinese = new Font(bfChinese, 10, Font.NORMAL);
Cell cell1 = null;
//
List<Map<String, Object>> userDate = getResource();
for (int i = 0; i < userDate.size(); i++) {
cell1 = new Cell(new Phrase(userDate.get(i).get("name").toString(), FontChinese));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
datatable.addCell(cell1);
cell1 = new Cell(new Phrase(userDate.get(i).get("age").toString(), FontChinese));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
datatable.addCell(cell1);
cell1 = new Cell(new Phrase(userDate.get(i).get("gender").toString(), FontChinese));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
datatable.addCell(cell1);
cell1 = new Cell(new Phrase(userDate.get(i).get("tel").toString(), FontChinese));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
datatable.addCell(cell1);
cell1 = new Cell(new Phrase(userDate.get(i).get("classes").toString(), FontChinese));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
datatable.addCell(cell1);
cell1 = new Cell(new Phrase(userDate.get(i).get("id").toString(), FontChinese));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
datatable.addCell(cell1);
}
//
Cell cella1 = new Cell(new Phrase(" :", FontChinese));
cella1.setHorizontalAlignment(Element.ALIGN_LEFT);
cella1.setLeading(30);
cella1.setColspan(4);
cella1.setBorder(0);
datatable.setDefaultCellBorder(0);
datatable.addCell(cella1);
Cell cella2 = new Cell(new Phrase(" :" + getNowTime(), FontChinese));//
cella2.setHorizontalAlignment(Element.ALIGN_LEFT);
cella2.setLeading(30);
cella2.setColspan(2);
cella1.setBorder(0);
datatable.addCell(cella2);
document.add(datatable);
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
// document
document.close();
// String pdfPath=file.getAbsolutePath()+"/"+pdfName;
// System.out.println(pdfPath);
//
// pdfPath=pdfPath.replaceAll("\\\\", "/");
return PDF_PATH;//
}
/**
* <p>Title: getResource</p>
* <p>Description: TODO</p>
* @param bean
* @param map
* @return
* @author zhangmeijia 2014-7-3 11:34:23
*/
private List<Map<String, Object>> getResource() {
List<User> uList = new ArrayList<User>();
for (int i = 0; i < 5; i++) {
User u = new User();
String iStr = new Integer(i).toString();
u.setId(iStr + "id");
u.setAge(iStr + "age");
u.setName(iStr + "name");
u.setClasses(iStr + "classes");
u.setGender(iStr + "gender");
u.setTel(iStr + "tel");
uList.add(u);
}
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (User u : uList) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", u.getId());
map.put("name", u.getName());
map.put("age", u.getAge());
map.put("classes", u.getClasses());
map.put("tel", u.getTel());
map.put("gender", u.getGender());
list.add(map);
}
return list;
}
//
public String getNowTime() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String now = formatter.format(new Date());
return now;
}
}
効果はtomcatプロジェクトの下のPDFフォルダの下に生産されたPDFファイルがあります.
図:
JSPページダウンロード.PDFファイルをダウンロードするには、パスを選択できます.
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*,java.io.*,java.text.* "%>
<%
//
request.setCharacterEncoding("utf-8");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String now = formatter.format(new Date());
String filename = now + ".pdf";
byte b[] = new byte[1024];
// output
OutputStream output = response.getOutputStream();
//
String path = request.getParameter("filepath");
// path=path.replaceAll("\\\\", "/");
//
File filePdf = new File(path + filename);
response.setContentType("application/pdf");
response.addHeader("Content-Disposition", "attachment; filename=" + filename);
FileInputStream in = new FileInputStream(filePdf);
int n;
while ((n = in.read(b)) != -1) {
output.write(b, 0, n);
}
output.flush();
in.close();
out.clear();
out = pageContext.pushBody();
%>