spring-boot中怎么实现一个PDF打印功能
发表于:2025-02-06 作者:千家信息网编辑
千家信息网最后更新 2025年02月06日,这篇文章将为大家详细讲解有关spring-boot中怎么实现一个PDF打印功能,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。1.导入jar(一定要注意
千家信息网最后更新 2025年02月06日spring-boot中怎么实现一个PDF打印功能
这篇文章将为大家详细讲解有关spring-boot中怎么实现一个PDF打印功能,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
1.导入jar(一定要注意版本,踩过很多坑)
com.itextpdf itextpdf 5.5.1 com.itextpdf itext-asian 5.2.0
2.工具类
package com.sungrow.sgframe.api.isolarapi.machineconfigservice.util;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 lombok.extern.log4j.Log4j2;import org.sg.tools.config.SungwsConfig;import org.sg.tools.util.UUIDUtil;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import java.util.concurrent.atomic.AtomicInteger;/** * @author shihaifeng * @date 2019-09-29 11:03 * @desc (PDF工具类) **/@Log4j2public class PDFUtil { private static Document document = null;// 建立一个Document对象 private static int maxWidth = 520; private static Font headfont;// 设置字体大小 private static Font keyfont;// 设置字体大小 private static Font textfont;// 设置字体大小 static { BaseFont bfChinese; try { bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); headfont = new Font(bfChinese, 10, Font.BOLD);// 设置字体大小 keyfont = new Font(bfChinese, 8, Font.BOLD);// 设置字体大小 textfont = new Font(bfChinese, 8, Font.NORMAL);// 设置字体大小 } catch (Exception e) { e.printStackTrace(); log.error(e.getMessage(), e); } } /** * 初始化文档 */ private static void initDocument(File file) { document = new Document();//每一次初始化一个document 不然会有问题 open()方法有问题 document.setPageSize(PageSize.A4);// 设置页面大小 try { FileOutputStream fileOutputStream = new FileOutputStream(file); PdfWriter.getInstance(document, fileOutputStream) .setViewerPreferences(PdfWriter.PageModeUseThumbs); document.open(); } catch (Exception e) { e.printStackTrace(); log.error(e.getMessage(), e); } } /** * 创建table * * @param colNumber * @return */ private static PdfPTable createTable(int colNumber) { PdfPTable table = new PdfPTable(colNumber); try { table.setTotalWidth(maxWidth); table.setLockedWidth(true); table.setHorizontalAlignment(Element.ALIGN_CENTER); table.getDefaultCell().setBorder(1); } catch (Exception e) { e.printStackTrace(); log.error(e.getMessage(), e); } return table; } /** * 创建table * * @param widths * @return */ private PdfPTable createTable(float[] widths) { PdfPTable table = new PdfPTable(widths); try { table.setTotalWidth(maxWidth); table.setLockedWidth(true); table.setHorizontalAlignment(Element.ALIGN_CENTER); table.getDefaultCell().setBorder(1); } catch (Exception e) { e.printStackTrace(); log.error(e.getMessage(), e); } return table; } /** * 创建 空table * * @return */ private static PdfPCell createBlankTable() { PdfPCell cell = new PdfPCell(); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); Paragraph paragraph = new Paragraph("", getPdfChineseFont()); cell.setPhrase(paragraph); return cell; } /** * 创建列 * * @param value * @param font * @param align * @return */ private PdfPCell createCell(String value, Font font, int align) { PdfPCell cell = new PdfPCell(); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(align); Paragraph paragraph = new Paragraph(String.valueOf(value), getPdfChineseFont()); cell.setPhrase(paragraph); return cell; } /** * 创建列 * * @param value * @param font * @return */ private static PdfPCell createHeadCell(String value, Font font) { PdfPCell cell = new PdfPCell(); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setBackgroundColor(new BaseColor(22022022)); cell.setFixedHeight(25.0f); Font headFont = getPdfChineseFont(); headFont.setColor(new BaseColor(0xff0000)); headFont.setSize(14); headFont.setStyle("bold"); Paragraph paragraph = new Paragraph(String.valueOf(value), headFont); cell.setPhrase(paragraph); return cell; } /** * 创建列 * * @param value * @param font * @param rowSpan 占多列 * @param colspan 占多行 * @return */ private static PdfPCell createCell(String value, Font font, int rowSpan, int colspan) { PdfPCell cell = new PdfPCell(); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setRowspan(rowSpan); cell.setColspan(colspan); Paragraph paragraph = new Paragraph(String.valueOf(value), getPdfChineseFont()); cell.setPhrase(paragraph); return cell; } /** * 创建列 * * @param value * @param font * @param align * @param colspan * @param boderFlag * @return */ private static PdfPCell createCell(String value, Font font, int align, int colspan, boolean boderFlag) { PdfPCell cell = new PdfPCell(); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(align); cell.setColspan(colspan); Paragraph paragraph = new Paragraph(String.valueOf(value), getPdfChineseFont()); cell.setPhrase(paragraph); cell.setPadding(3.0f); if (!boderFlag) { cell.setBorder(0); cell.setPaddingTop(15.0f); cell.setPaddingBottom(8.0f); } return cell; } /** * 增加中文显示 * * @return */ private static Font getPdfChineseFont() { BaseFont bfChinese = null; try { bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); } catch (DocumentException e) { e.printStackTrace(); log.error(e.getMessage(), e); } catch (IOException e) { e.printStackTrace(); log.error(e.getMessage(), e); } Font fontChinese = new Font(bfChinese, 12, Font.NORMAL); return fontChinese; } /** * 创建pdf */ public static String createPDF(Maptittle, List
关于spring-boot中怎么实现一个PDF打印功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
大小
字体
内容
数据
标题
功能
三个
工具
文件
文章
更多
知识
篇文章
部件
问题
不错
成功
一行
信息
单元
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
软件开发三大基础平台
交通网络安全手抄报内容
深圳网络安全整改
软件开发视频演示要求
工商银行手机银行基金服务器异常
网络安全技术培训班
临湘职高安装软件开发学校
软件开发宣传部职责
蛋白结构数据库
互联网科技公司的特性
可界面选择任意数据库表
潮州租房网络安全
河北2018网络安全题库
大数据网络安全专业排名
基于构建的软件开发方法
pgsql为什么是对象型数据库
教务系统数据库目的
网络安全独立站
网络安全保护能力包括
运营商为什么不用浪潮服务器
vultr服务器配置
wingate代理服务器
杭州市网络技术公司电话
十通数据库
怎么查看dns服务器的ip
数据库的查询计划
暗黑3服务器
摩托日记软件开发
国内icloud服务器在哪
服务器功耗低u