java生成PDF,并下载到本地

1、首先要写一个PDF工具类,以及相关工具 
2、PDF所需jar包 
iText是一种生成PDF报表的Java组件 
freemarker是基于模板来生成文本输出

    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itext-asian</artifactId>
        <version>5.2.0</version>
    </dependency>

    <dependency>
        <groupId>com.lowagie</groupId>
        <artifactId>itext</artifactId>
        <version>2.1.7</version>
    </dependency>

    <dependency>
        <groupId>org.freemarker</groupId>
        <artifactId>freemarker</artifactId>
        <version>2.3.23</version>
    </dependency>

 

3、需要使用Adobe Acrobat pro软件把要生成的模板转换为PDF格式 
打开Adobe Acrobat pro,打开模板,选择 |—— 准备表单 ,它会自动检测并命名表单域,然后保存为pdf格式即可

PDF工具类

public class PDFTemplet {
    private String templatePdfPath;
    private String targetPdfpath;
    private ServiceOrder order ;

    public PDFTemplet() {
    }

public void PDFTemplet(File file,String basePath)thows Exception{
/*模板路径*/
      PdfReader reader = new PdfReader(templatePdfPath);
ByteArrayOutputStream bos=new ByteArrayOutputStream();
/* 读取*/
PdfStamper pdfStamper= new PdfStamper(reader,bos);
/*使用中文字体*/
BaseFont baseFont=BaseFont.createFont(basePath+"WEB-INF/static/SIMHEI.TTF",BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
ArrayList<BaseFont> fontList=new ArrayList<>();
fontList.add(baseFont);
AcroFields  s=pdfStamper.getAcroFields();
s.setSubstitutionFonts(fontList);

/*需要注意的是  setField的name和命名的表单域名字要一致*/
s.setField("enterpriseName",order.getEnerpriseName());
 s.setField("incubatorName",order.getIncubatorName());
        s.setField("recommend","");//孵化器推荐
        s.setField("contacts",order.getContacts());
        s.setField("phone",order.getPhone());
        s.setField("email",order.getEmail());
        s.setField("category ","");//服务类别
        s.setField("demand",order.getDemand());
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
        String createTime = formatter.format(order.getCreateTime());
        String updateTime = formatter.format(order.getUpdateTime());
        s.setField("createTime",createTime);
        s.setField("updateTime", updateTime);
ps.setFormFlattenning(true);
ps.close();
FileOutputStream fileSteam =new FileOutPutStream(file);
fos.write(bos.toByteArray);
fos.close();}


}

 

调用方法

 @RequestMapping(value ="downloadPdf", method = RequestMethod.GET)
    public String downloadPDF(@PathVariable("id") Integer id,HttpServletRequest request) throws Exception {
        ServiceOrder serviceOrder = serviceOrderService.getById(id);
        PDFTemplet pdfTT = new PDFTemplet();
        pdfTT.setOrder(serviceOrder);
        String basePath = request.getSession().getServletContext().getRealPath("/");
        String template = request.getSession().getServletContext().getRealPath("/") + "WEB-INF/static/excel/confirmation.pdf";

        pdfTT.setTemplatePdfPath(template);
        pdfTT.setTargetPdfpath("D:/企业服务确认单.pdf");
        pdfTT.setOrder(serviceOrder);

        File file = new File("D:/企业服务确认单.pdf");
        file.createNewFile();
        pdfTT.templetTicket(file,basePath);
        return "/master/serviceOrder/orderList";
    }
posted @ 2018-07-13 15:26  流星~泪雨  阅读(1726)  评论(0编辑  收藏  举报