套打-生成pdf

/**

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 <code>GET</code> and <code>POST</code> 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 <code>GET</code> 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 <code>POST</code> 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 "";
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值