java生成pdf表格并支持下载可选另存为

java 复制代码
package com.qf.staff_manage.controller;

import com.itextpdf.kernel.color.Color;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.TextAlignment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("candidate")
public class dd {
    @RequestMapping("/dd")
    public void createpdf(HttpServletResponse response) throws IOException {
        try {
            response.reset();
            String fileName = new String(("信用承诺失信行为查询.pdf").getBytes("gb2312"), "ISO8859-1");
            response.setContentType("application/pdf");
            response.setHeader("Content-disposition", "attachment; filename=" + fileName);

            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            PdfWriter writer = new PdfWriter(baos);

            PdfDocument pdf = new PdfDocument(writer);
            Document document = new Document(pdf, PageSize.A4);
            // 创建字体
            PdfFont font = PdfFontFactory.createFont("STSong-Light", "UniGB-UCS2-H", true); // 设置加粗和字体大小
            PdfFont font1 = PdfFontFactory.createFont("STSong-Light", "UniGB-UCS2-H", true);
            // 创建表格
            Table table = new Table(5); // 3列的表格
            //添加标题

            table.addHeaderCell(new Cell(1, 5).add(new Paragraph("信用承诺失信行为查询").setFont(font).setTextAlignment(TextAlignment.CENTER)).setBold().setFontSize(18));
            // 添加表头
            table.addCell(new Cell().add(new Paragraph("序号").setFont(font).setTextAlignment(TextAlignment.CENTER)).setBold());
            table.addCell(new Cell().add(new Paragraph("单位名称").setFont(font).setTextAlignment(TextAlignment.CENTER)).setBold());
            table.addCell(new Cell().add(new Paragraph("是/否信用承诺失信行为").setFont(font).setTextAlignment(TextAlignment.CENTER)).setBold());
            table.addCell(new Cell().add(new Paragraph("查询人").setFont(font).setTextAlignment(TextAlignment.CENTER)).setBold());
            table.addCell(new Cell().add(new Paragraph("查询时间").setFont(font).setTextAlignment(TextAlignment.CENTER)).setBold());
            //查询出表格数据

            // 在文档中添加表格
            document.add(table);
            // 关闭文档
            document.close();
            byte[] pdfBytes = baos.toByteArray();
            // 将PDF内容写入响应输出流
            response.setContentLength(pdfBytes.length);
            response.getOutputStream().write(pdfBytes);
            response.getOutputStream().flush();
            response.getOutputStream().close();
            writer.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

直接复制以上代码即可完成,但是也要引入一些依赖

java 复制代码
<dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>kernel</artifactId>
            <version>7.0.3</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>io</artifactId>
            <version>7.0.3</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>layout</artifactId>
            <version>7.0.3</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>font-asian</artifactId>
            <version>7.0.3</version>
        </dependency>

引入以后修修补补增增改改就可以了

相关推荐
傻乐u兔25 分钟前
C语言进阶————指针4
c语言·开发语言
大模型玩家七七27 分钟前
基于语义切分 vs 基于结构切分的实际差异
java·开发语言·数据库·安全·batch
历程里程碑27 分钟前
Linux22 文件系统
linux·运维·c语言·开发语言·数据结构·c++·算法
牛奔2 小时前
Go 如何避免频繁抢占?
开发语言·后端·golang
寻星探路5 小时前
【深度长文】万字攻克网络原理:从 HTTP 报文解构到 HTTPS 终极加密逻辑
java·开发语言·网络·python·http·ai·https
lly2024067 小时前
Bootstrap 警告框
开发语言
2601_949146538 小时前
C语言语音通知接口接入教程:如何使用C语言直接调用语音预警API
c语言·开发语言
曹牧8 小时前
Spring Boot:如何测试Java Controller中的POST请求?
java·开发语言
KYGALYX8 小时前
服务异步通信
开发语言·后端·微服务·ruby