package com.svse.servlet;
import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.rtf.RtfWriter2;
import com.svse.dao.BookInfoDao;
import com.svse.vo.BookInfo;
public class PdfServlet extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String method = request.getParameter("method");
if (method.equals("show")) {
this.show(request, response);
}
if (method.equals("pdf")) {
this.createPdf(request, response);
}
}
/**
* 获得所有图书信息
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void show(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
BookInfoDao dao = new BookInfoDao();
List<BookInfo> list = dao.findAll();
request.setAttribute("list", list);
request.getRequestDispatcher("show.jsp").forward(request, response);
}
/**
* 动态生成pdf文件
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void createPdf(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
try {
Document document = new Document();
PdfWriter.getInstance(document, buffer);
// RtfWriter2.getInstance(document, buffer);
// 定义表格对象
Table table = new Table(5);
float[] widths = { 0.30f, 0.20f, 0.30f, 0.10f, 0.10f };
// 设置表格属性
table.setBorderWidth(1);
table.setBorderColor(new Color(0, 0, 0));
table.setPadding(0);
table.setSpacing(0);
// 设置列宽
table.setWidths(widths);
// 中文处理
BaseFont bfChinese = BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
Phrase ph = new Paragraph("图书信息", FontChinese);
Cell cell = new Cell(ph);
//跨5列
cell.setColspan(5);
//设置水平居中
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
Phrase p1 = new Phrase("图书名", FontChinese);
Phrase p2 = new Phrase("图书ISBN ", FontChinese);
Phrase p3 = new Phrase("图书作者", FontChinese);
Phrase p4 = new Phrase("图书价格", FontChinese);
Phrase p5 = new Phrase("图书数量", FontChinese);
//设置表头信息
Cell cell1 = new Cell(p1);
Cell cell2 = new Cell(p2);
Cell cell3 = new Cell(p3);
Cell cell4 = new Cell(p4);
Cell cell5 = new Cell(p5);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
table.addCell(cell4);
table.addCell(cell5);
//获取所有图书信息,并显示
BookInfoDao dao = new BookInfoDao();
List<BookInfo> list = dao.findAll();
for (int i = 0; i < list.size(); i++) {
BookInfo book = list.get(i);
Phrase p6 = new Phrase(book.getBookName(), FontChinese);
Cell c1 = new Cell(p6);
Phrase p7 = new Phrase(book.getIsbn());
Cell c2 = new Cell(p7);
Phrase p8 = new Phrase(book.getAuthor(), FontChinese);
Cell c3 = new Cell(p8);
Phrase p9 = new Phrase(book.getPrice());
Cell c4 = new Cell(p9);
Phrase p10 = new Phrase(book.getStock());
Cell c5 = new Cell(p10);
table.addCell(c1);
table.addCell(c2);
table.addCell(c3);
table.addCell(c4);
table.addCell(c5);
}
document.open();
document.add(table);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment;Filename=" + "aa"
+ ".pdf");
response.setContentLength(buffer.size());
ServletOutputStream out = response.getOutputStream();
buffer.writeTo(out);
out.flush();
out.close();
}
}
评论1
最新资源