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>

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

相关推荐
海梨花几秒前
【从零开始学习Redis】项目实战-黑马点评D2
java·数据库·redis·后端·缓存
共享家95271 分钟前
linux-高级IO(上)
java·linux·服务器
Sammyyyyy8 分钟前
2025年,Javascript后端应该用 Bun、Node.js 还是 Deno?
开发语言·javascript·node.js
橘子郡1238 分钟前
观察者模式和发布订阅模式对比,Java示例
java
指针满天飞9 分钟前
Collections.synchronizedList是如何将List变为线程安全的
java·数据结构·list
Java技术小馆10 分钟前
重构 Controller 的 7 个黄金法则
java·后端·面试
金銀銅鐵30 分钟前
[Java] 以 IntStream 为例,浅析 Stream 的实现
java·后端
William一直在路上40 分钟前
Python数据类型转换详解:从基础到实践
开发语言·python
看到我,请让我去学习1 小时前
Qt— 布局综合项目(Splitter,Stacked,Dock)
开发语言·qt
GUET_一路向前2 小时前
【C语言防御性编程】if条件常量在前,变量在后
c语言·开发语言·if-else·防御性编程