Java Servlet PDF文件生成

来源:互联网 发布:2k17乔丹捏脸详细数据 编辑:程序博客网 时间:2024/04/29 20:14


public class PdfServlet extends HttpServlet {


public CertServlet() {
    }

    public void init(ServletConfig config) throws ServletException {
  
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

          HashMap<String, String> outpdf = new HashMap<String, String>();    //用来存储pdf相关文字信息

          outpdf.put("imgpath", imgpath);

          outpdf.put("note", note);   

          ......

         outPDF(response, imgpath, outpdf);

   }


public void outPDF(HttpServletResponse response, String imgpath, HashMap<String, String> outpdf,) {
        response.setContentType("application/pdf");
        if (isDownload == true) {
            response.setHeader("Content-disposition", "attachment; filename=" + "certificate.pdf");
        }
        try {
            CertPDF pdf = new CertPDF(wizbini);
            OutputStream os = response.getOutputStream();
            pdf.CertTemplate(os, response, imgpath, outpdf, isDownload, isShowNo);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}




package com.cw.wizbank.cert;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;

import Word.Style;

import com.cw.wizbank.config.WizbiniLoader;
import com.cw.wizbank.util.cwUtils;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;

public class CertPDF {
    private WizbiniLoader wizbini = null;

    public CertPDF(WizbiniLoader wizbini) {
        this.wizbini = wizbini;
    }

    public void CertTemplate(OutputStream os, HttpServletResponse response, String bg_url,
            HashMap<String, String> outpdf, boolean isDownload, boolean isShowNo) throws DocumentException,
            IOException {
        try {
            int width = 900;
            int height = 650;
            File file = new File(bg_url);
            if (file.exists()) {
                BufferedImage Bi = ImageIO.read(file);
                width = Bi.getWidth();
                height = Bi.getHeight();
            }
            Rectangle pSize = new Rectangle(width, height);   //A4纸张大小 595 842
            Document doc = new Document(pSize, 0, 0, 0, 0);
            PdfWriter.getInstance(doc, os);
            String font = this.wizbini.getWebInfRoot() + cwUtils.SLASH + "fonts" + cwUtils.SLASH
                    + "SURSONG.TTF";
            BaseFont bfChinese = BaseFont.createFont(font, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            Font Fontcerttitle = new Font(bfChinese, 35, Font.BOLD);//
            Font FontChinese = new Font(bfChinese, 28, Font.BOLD);//
            Font font_state = new Font(bfChinese, 22, Font.ITALIC);// 状态栏(小字体风格)
            Font font_date = new Font(bfChinese, 22, Font.BOLD);// 日期栏
            Font font_core = new Font(bfChinese, 18, Font.BOLD);//
            Font font_content = new Font(bfChinese, 22, Font.BOLD);
            font_content.setStyle(Style.clsid);
            doc.open();
            Image image = Image.getInstance(bg_url);
            image.setAlignment(Image.UNDERLYING);
            
            // image.scalePercent(65);
            doc.add(image);
            // 证书编号
            Paragraph context1 = null;
            // 证书名称
            Paragraph context2 = null;

           
                // 证书编号
                context1 = new Paragraph(outpdf.get("certcore"), font_core);
                // 证书名称
                context2 = new Paragraph(outpdf.get("certtitle"), Fontcerttitle);

            
            // 证书编号
            if(isShowNo){
                context1.setSpacingBefore(120); // 离上一段落空的行数
                context1.setAlignment(Element.ALIGN_RIGHT);
                context1.setIndentationRight(70); // 距离右边的距离
                doc.add(context1);
            }

            // 证书名称
            context2.setAlignment(Element.ALIGN_CENTER);
            context2.setSpacingBefore(40); // 离上一段落空的行数
            context2.setIndentationLeft(70);
            context2.setIndentationRight(70);
            doc.add(context2);

       
            
            // 内容
            String note = outpdf.get("note");
            Paragraph contextnote = new Paragraph(note.substring(0, note.length()), font_content);
            contextnote.setSpacingBefore(25);        //上边空白距离       padding-top
            contextnote.setFirstLineIndent(50);        //首行缩进        text-indent
            contextnote.setIndentationLeft(60);        //左边空白距离     padding-left
            contextnote.setIndentationRight(60);    //右边空白距离    padding-right
            doc.add(contextnote);

            doc.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


0 0
原创粉丝点击