Java Servlet PDF文件生成


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();
        }
    }
}


Servlet3.1规范(最终版) JavaTM Servlet 规范 版本 3.1(最终版) Shing Wai Chan Rajiv Mordani [作者] 穆茂强 张开涛 [译者] 2012年6月翻译 2013年7月修订 目录 前言 ............................... 3 其他资料 ............................................................................................................................................................ 3 谁应该读此规范 ................................................................................................................................................ 3 API规范 ............................................................................................................................................................3 其他的 Java 平台规范 ....................................................................................................................................... 3 其他重要参考资料 ............................................................................................................................................ 4 提供反馈 ............................................................................................................................................................ 4 专家组成员 ........................................................................................................................................................ 5 答谢 .................................................................................................................................................................... 5 目录 ............................... 6 概览 .............................. 13 1.1 1.2 1.3 1.4 1.5 1.6 什么是 Servlet? .................................................................................................................................. 13 什么是Servlet容器?...............................................................................................................................13 例子...........................................................................................................................................................13 Serv
servlet课件Servlet课件..........................................................................................1 第一章 Servlet基础部分 .....................................................................2 1.1 Servlet简介 .........................................................................2 1.2 Web服务器...........................................................................2 1、介绍...............................................................................2 2、Tomcat的配置..................................................................2 3、Tomcat服务器具体介绍 .......................................................3 4、jar命令介绍......................................................................3 1.3 Servlet的配置文件 .................................................................3 1.4 Servlet的调用过程 .................................................................4 第二章 Servlet核心...........................................................................5 2.1 整体介绍 .............................................................................5 2.2 Servlet的接口.......................................................................5 2.3 servlet的生命周期..................................................................6 1、Servlet的生命周期分为四个阶段 ............................................6 2、在web.xml文件中配置Servlet对象的启动时就创建......................7 3、通过<init-param>标签来配置初始化参数.................................7 2.4 Get请求和Post请求 ................................................................7 2.5 ServletRequest ....................................................................8 request.getInputStream()方法................................................8 request.getReader()方法 .......................................................8 request.getParameter(String name)方法,...............................8 request.getContextPath()方法 ................................................8 等等。。。。。。。。。。。。。。。。。。。。
Java生成PDF文件并下载,通常可以使用第三方库,如iText、Apache PDFBox等。这里我将简单概述使用iText库的一个基本示例: 首先,你需要添加iText库到你的项目中。如果你使用Maven,可以在pom.xml文件中加入依赖: ```xml <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.13</version> <!-- 根据最新版本替换 --> </dependency> ``` 然后,你可以编写一个简单的Java方法来创建PDF并下载: ```java import com.itextpdf.text.Document; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; import javax.servlet.http.HttpServletResponse; import java.io.FileOutputStream; import java.io.IOException; public void generatePdfAndDownload(HttpServletResponse response) { try { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream("path_to_your_pdf_file.pdf")); // 替换为实际保存路径 document.open(); // 打开文档 document.add(new Paragraph("Hello, this is a sample PDF generated with iText!")); // 添加内容 // 设置响应头,告诉浏览器这是一个PDF下载 response.setContentType("application/pdf"); response.setHeader("Content-Disposition", "attachment; filename=sample.pdf"); // 写入PDF并关闭 document.close(); response.flushBuffer(); // 确保立即发送到客户端 } catch (IOException e) { e.printStackTrace(); } } ``` 在这个例子中,`generatePdfAndDownload`方法接收一个`HttpServletResponse`作为参数,用于设置HTTP响应以触发下载。当你调用这个方法时,用户会看到一个弹出框请求他们保存PDF文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值