导出文件:使用itext导出数据为PDF并添加文字与图片水印

}

}

}

}

导出数据为PDF

  • 配置文件

server:

port: 8080

spring配置

spring:

datasource:

driver-class-name: com.mysql.cj.jdbc.Driver

url: jdbc:mysql://localhost:3306/standard-core?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8

username: root

password: root

mybatisplus配置

mybatis-plus:

搜索指定包别名

typeAliasesPackage: com.heartsuit.domain

配置mapper的扫描,找到所有的mapper.xml映射文件

mapper-locations: classpath:mapper/**/*.xml

configuration:

map-underscore-to-camel-case: true

log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

  • 核心服务

/**

  • @Author Heartsuit

  • @Date 2022-01-02

*/

@Service

public class StdCommitteeServiceImpl extends ServiceImpl<StdCommitteeMapper, StdCommittee> implements IStdCommitteeService {

@Autowired

private IStdCommitteeSecretariatService stdCommitteeSecretariatService;

@Autowired

private IStdCommitteeSecretariatStaffService stdCommitteeSecretariatStaffService;

@Autowired

private IStdCommitteeBranchService stdCommitteeBranchService;

private void buildCommittee(StdCommittee stdCommittee) {

// one 2 one

StdCommitteeSecretariat stdCommitteeSecretariat = stdCommitteeSecretariatService.getOne(new QueryWrapper()

.lambda().eq(StdCommitteeSecretariat::getCommitteeId, stdCommittee.getId()));

// one 2 many

List staffs = stdCommitteeSecretariatStaffService.list(new QueryWrapper()

.lambda().eq(StdCommitteeSecretariatStaff::getCommitteeSecretariatId, stdCommitteeSecretariat.getId()));

stdCommitteeSecretariat.setStdCommitteeSecretariatStaffs(staffs);

stdCommittee.setStdCommitteeSecretariat(stdCommitteeSecretariat);

// one 2 many

List branches = stdCommitteeBranchService.list(new QueryWrapper()

.lambda().eq(StdCommitteeBranch::getCommitteeId, stdCommittee.getId()));

stdCommittee.setStdCommitteeBranches(branches);

}

@Override

public void generatePdf(StdCommittee stdCommittee, OutputStream outputStream) {

buildCommittee(stdCommittee);

Document document = new Document();

try {

PdfWriter.getInstance(document, outputStream);

document.open();

// 解决中文不显示问题

BaseFont bfChinese = BaseFont.createFont(“STSong-Light”, “UniGB-UCS2-H”, BaseFont.NOT_EMBEDDED);

// 标题加粗

Font fontChina18 = new Font(bfChinese, PDFConstant.FONT_SIZE_18, Font.BOLD);

Font fontChina15 = new Font(bfChinese, PDFConstant.FONT_SIZE_15, Font.BOLD);

Font fontChina10 = new Font(bfChinese, PDFConstant.FONT_SIZE_10);

// 获取pdf头文件信息

Map<String, String> headInfo = new HashMap<>();

headInfo.put(“firstHeadInfo”, “标准化技术委员会登记表”);

// 空格

Paragraph blank1 = new Paragraph(" ", new Font(bfChinese, PDFConstant.FONT_SIZE_5));

Paragraph firstTitle = new Paragraph(headInfo.get(“firstHeadInfo”), fontChina18);

firstTitle.setAlignment(Element.ALIGN_CENTER);// 居中

document.add(firstTitle);

// 添加空格

document.add(blank1);

// 3 创建表格

PdfPTable table = new PdfPTable(PDFConstant.TABLE_COLUMN_NUMBER_8);// 表格总共几列

table.setWidthPercentage(PDFConstant.TABLE_WIDTH_PERCENTAGE);// 表格宽度为100%

PdfUtil.addTableCell(table, “名称”, fontChina10, false, false, 0, 0);

PdfUtil.addTableCell(table, stdCommittee.getName(), fontChina10, true, false, 3, 0);//跨三列

PdfUtil.addTableCell(table, “编号”, fontChina10, false, false, 0, 0);

PdfUtil.addTableCell(table, stdCommittee.getCode(), fontChina10, true, false, 3, 0);//跨三列

PdfUtil.addTableCell(table, “本届是第几届”, fontChina10, false, false, 0, 0);

PdfUtil.addTableCell(table, stdCommittee.getNumberSession().toString(), fontChina10, true, false, 3, 0);//跨三列

PdfUtil.addTableCell(table, “本届成立时间”, fontChina10, false, false, 0, 0);

PdfUtil.addTableCell(table, DateFormatUtils.format(stdCommittee.getEstablishDate(), “yyyy-MM-dd”), fontChina10, true, false, 3, 0);//跨三列

PdfUtil.addTableCell(table, “负责制修订地方标准的专业领域”, fontChina10, false, true, 0, 3); //跨三行

PdfUtil.addTableCell(table, stdCommittee.getProfessionalField(), fontChina10, true, true, 7, 3);//跨七列

List staffs = stdCommittee.getStdCommitteeSecretariat().getStdCommitteeSecretariatStaffs();

PdfUtil.addTableCell(table, “技术委员会秘书处工作人员”, fontChina10, false, true, 0, staffs.size() + 1);

PdfUtil.addTableCell(table, “姓名”, fontChina10, true, false, 0, 0);

PdfUtil.addTableCell(table, “秘书类型”, fontChina10, true, false, 0, 0);

PdfUtil.addTableCell(table, “职务/职称”, fontChina10, true, false, 0, 0);

PdfUtil.addTableCell(table, “出生年月”, fontChina10, true, false, 0, 0);

PdfUtil.addTableCell(table, “学历”, fontChina10, true, false, 0, 0);

PdfUtil.addTableCell(table, “电话”, fontChina10, true, false, 2, 0);

staffs.forEach(staff -> {

PdfUtil.addTableCell(table, staff.getName(), fontChina10, true, false, 0, 0);

PdfUtil.addTableCell(table, staff.getType(), fontChina10, true, false, 0, 0);

PdfUtil.addTableCell(table, staff.getProfessionalTitle(), fontChina10, true, false, 0, 0);

PdfUtil.addTableCell(table, DateFormatUtils.format(staff.getBirthday(), “yyyy-MM-dd”), fontChina10, true, false, 0, 0);

PdfUtil.addTableCell(table, staff.getQualification(), fontChina10, true, false, 0, 0);

PdfUtil.addTableCell(table, staff.getPhone(), fontChina10, true, false, 2, 0);

});

PdfUtil.addTableCell(table, “技术委员会下设分技术委员会或标准化技术专家组”, fontChina15, true, false, 8, 0);//跨8列

PdfUtil.addTableCell(table, “id”, fontChina10, true, false, 2, 0);

PdfUtil.addTableCell(table, “编号”, fontChina10, true, false, 2, 0);

PdfUtil.addTableCell(table, “名称”, fontChina10, true, false, 2, 0);

PdfUtil.addTableCell(table, “委员数”, fontChina10, true, false, 2, 0);

stdCommittee.getStdCommitteeBranches().forEach(branch -> {

PdfUtil.addTableCell(table, branch.getId().toString(), fontChina10, true, false, 2, 0);

PdfUtil.addTableCell(table, branch.getCode(), fontChina10, true, false, 2, 0);

PdfUtil.addTableCell(table, branch.getName(), fontChina10, true, false, 2, 0);

PdfUtil.addTableCell(table, branch.getNumberMember().toString(), fontChina10, true, false, 2, 0);

});

document.add(table);

} catch (DocumentException | IOException e) {

e.printStackTrace();

} finally {

document.close();

}

}

}

  • 导出效果

直接浏览器访问: http://localhost:8080/committee/download/1477482360448090113

2022-01-02-ExportPDF-Browser.jpg

或者在 PostMan 中访问: http://localhost:8080/committee/download/1477482360448090113 ,不过需要保存为文件后再查看生成的PDF文件内容。

2022-01-02-ExportPDF-PostMan.jpg

添加文字水印

  • 配置文件

为了方便控制是否启用文字水印,我在原配置中添加了以下自定义配置(实际生产中一般与配置中心配合使用,实现动态控制开关):

pdf:

watermark:

text:

enabled: true

content: ‘Heartsuit文字水印666’

  • 配置类

/**

  • @Author Heartsuit

  • @Date 2022-01-02

*/

@Configuration

@ConfigurationProperties(prefix = “pdf.watermark”)

@Data

public class PdfConfigProperties

{

private TextProperties text = new TextProperties();

@Data

public static class TextProperties{

private Boolean enabled;

private String content;

}

}

  • 核心服务类

public class TextWaterMark extends PdfPageEventHelper {

private String waterMarkText;

public TextWaterMark(String waterMarkText) {

this.waterMarkText = waterMarkText;

}

public void onEndPage(PdfWriter writer, Document document) {

try {

float pageWidth = document.right() + document.left();//获取pdf内容正文页面宽度

float pageHeight = document.top() + document.bottom();//获取pdf内容正文页面高度

//设置水印字体格式

BaseFont base = BaseFont.createFont(“STSong-Light”, “UniGB-UCS2-H”, BaseFont.NOT_EMBEDDED);

Font waterMarkFont = new Font(base, 20, Font.BOLD, BaseColor.LIGHT_GRAY);

PdfContentByte waterMarkPdfContent = writer.getDirectContentUnder();

Phrase phrase = new Phrase(waterMarkText, waterMarkFont);

//两行三列

ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase,

pageWidth * 0.25f, pageHeight * 0.2f, 45);

ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase,

pageWidth * 0.25f, pageHeight * 0.5f, 45);

ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase,

pageWidth * 0.25f, pageHeight * 0.8f, 45);

ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase,

pageWidth * 0.65f, pageHeight * 0.2f, 45);

ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase,

pageWidth * 0.65f, pageHeight * 0.5f, 45);

ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase,

pageWidth * 0.65f, pageHeight * 0.8f, 45);

} catch (DocumentException | IOException de) {

de.printStackTrace();

}

}

}

  • 导出PDF代码需修改的部分

2022-01-02-WaterMarkTextChange.jpg

  • 导出效果

只有一页时,导出的PDF文件效果:

2022-01-02-WaterMarkText.jpg

当有多页时,导出的PDF文件效果:

2022-01-02-WaterMarkTextPage.jpg

添加图片水印

  • 配置文件

为了方便控制是否启用图片水印,我在原配置中添加了以下自定义配置(实际生产中一般与配置中心配合使用,实现动态控制开关):

另外,图片文件我直接放到了 resources 根目录下,然后代码中通过 Resource 类读取到路径,传递给生产带图片水印的核心服务层方法。

pdf:

watermark:

text:

enabled: false

content: ‘Heartsuit文字水印666’

image:

enabled: true

file: ‘avatar.jpg’

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)

img

最后

ActiveMQ消息中间件面试专题

  • 什么是ActiveMQ?
  • ActiveMQ服务器宕机怎么办?
  • 丢消息怎么办?
  • 持久化消息非常慢怎么办?
  • 消息的不均匀消费怎么办?
  • 死信队列怎么办?
  • ActiveMQ中的消息重发时间间隔和重发次数吗?

ActiveMQ消息中间件面试专题解析拓展:

BAT面试文档:ActiveMQ+redis+Spring+高并发多线程+JVM


redis面试专题及答案

  • 支持一致性哈希的客户端有哪些?
  • Redis与其他key-value存储有什么不同?
  • Redis的内存占用情况怎么样?
  • 都有哪些办法可以降低Redis的内存使用情况呢?
  • 查看Redis使用情况及状态信息用什么命令?
  • Redis的内存用完了会发生什么?
  • Redis是单线程的,如何提高多核CPU的利用率?

BAT面试文档:ActiveMQ+redis+Spring+高并发多线程+JVM


Spring面试专题及答案

  • 谈谈你对 Spring 的理解
  • Spring 有哪些优点?
  • Spring 中的设计模式
  • 怎样开启注解装配以及常用注解
  • 简单介绍下 Spring bean 的生命周期

Spring面试答案解析拓展

BAT面试文档:ActiveMQ+redis+Spring+高并发多线程+JVM


高并发多线程面试专题

  • 现在有线程 T1、T2 和 T3。你如何确保 T2 线程在 T1 之后执行,并且 T3 线程在 T2 之后执行?
  • Java 中新的 Lock 接口相对于同步代码块(synchronized block)有什么优势?如果让你实现一个高性能缓存,支持并发读取和单一写入,你如何保证数据完整性。
  • Java 中 wait 和 sleep 方法有什么区别?
  • 如何在 Java 中实现一个阻塞队列?
  • 如何在 Java 中编写代码解决生产者消费者问题?
  • 写一段死锁代码。你在 Java 中如何解决死锁?

高并发多线程面试解析与拓展

BAT面试文档:ActiveMQ+redis+Spring+高并发多线程+JVM


jvm面试专题与解析

  • JVM 由哪些部分组成?
  • JVM 内存划分?
  • Java 的内存模型?
  • 引用的分类?
  • GC什么时候开始?

JVM面试专题解析与拓展!

BAT面试文档:ActiveMQ+redis+Spring+高并发多线程+JVM

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

高并发多线程面试专题

  • 现在有线程 T1、T2 和 T3。你如何确保 T2 线程在 T1 之后执行,并且 T3 线程在 T2 之后执行?
  • Java 中新的 Lock 接口相对于同步代码块(synchronized block)有什么优势?如果让你实现一个高性能缓存,支持并发读取和单一写入,你如何保证数据完整性。
  • Java 中 wait 和 sleep 方法有什么区别?
  • 如何在 Java 中实现一个阻塞队列?
  • 如何在 Java 中编写代码解决生产者消费者问题?
  • 写一段死锁代码。你在 Java 中如何解决死锁?

高并发多线程面试解析与拓展

[外链图片转存中…(img-NNJfIPWy-1713659851910)]


jvm面试专题与解析

  • JVM 由哪些部分组成?
  • JVM 内存划分?
  • Java 的内存模型?
  • 引用的分类?
  • GC什么时候开始?

JVM面试专题解析与拓展!

[外链图片转存中…(img-DpzRBc3E-1713659851910)]

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 16
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值