springboot integra itextpdf para exportar pdf

springboot integra itextpdf para exportar pdf

1.dependencia de pom

Si usa otras versiones, puede obtener un error, se recomienda usar estas 2 versiones

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

2. Código de exportación

package com.email.demo.controller;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

@Api(value = "springboot导出PDF", tags = {
    
    "springboot导出PDF"})
@Slf4j
@Validated
@RestController
@RequestMapping("/pdf")
public class PDFController {
    
    

    SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd");
    String formatTimeStr = format.format(new Date());
    SimpleDateFormat format2 =  new SimpleDateFormat("yyyyMMddHHmmss");
    String formatTimeStr2 = format2.format(new Date());
    private Integer size = 20;

    @GetMapping("/generatePDF")
    public String generatePDF() throws Exception{
    
    

        // 生成文件位置
        String filename = "src/main/resources/pdf/" + formatTimeStr2 + ".pdf";
        Document document = new Document(PageSize.A4);  // 生成是A4纸
        PdfWriter.getInstance(document, new FileOutputStream(filename));
        document.addTitle("这是PDF标题");
        document.open();

        PdfPTable table = generateTemplate();
        document.add(table);
        document.close();
        log.info("生成PDF成功! 生成路径为:{}! 生成时间为:{}", filename, formatTimeStr2);

        return "生成PDF成功!";
    }

    /**
     * 生成模板内容
     * @return
     */
    public PdfPTable generateTemplate() throws Exception{
    
    

        Font font = new Font(BaseFont.createFont( "STSongStd-Light" ,"UniGB-UCS2-H",BaseFont.NOT_EMBEDDED));
        font.setSize(7);
        PdfPTable table = new PdfPTable(10);
        // PDF导出模板  第一行
        PdfPCell cell1 = new PdfPCell(new Phrase("PDF导出模板", font));
        cell1.setColspan(10);
        setStyle(cell1);
        table.addCell(cell1);

        // 申请单位公章: 第二行
        PdfPCell cell2 = new PdfPCell(new Phrase("申请单位公章:", font));
        cell2.setColspan(2);
        setStyle(cell2);
        table.addCell(cell2);

        // 空格 第二行
        PdfPCell cell3 = new PdfPCell(new Phrase(" "));
        cell3.setColspan(2);
        setStyle(cell3);
        table.addCell(cell3);

        // 空格 第二行
        PdfPCell cell4 = new PdfPCell(new Phrase(" "));
        cell4.setColspan(1);
        setStyle(cell4);
        table.addCell(cell4);

        // 申请日期: 第二行
        PdfPCell cell5 = new PdfPCell(new Phrase("申请日期:" + formatTimeStr, font));
        cell5.setColspan(2);
        setStyle(cell5);
        table.addCell(cell5);

        // 空格 第二行
        PdfPCell cell6 = new PdfPCell(new Phrase(" "));
        cell6.setColspan(1);
        setStyle(cell6);
        table.addCell(cell6);

        // 单位:元 第二行
        PdfPCell cell7 = new PdfPCell(new Phrase("单位:元", font));
        cell7.setColspan(2);
        setStyle(cell7);
        table.addCell(cell7);

        // 序号 第三,四行
        PdfPCell cell8 = new PdfPCell(new Phrase("序号", font));
        cell8.setColspan(1);    // 占几列
        cell8.setRowspan(2);    // 占几行
        setStyle(cell8);
        table.addCell(cell8);

        // 资金性质 第三,四行
        PdfPCell cell9 = new PdfPCell(new Phrase("资金性质", font));
        cell9.setColspan(1);    // 占几列
        cell9.setRowspan(2);    // 占几行
        setStyle(cell9);
        table.addCell(cell9);

        // 编码 第三,四行
        PdfPCell cell11 = new PdfPCell(new Phrase("编码", font));
        cell11.setColspan(1);    // 占几列
        cell11.setRowspan(2);    // 占几行
        setStyle(cell11);
        table.addCell(cell11);

        // 名称 第三,四行
        PdfPCell cell12 = new PdfPCell(new Phrase("名称", font));
        cell12.setColspan(1);    // 占几列
        cell12.setRowspan(2);    // 占几行
        setStyle(cell12);
        table.addCell(cell12);

        // 项目名称 第三,四行
        PdfPCell cell13 = new PdfPCell(new Phrase("项目名称", font));
        cell13.setColspan(1);    // 占几列
        cell13.setRowspan(2);    // 占几行
        setStyle(cell13);
        table.addCell(cell13);

        // 姓名 第三,四行
        PdfPCell cell14 = new PdfPCell(new Phrase("姓名", font));
        cell14.setColspan(1);    // 占几列
        cell14.setRowspan(2);    // 占几行
        setStyle(cell14);
        table.addCell(cell14);

        // 银行账号 第三,四行
        PdfPCell cell15 = new PdfPCell(new Phrase("银行账号", font));
        cell15.setColspan(1);    // 占几列
        cell15.setRowspan(2);    // 占几行
        setStyle(cell15);
        table.addCell(cell15);

        // 开户银行 第三,四行
        PdfPCell cell16 = new PdfPCell(new Phrase("开户银行", font));
        cell16.setColspan(1);    // 占几列
        cell16.setRowspan(2);    // 占几行
        setStyle(cell16);
        table.addCell(cell16);

        // 申请金额 第三,四行
        PdfPCell cell17 = new PdfPCell(new Phrase("申请金额", font));
        cell17.setColspan(1);    // 占几列
        cell17.setRowspan(2);    // 占几行
        setStyle(cell17);
        table.addCell(cell17);

        // 核定金额 第三,四行
        PdfPCell cell18 = new PdfPCell(new Phrase("核定金额", font));
        cell18.setColspan(1);    // 占几列
        cell18.setRowspan(2);    // 占几行
        setStyle(cell18);
        table.addCell(cell18);

        // 根据自己业务封装数据
        for (int i = 0; i < 8; i++) {
    
    
            for (int j = 0; j < 10; j++) {
    
    
                PdfPCell cell19 = new PdfPCell(new Phrase("坑位置"+i+j, font));
                cell19.setColspan(1);    // 占几列
                cell19.setRowspan(1);    // 占几行
                setStyle(cell19);
                table.addCell(cell19);
            }
        }

        return table;
    }

    /**
     * 设置样式
     * @param cell
     */
    private void setStyle(PdfPCell cell){
    
    
        cell.setFixedHeight(size);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置水平居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);//设置垂直居中
    }
}

3. Prueba

Use swagger aquí para probar el
servicio y visite swagger después de comenzar: http: // localhost: 2080 / swagger-ui.html

Inserte la descripción de la imagen aquí
¡La solicitud está bien!

Inserte la descripción de la imagen aquí
La consola también imprime los resultados

Inserte la descripción de la imagen aquí
El
Inserte la descripción de la imagen aquí
resultado de la exportación pdf también se genera en recursos . ¡La prueba está bien!

Bienvenidos los grandes para que dejen comentarios y aprendan juntos !!! Gracias !!!

===========================
Artículo original, reimpreso con la fuente.

Supongo que te gusta

Origin blog.csdn.net/dayonglove2018/article/details/106814133
Recomendado
Clasificación