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>

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

相关推荐
熟悉的新风景41 分钟前
springboot项目或其他项目使用@Test测试项目接口配置-spring-boot-starter-test
java·spring boot·后端
心平愈三千疾41 分钟前
学习秒杀系统-实现秒杀功能(商品列表,商品详情,基本秒杀功能实现,订单详情)
java·分布式·学习
玩代码1 小时前
备忘录设计模式
java·开发语言·设计模式·备忘录设计模式
BUTCHER52 小时前
Docker镜像使用
java·docker·容器
岁忧2 小时前
(nice!!!)(LeetCode 面试经典 150 题 ) 30. 串联所有单词的子串 (哈希表+字符串+滑动窗口)
java·c++·leetcode·面试·go·散列表
技术猿188702783512 小时前
实现“micro 关键字搜索全覆盖商品”并通过 API 接口提供实时数据(一个方法)
开发语言·网络·python·深度学习·测试工具
放飞自我的Coder2 小时前
【colab 使用uv创建一个新的python版本运行】
开发语言·python·uv
艾莉丝努力练剑3 小时前
【数据结构与算法】数据结构初阶:详解顺序表和链表(四)——单链表(下)
c语言·开发语言·数据结构·学习·算法·链表
zyhomepage3 小时前
科技的成就(六十九)
开发语言·网络·人工智能·科技·内容运营
珊瑚里的鱼3 小时前
第十三讲 | map和set的使用
开发语言·c++·笔记·visualstudio·visual studio