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

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);
        }
    }
}

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


            com.itextpdf
            kernel
            7.0.3
        
        
            com.itextpdf
            io
            7.0.3
        
        
            com.itextpdf
            layout
            7.0.3
        
        
            com.itextpdf
            font-asian
            7.0.3
        

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

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