Java 生成「奖学金证书」PDF 示例(Template)
Template1 页 A4 · 53 KB
奖学金获奖证书,居中标题 + 盖章位。

输出规格
- PDF 页数
- 1
- 图表类型
- Template
- 模板行数
- 41
- 源码行数
- 86
Java 源码 · JQuickBiz89ScholarshipCert.java
以下代码即生成上方 PDF 的完整测试类,可直接复制运行;Maven 依赖见快速开始章节。
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright (c) [2025-2099] Martin ([email protected])
*/
package com.github.paohaijiao.biz.scenario;
import com.github.paohaijiao.config.JPdfConfig;
import com.github.paohaijiao.executor.JQuickPdfFactory;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
public class JQuickBiz89ScholarshipCert {
private static final String PDF_DIR = "D:/test/pdf/";
private static final String IMG_DIR = "D:/test/image/";
private static final String OUT_FILE = PDF_DIR + "89_scholarship_cert.pdf";
private static final String TEMPLATE = "report/biz/89_scholarship_cert.txt";
public static void main(String[] args) throws IOException {
mkdirs();
generateSeal(IMG_DIR + "89_seal.png");
Map<String, Object> data = new LinkedHashMap<String, Object>();
data.put("certNo", "JXJ-2026-0098");
data.put("studentName", "顾清和");
data.put("schoolName", "××大学");
data.put("awardName", "校级优秀学生一等奖学金");
data.put("academicYear", "2025-2026");
data.put("issueDate", "2026年10月19日");
JPdfConfig config = new JPdfConfig();
config.getLayoutConfig().setFlexLayout(true);
config.getDoc().setMargins(Arrays.asList(42, 44, 42, 44));
config.getFooterConfig().setEnabled(false);
JQuickPdfFactory factory = new JQuickPdfFactory(config);
factory.bindAll(data);
byte[] pdf = factory.executeResource(TEMPLATE);
FileOutputStream out = new FileOutputStream(OUT_FILE);
out.write(pdf);
out.close();
System.out.println("已生成:" + OUT_FILE);
}
private static void mkdirs() {
new File(PDF_DIR).mkdirs();
new File(IMG_DIR).mkdirs();
}
private static void generateSeal(String path) throws IOException {
int size = 210;
BufferedImage image = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Color red = new Color(0xC6, 0x28, 0x28);
g.setColor(red);
g.setStroke(new BasicStroke(6f));
g.drawOval(8, 8, size - 16, size - 16);
g.setFont(new Font("Microsoft YaHei", Font.BOLD, 16));
g.drawString("××大学", size / 2 - 28, 62);
g.drawString("学生工作处", size / 2 - 40, size / 2 + 30);
g.dispose();
ImageIO.write(image, "png", new File(path));
}
}最小调用(3 行)
不想读完整测试类?核心就是这三行:绑定图表 SVG,执行模板,拿到 PDF 字节流。可直接复制到你的项目里。
// config 可省略;svg 来自 JChartRendererFactory.renderChart(...),见下方完整源码
byte[] pdf = new JQuickPdfFactory(config).executeResource("report/biz/89_scholarship_cert.txt");模板文件 · 89_scholarship_cert.txt
类 HTML + 内联 CSS 的模板语法,绑定数据后渲染为 PDF。下方为完整模板,可直接复制,也可下载原始文件。
<pdf>
<body>
<div style="border:3px solid #B8860B; padding:9px">
<div style="border:1px solid #D4AF37; padding:18px">
<div style="display:flex; justifyContent:space-between">
<div style="width:200px">
<p style="fontSize:11pt; bold:true; color:#8C6D3F; characterSpacing:2pt">${schoolName}</p>
</div>
<div style="width:200px; textAlignment:right">
<p style="fontSize:8.5pt; color:#8C6D3F">'证书编号:'${certNo}</p>
</div>
</div>
<h1 style="fontSize:44pt; bold:true; color:#B8860B; textAlignment:center; characterSpacing:18pt; marginTop:22px">'荣 誉 证 书'</h1>
<p style="fontSize:10pt; color:#A08A6A; textAlignment:center; characterSpacing:5pt">'CERTIFICATE OF HONOR'</p>
<lineSeparator style="strokeColor:#D4AF37; marginTop:8px"></lineSeparator>
<p style="fontSize:13pt; color:#333333; textAlignment:center; marginTop:24px">'兹授予'</p>
<p style="fontSize:28pt; bold:true; color:#1A2733; textAlignment:center; characterSpacing:6pt; marginTop:4px">${studentName}</p>
<p style="fontSize:11.5pt; color:#333333; textAlignment:center; marginTop:10px">'同学 '${academicYear}' 学年「'${awardName}'」荣誉称号,'</p>
<p style="fontSize:11.5pt; color:#333333; textAlignment:center">'以表彰其在学业与综合素质方面的优异表现。'</p>
<p style="fontSize:11.5pt; color:#333333; textAlignment:center; marginTop:8px">'特发此证,以资鼓励。'</p>
<div style="display:flex; justifyContent:space-between; marginTop:26px">
<div style="width:300px">
<p style="fontSize:9pt; color:#8a939b">'本证书可在校学生工作管理系统在线验证'</p>
<p style="fontSize:9pt; color:#8a939b; marginTop:3px">'查询地址:https://example.edu.cn/honor'</p>
</div>
<div style="width:230px; textAlignment:center">
<image src='D:/test/image/89_seal.png' style="width:104px; height:104px"></image>
<p style="fontSize:10.5pt; bold:true; color:#8C6D3F; textAlignment:center">${schoolName}</p>
<p style="fontSize:10.5pt; color:#333333; textAlignment:center; marginTop:2px">${issueDate}</p>
</div>
</div>
</div>
</div>
</body>
</pdf>同分组示例
本页 PDF、预览图、模板与源码均由 JQuick-Pdf 真实运行产出,可自由复制到你的项目中使用。