Java 生成「医疗器械合格证」PDF 示例(Template)
Template1 页 A4 · 53 KB
产品合格证版式,适用于医疗器械出厂检验合格标识。

输出规格
- PDF 页数
- 1
- 图表类型
- Template
- 模板行数
- 64
- 源码行数
- 92
Java 源码 · JQuickBiz84MedicalCertificate.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 JQuickBiz84MedicalCertificate {
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 + "84_medical_certificate.pdf";
private static final String TEMPLATE = "report/biz/84_medical_certificate.txt";
public static void main(String[] args) throws IOException {
mkdirs();
generateSeal(IMG_DIR + "84_seal.png");
Map<String, Object> data = new LinkedHashMap<String, Object>();
data.put("certNo", "HC-2026-B0912-0186");
data.put("productName", "一次性使用无菌注射器(带针)");
data.put("model", "5ml 0.6×25TWLB");
data.put("batchNo", "20260912-A03");
data.put("produceDate", "2026年09月12日");
data.put("validUntil", "2029年09月11日");
data.put("manufacturer", "××医疗器械制造有限公司");
data.put("licenseNo", "苏食药监械生产许 2006XXXX 号");
data.put("address", "××省××市××区××路 66 号");
data.put("inspector", "王丽");
JPdfConfig config = new JPdfConfig();
config.getLayoutConfig().setFlexLayout(true);
// 上下留白多、左右收窄,让内容呈「卡片」居中效果
config.getDoc().setMargins(Arrays.asList(70, 78, 70, 78));
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 = 180;
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, 19));
g.drawString("质检合格", size / 2 - 44, size / 2 - 4);
g.setFont(new Font("Microsoft YaHei", Font.PLAIN, 11));
g.drawString("QC PASSED", size / 2 - 32, size / 2 + 22);
g.dispose();
ImageIO.write(image, "png", new File(path));
}
}最小调用(3 行)
不想读完整测试类?核心就是这三行:绑定图表 SVG,执行模板,拿到 PDF 字节流。可直接复制到你的项目里。
// config 可省略;svg 来自 JChartRendererFactory.renderChart(...),见下方完整源码
byte[] pdf = new JQuickPdfFactory(config).executeResource("report/biz/84_medical_certificate.txt");模板文件 · 84_medical_certificate.txt
类 HTML + 内联 CSS 的模板语法,绑定数据后渲染为 PDF。下方为完整模板,可直接复制,也可下载原始文件。
<pdf>
<body>
<div style="border:2px solid #1B5E20; padding:12px">
<div style="border:1px solid #A5C6A8; padding:12px">
<h1 style="fontSize:20pt; bold:true; color:#1B5E20; textAlignment:center; characterSpacing:8pt">'产品合格证'</h1>
<p style="fontSize:8pt; color:#5A6B7C; textAlignment:center; marginBottom:8px">'CERTIFICATE OF CONFORMITY'</p>
<p style="fontSize:8.5pt; color:#5A6B7C; textAlignment:right">'编号:'${certNo}</p>
<table style="width:100%; fontSize:8.5pt; marginTop:4px">
<tr>
<td style="width:78px; padding:4px; backgroundColor:#F0F7F0; bold:true; border:1px solid #A5C6A8">'产品名称'</td>
<td style="padding:4px; border:1px solid #A5C6A8">${productName}</td>
</tr>
<tr>
<td style="padding:4px; backgroundColor:#F0F7F0; bold:true; border:1px solid #A5C6A8">'型号规格'</td>
<td style="padding:4px; border:1px solid #A5C6A8">${model}</td>
</tr>
<tr>
<td style="padding:4px; backgroundColor:#F0F7F0; bold:true; border:1px solid #A5C6A8">'生产批号'</td>
<td style="padding:4px; border:1px solid #A5C6A8">${batchNo}</td>
</tr>
<tr>
<td style="padding:4px; backgroundColor:#F0F7F0; bold:true; border:1px solid #A5C6A8">'生产日期'</td>
<td style="padding:4px; border:1px solid #A5C6A8">${produceDate}</td>
</tr>
<tr>
<td style="padding:4px; backgroundColor:#F0F7F0; bold:true; border:1px solid #A5C6A8">'有效期至'</td>
<td style="padding:4px; border:1px solid #A5C6A8">${validUntil}</td>
</tr>
<tr>
<td style="padding:4px; backgroundColor:#F0F7F0; bold:true; border:1px solid #A5C6A8">'生产企业'</td>
<td style="padding:4px; border:1px solid #A5C6A8">${manufacturer}</td>
</tr>
<tr>
<td style="padding:4px; backgroundColor:#F0F7F0; bold:true; border:1px solid #A5C6A8">'生产许可证'</td>
<td style="padding:4px; border:1px solid #A5C6A8">${licenseNo}</td>
</tr>
<tr>
<td style="padding:4px; backgroundColor:#F0F7F0; bold:true; border:1px solid #A5C6A8">'生产地址'</td>
<td style="padding:4px; border:1px solid #A5C6A8">${address}</td>
</tr>
</table>
<p style="fontSize:8pt; color:#333333; textIndent:16pt; marginTop:8px">'本产品经检验,符合产品技术要求及相关标准规定,准予出厂。'</p>
<div style="display:flex; justifyContent:space-between; marginTop:10px">
<div style="width:150px">
<p style="fontSize:9pt; color:#333333; marginTop:26px">'检验员:'${inspector}</p>
<p style="fontSize:8.5pt; color:#5A6B7C; marginTop:6px">'检验日期:'${produceDate}</p>
</div>
<div style="width:150px; textAlignment:center">
<image src='D:/test/image/84_seal.png' style="width:84px; height:84px"></image>
<p style="fontSize:8pt; color:#1B5E20; textAlignment:center">'质检合格章'</p>
</div>
</div>
<p style="fontSize:7pt; color:#8a939b; textAlignment:center; marginTop:8px">'请妥善保存本证,凭此证享受售后服务'</p>
</div>
</div>
</body>
</pdf>同分组示例
本页 PDF、预览图、模板与源码均由 JQuick-Pdf 真实运行产出,可自由复制到你的项目中使用。