/**
* 生成PDF
* @author momo
* @since 2018-8-6
* @return 字节数组输出流转成的字符串
*/
public String generatePDF() {
byte[] result=null;
ByteArrayOutputStream baos = null;
Document doc =null;
try {
Rectangle rectPageSize = new Rectangle(PageSize.A4);
doc = new Document(rectPageSize);
baos = new ByteArrayOutputStream();
PdfWriter.getInstance(doc,baos);
doc.open();
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font titleFont = new Font(bfChinese, 26, Font.BOLD);
Paragraph title = new Paragraph("通知书", titleFont);
title.setAlignment(Element.ALIGN_CENTER);
doc.add(title);
Font smallTitleFont = new Font(bfChinese, 15, Font.NORMAL);
Paragraph smallTitle = new Paragraph("通 〔A123〕 号", smallTitleFont);
smallTitle.setAlignment(Element.ALIGN_CENTER);
smallTitle. setLeading(40f);
doc.add(smallTitle);
Font enterpriseFont = new Font(bfChinese, 15, Font.NORMAL);
Paragraph enterprise = new Paragraph("", enterpriseFont);
enterprise. setLeading(60f);
doc.add(enterprise);
Chunk qymc = new Chunk(" FDSAFASDFA ", enterpriseFont);
qymc.setTextRise(1);
qymc.setUnderline(0.2f, -2f);
doc.add(qymc);
Chunk nsrsbh = new Chunk(":12312421421)", enterpriseFont);
doc.add(nsrsbh);
Font syFont = new Font(bfChinese, 15, Font.NORMAL);
Paragraph sy = new Paragraph("事由: ", syFont);
sy.setIndentationLeft(24);
sy. setLeading(30f);
doc.add(sy);
Font yjFont = new Font(bfChinese, 15, Font.NORMAL);
Paragraph yj = new Paragraph("依据: ", yjFont);
yj.setIndentationLeft(24);
doc.add(yj);
Font tznrFont = new Font(bfChinese, 15, Font.NORMAL);
Paragraph tznr = new Paragraph("通知内容: ",tznrFont);
tznr.setIndentationLeft(24);
doc.add(tznr);
Font qzFont = new Font(bfChinese, 15, Font.NORMAL);
Paragraph qz = new Paragraph("(签章)", qzFont);
qz.setIndentationLeft(300);
qz. setLeading(100f);
doc.add(qz);
doc.newPage();
PdfPTable table=new PdfPTable(8);
table.setTotalWidth(new float[]{100,350,350});
Font tableTitleFont=new Font(bfChinese,18,Font.BOLD);
Font tableTitle2Font=new Font(bfChinese,12,Font.BOLD);
Font titleCellFont=new Font(bfChinese,12,Font.NORMAL);
table.addCell(mergeRow("清单",tableTitleFont));
table.addCell(getPDFCell("序号",tableTitle2Font));
table.addCell(getPDFCell("标题一",tableTitle2Font));
table.addCell(getPDFCell("标题二",tableTitle2Font));
table.addCell(getPDFCell("标题三",tableTitle2Font));
for (int i = 0; i < dataList.size(); i++) {
table.addCell(getPDFCell(i,tableTitle2Font));
table.addCell(getPDFCell("数据一"+i,tableTitle2Font));
table.addCell(getPDFCell("数据二"+i,tableTitle2Font));
table.addCell(getPDFCell("数据三"+i,tableTitle2Font));
}
table.setWidthPercentage(110);
doc.add(table);
if(doc != null){
doc.close();
}
String result =new String(baos.toByteArray(), "ISO-8859-1");
result = java.net.URLEncoder.encode(result, "ISO-8859-1");
}catch(Exception e) {
log.error("PDF异常", e);
}finally{
if(baos != null){
try {
baos.close();
} catch (IOException e) {
log.error("PDF异常", e);
}
}
}
return result;
}
/**合并行的静态函数
*
* @author momo
* @since 2018-8-7
* @param str
* @param font
* @return
*/
public static PdfPCell mergeRow(String str,Font font) {
PdfPCell cell=new PdfPCell(new Paragraph(str,font));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setMinimumHeight(40);
cell.setColspan(8);
return cell;
}
/**
* 获取指定内容与字体的单元格
*
* @author momo
* @since 2018-8-6
* @param string
* @param font
* @return
*/
public static PdfPCell getPDFCell(String string, Font font) {
PdfPCell cell=new PdfPCell(new Paragraph(string,font));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setMinimumHeight(30);
return cell;
}
二、 Servlet下载
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String result= generatePDF();
result = java.net.URLDecoder.decode(result, "ISO-8859-1");
ByteArrayInputStream inStream = new ByteArrayInputStream(
result.getBytes("ISO-8859-1"));
response.setContentType("bin");
response.addHeader("Content-Disposition","attachment; filename=\"" + fileName+ ".zip\"");
byte[] b = new byte[2048];
int len;
try {
while ((len = inStream.read(b)) > 0)
response.getOutputStream().write(b, 0, len);
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}