/**
iTextAsian.jar
itext-2.0.1.jar
*/
package cn.gov.zjport.webapp.ata.pdf;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
//import freemarker.template.utility.Coordinate;
//import freemarker.template.utility.ParsePrintTemplate;
/**
*
* @author pubinfo
* @version
*/
public class PdfProducerServlet extends HttpServlet {
/** Processes requests for both HTTP GET
and POST
methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*
ParsePrintTemplate parsept = new ParsePrintTemplate();
HttpSession session = request.getSession();
Map printcontent = (Map)session.getAttribute("printdata");
String templatename =(String)session.getAttribute("templatename");
ArrayList itemlist = parsept.getParameter(templatename);
*/
Rectangle pageSize = null;
float width =21 *72f/2.54f;
float height = 29.7f*72f/2.54f;
pageSize = new Rectangle(width, height);
Document document = new Document(pageSize, 36,36,36,36);
ByteArrayOutputStream ba = new ByteArrayOutputStream();
ServletOutputStream servletOut = response.getOutputStream();
try {
PdfWriter writer =PdfWriter.getInstance(document, ba);
document.open();
PdfContentByte cb = writer.getDirectContent();
cb.beginText();
//汉字
//BaseFont bfComic = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
BaseFont bfComic = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.setFontAndSize(bfComic, 9);
float x, y;
x = 2* 72f/2.5f;
y = height - 2* 72f/2.5f;
cb.showTextAligned(PdfContentByte.ALIGN_LEFT, "中国", x, y, 0);
cb.endText();
ba.writeTo(servletOut);
} catch(DocumentException de) {
de.printStackTrace();
System.err.println("A Document error:" +de.getMessage());
}
document.close();
response.setContentType("application/pdf");
response.setContentLength(ba.size());
ServletOutputStream out = response.getOutputStream();
ba.writeTo(out);
out.flush();
}
/** Handles the HTTP GET
method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/** Handles the HTTP POST
method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
public String getServletInfo() {
return "";
}
}
分享到:
2007-05-31 14:20
浏览 1666
评论