java 生成pdf 并通过浏览器下载

java 生成pdf 并通过浏览器下载

  1. 拉取依赖
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.9</version>
</dependency>

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

直接上代码

package com.tcl.tof.order.obg.ohs.remote.controller;

import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import org.springframework.core.io.ClassPathResource;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;

/**
 * @Author ex_chao.xie
 * @Date 2023-05-04 19:22
 * @Version 1.0
 */
public class PdfUtil{

    public void writePDF(HttpServletResponse response) {
        try {
            OutputStream outputStream = response.getOutputStream();

            //模板路径,放到项目里用这个ClassPathResource
            ClassPathResource classPathResource = new ClassPathResource("templates/test.pdf");
            InputStream inputStream = classPathResource.getInputStream();

            PdfReader reader = new PdfReader(inputStream);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            PdfStamper ps = new PdfStamper(reader, bos);

            //设置字体
            final BaseFont font = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            ArrayList<BaseFont> fontList = new ArrayList<>();
            fontList.add(font);


            ps.setFormFlattening(true);
            ps.close();
            //*******************填充编辑好后的pdf**************
            reader = new PdfReader(bos.toByteArray());
            Rectangle pageSize = reader.getPageSize(1);
            Document document = new Document(pageSize);
            PdfWriter writer = PdfWriter.getInstance(document, outputStream);
            // 打开文档
            document.open();
            PdfContentByte cbUnder = writer.getDirectContentUnder();

            PdfImportedPage pageTemplate = writer.getImportedPage(reader, 1);
            cbUnder.addTemplate(pageTemplate, 0, 0);
            for (int i=0;i<10;i++) {
                createTable5(writer, "第"+i+"页");
                // 新开一页pdf
                document.newPage();
             }

            response.setContentType("application/pdf");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=report.pdf");
            outputStream.write(bos.toByteArray());
            document.close();
            outputStream.flush();
            outputStream.close();
            bos.close();
        } catch (Exception e) {

        }

    }

    public void createTable5(PdfWriter writer, String i) {
        try{
            // 设置一个50长度单元格 //如果是表格可以 new PdfPTable(new float[]{50,50})
            PdfPTable table = new PdfPTable(50);
            table.setTotalWidth(50); // 宽度
            PdfPCell heardCell = new PdfPCell();
            heardCell.setPhrase(new Phrase(i, getTextFont()));
            heardCell.setMinimumHeight(50); // 高度
            table.addCell(heardCell);
            // 设置坐标
            table.writeSelectedRows(0, -1, 1, 100, writer.getDirectContentUnder());
        }catch (Exception e){

        }
    }

    // 设置字体
    public Font getTextFont(){
        try{
            BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            Font  textFont = new Font(baseFont, 10, Font.NORMAL);
            return textFont;
        }catch (Exception e){

        }
        return null;
    }
}

你可能感兴趣的:(servlet,java,开发语言)