Java 生成「产品质量检验报告(质检机构)」PDF 示例(BAR)
BAR2 页 A4 · 104 KB
质检机构出具的委托检验报告:样品信息与五列检验项目表(技术要求 / 实测结果),并用分组柱状图对比标准限值与实测值,最后给出绿底大字判定结论与三栏签字区。

输出规格
- PDF 页数
- 2
- 图表类型
- BAR
- 模板行数
- 135
- 源码行数
- 88
Java 源码 · JQuickBiz50InspectionReportTest.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.demo.biz;
import com.github.paohaijiao.JOption;
import com.github.paohaijiao.axis.JCategoryAxis;
import com.github.paohaijiao.axis.JValueAxis;
import com.github.paohaijiao.code.JTrigger;
import com.github.paohaijiao.config.JPdfConfig;
import com.github.paohaijiao.demo.constant.JQuickConstant;
import com.github.paohaijiao.enums.JChartType;
import com.github.paohaijiao.executor.JQuickPdfFactory;
import com.github.paohaijiao.factory.JChartRendererFactory;
import com.github.paohaijiao.series.JBar;
import org.junit.Test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 检验报告 Demo 50:产品质量检验报告(质检机构场景)
*
* <p>业务场景:质检机构出具委托检验报告,记录样品信息、各检验项目的技术要求与实测结果,
* 给出单项判定与整份报告的最终结论。</p>
*
* <p>版式:页头左标题区 + 右编号信息盒;样品信息四列表;检验项目五列表;
* 实测值与标准限值对比柱状图;整块绿底大字判定结论;三栏签字区 + 声明小字。</p>
*
* <p>图表说明:使用 {@link JChartType#BAR} 分组柱状图对比「标准限值 / 实测值」,
* 使判定结论有可核对的数据支撑;模板中 {@code <svg>} 只声明宽度,高度按原比例自动缩放。</p>
*
* <p>模板:{@code report/biz/50_inspection_report.txt},输出:{@code D:\test\biz\50_inspection_report.pdf}。</p>
*
* @author Martin
* @version 1.0.0
*/
public class JQuickBiz50InspectionReportTest {
public static final String path = JQuickConstant.path;
@Test
public void biz50InspectionReport() throws IOException {
JOption option = new JOption();
option.title().text("各检验项目实测值与标准限值对比").subtext("绝缘电阻按 1/20 比例折算显示");
option.tooltip().trigger(JTrigger.axis);
JCategoryAxis xAxis = new JCategoryAxis();
xAxis.data("绝缘电阻", "耐电压", "空载电流", "空载损耗", "效率");
option.xAxis(xAxis);
option.yAxis(new JValueAxis());
// 两组柱:标准限值与实测值,便于目视判断余量
JBar standard = new JBar();
standard.name("标准限值").data(20, 100, 5.6, 190, 87);
JBar measured = new JBar();
measured.name("实测值").data(24, 100, 4.8, 172, 89.4);
option.series(standard, measured);
String svg = JChartRendererFactory.renderChart(JChartType.BAR, option);
File dir = new File(path + "biz");
if (!dir.exists()) {
dir.mkdirs();
}
FileOutputStream fileOutputStream = new FileOutputStream(path + "biz/50_inspection_report.pdf");
JPdfConfig config = new JPdfConfig();
// 开启 flex 行布局,使模板中的 display:flex 生效(页头双栏、图表居中、三栏签字区)
config.getLayoutConfig().setFlexLayout(true);
JQuickPdfFactory factory = new JQuickPdfFactory(config);
factory.bind("svg", svg);
byte[] bytes = factory.executeResource("report/biz/50_inspection_report.txt");
fileOutputStream.write(bytes);
fileOutputStream.close();
}
}最小调用(3 行)
不想读完整测试类?核心就是这三行:绑定图表 SVG,执行模板,拿到 PDF 字节流。可直接复制到你的项目里。
// config 可省略;svg 来自 JChartRendererFactory.renderChart(...),见下方完整源码
factory.bind("svg", svg);
byte[] pdf = new JQuickPdfFactory(config).executeResource("report/biz/50_inspection_report.txt");模板文件 · 50_inspection_report.txt
类 HTML + 内联 CSS 的模板语法,绑定数据后渲染为 PDF。下方为完整模板,可直接复制,也可下载原始文件。
<pdf>
<body>
// ---------- 页头:左标题区 + 右编号信息盒 ----------
<div style="display:flex; justifyContent:space-between; marginBottom:10px">
<div style="width:320px">
<h1 style="fontSize:13pt; bold:true; color:#1F4E79; characterSpacing:1pt; marginBottom:2px">'××省产品质量监督检验研究院'</h1>
<h2 style="fontSize:17pt; bold:true; color:#1A2733; textAlignment:center; marginBottom:2px">'产品质量检验报告'</h2>
<p style="fontSize:8pt; color:#6c757d; textAlignment:center">'(委托检验 · 依据 GB/T 755-2019、GB/T 1032-2012)'</p>
</div>
// 信息盒宽 235px:盒内可用宽度 219px(235 减去左右内边距),恰好容纳 9pt 下 16 个汉字
//(「委托单位:××机电设备有限公司」约 144pt / 200px);盒宽 195px 时该行会折行成「…有限公 / 司」。
<div style="width:235px; border:1px solid #1F4E79; padding:8px">
<p style="fontSize:9pt; color:#1F4E79; bold:true; marginBottom:3px">'报告编号:JY-2024-1206'</p>
<p style="fontSize:9pt; color:#5A6B7C; marginBottom:3px">'委托单位:××机电设备有限公司'</p>
<p style="fontSize:9pt; color:#5A6B7C; marginBottom:3px">'检验类别:委托检验'</p>
<p style="fontSize:9pt; color:#5A6B7C">'签发日期:2024年12月20日'</p>
</div>
</div>
// ---------- 一、样品信息:标签底色 + 值左对齐 ----------
<h3 style="fontSize:11pt; bold:true; color:#1F4E79; borderBottom:1px solid #1F4E79; paddingBottom:2px; marginBottom:6px">'一、样品信息'</h3>
<table style="width:560px; fontSize:9pt">
<tr>
<td style="width:80px; padding:5px; backgroundColor:#E8EDF2; bold:true; border:1px solid #C3CDD8">'样品名称'</td>
<td style="width:200px; padding:5px; border:1px solid #C3CDD8">'三相异步电动机'</td>
<td style="width:80px; padding:5px; backgroundColor:#E8EDF2; bold:true; border:1px solid #C3CDD8">'规格型号'</td>
<td style="width:200px; padding:5px; border:1px solid #C3CDD8">'Y2-132M-4'</td>
</tr>
<tr>
<td style="padding:5px; backgroundColor:#E8EDF2; bold:true; border:1px solid #C3CDD8">'样品编号'</td>
<td style="padding:5px; border:1px solid #C3CDD8">'YP-2024-1206-01'</td>
<td style="padding:5px; backgroundColor:#E8EDF2; bold:true; border:1px solid #C3CDD8">'样品数量'</td>
<td style="padding:5px; border:1px solid #C3CDD8">'12 台(抽检 3 台)'</td>
</tr>
<tr>
<td style="padding:5px; backgroundColor:#E8EDF2; bold:true; border:1px solid #C3CDD8">'受检单位'</td>
<td style="padding:5px; border:1px solid #C3CDD8">'××机电设备有限公司'</td>
<td style="padding:5px; backgroundColor:#E8EDF2; bold:true; border:1px solid #C3CDD8">'到样日期'</td>
<td style="padding:5px; border:1px solid #C3CDD8">'2024年12月10日'</td>
</tr>
<tr>
<td style="padding:5px; backgroundColor:#E8EDF2; bold:true; border:1px solid #C3CDD8">'样品状态'</td>
<td style="padding:5px; border:1px solid #C3CDD8">'包装完好,外观无损伤'</td>
<td style="padding:5px; backgroundColor:#E8EDF2; bold:true; border:1px solid #C3CDD8">'检验环境'</td>
<td style="padding:5px; border:1px solid #C3CDD8">'温度 22℃ 湿度 55%RH'</td>
</tr>
</table>
// ---------- 二、检验项目与结果:五列,判定列居中 ----------
<h3 style="fontSize:11pt; bold:true; color:#1F4E79; borderBottom:1px solid #1F4E79; paddingBottom:2px; marginTop:10px; marginBottom:6px">'二、检验项目与结果'</h3>
<table style="width:560px; fontSize:9pt">
<tr>
<th style="width:36px; padding:5px; backgroundColor:#1F4E79; color:white; textAlign:center; border:1px solid #1F4E79">'序号'</th>
<th style="width:130px; padding:5px; backgroundColor:#1F4E79; color:white; textAlign:center; border:1px solid #1F4E79">'检验项目'</th>
<th style="width:150px; padding:5px; backgroundColor:#1F4E79; color:white; textAlign:center; border:1px solid #1F4E79">'技术要求'</th>
<th style="width:150px; padding:5px; backgroundColor:#1F4E79; color:white; textAlign:center; border:1px solid #1F4E79">'检验结果'</th>
<th style="width:94px; padding:5px; backgroundColor:#1F4E79; color:white; textAlign:center; border:1px solid #1F4E79">'单项判定'</th>
</tr>
<tr>
<td style="padding:5px; textAlign:center; border:1px solid #D3DCE5">'1'</td>
<td style="padding:5px; border:1px solid #D3DCE5">'绝缘电阻(冷态)'</td>
<td style="padding:5px; border:1px solid #D3DCE5">'不小于 20 MΩ'</td>
<td style="padding:5px; border:1px solid #D3DCE5">'480 MΩ'</td>
<td style="padding:5px; textAlign:center; color:#1E7A46; bold:true; border:1px solid #D3DCE5">'合格'</td>
</tr>
<tr>
<td style="padding:5px; textAlign:center; border:1px solid #D3DCE5">'2'</td>
<td style="padding:5px; border:1px solid #D3DCE5">'耐电压试验'</td>
<td style="padding:5px; border:1px solid #D3DCE5">'1min 无击穿闪络'</td>
<td style="padding:5px; border:1px solid #D3DCE5">'无击穿、无闪络'</td>
<td style="padding:5px; textAlign:center; color:#1E7A46; bold:true; border:1px solid #D3DCE5">'合格'</td>
</tr>
<tr>
<td style="padding:5px; textAlign:center; border:1px solid #D3DCE5">'3'</td>
<td style="padding:5px; border:1px solid #D3DCE5">'空载电流'</td>
<td style="padding:5px; border:1px solid #D3DCE5">'不大于 5.6 A'</td>
<td style="padding:5px; border:1px solid #D3DCE5">'4.8 A'</td>
<td style="padding:5px; textAlign:center; color:#1E7A46; bold:true; border:1px solid #D3DCE5">'合格'</td>
</tr>
<tr>
<td style="padding:5px; textAlign:center; border:1px solid #D3DCE5">'4'</td>
<td style="padding:5px; border:1px solid #D3DCE5">'空载损耗'</td>
<td style="padding:5px; border:1px solid #D3DCE5">'不大于 190 W'</td>
<td style="padding:5px; border:1px solid #D3DCE5">'172 W'</td>
<td style="padding:5px; textAlign:center; color:#1E7A46; bold:true; border:1px solid #D3DCE5">'合格'</td>
</tr>
<tr>
<td style="padding:5px; textAlign:center; border:1px solid #D3DCE5">'5'</td>
<td style="padding:5px; border:1px solid #D3DCE5">'效率(额定点)'</td>
<td style="padding:5px; border:1px solid #D3DCE5">'不低于 87.0 %'</td>
<td style="padding:5px; border:1px solid #D3DCE5">'89.4 %'</td>
<td style="padding:5px; textAlign:center; color:#1E7A46; bold:true; border:1px solid #D3DCE5">'合格'</td>
</tr>
</table>
// ---------- 检验结果可视化:以技术要求为基准线的对比柱状图 ----------
<div style="display:flex; justifyContent:center; marginTop:8px">
<svg style="width:640pt">${svg}</svg>
</div>
<p style="fontSize:8pt; color:#6c757d; italic:true; textAlignment:center; marginTop:4px">'图 1 各检验项目实测值与标准限值对比(绝缘电阻按 1/20 比例折算显示)'</p>
// ---------- 判定结论:整块绿底强调 ----------
<div style="border:2px solid #1E7A46; background:#EAF5EF; padding:10px; marginTop:8px">
<h2 style="fontSize:15pt; bold:true; color:#1E7A46; textAlignment:center; characterSpacing:2pt">'判 定 结 论 : 合 格'</h2>
<p style="fontSize:9pt; color:#333333; textAlignment:center; marginTop:4px">'所检 5 个项目全部符合 GB/T 755-2019《旋转电机 定额和性能》要求,本次检验判定为合格。'</p>
</div>
// ---------- 备注:有序列表 ----------
<h3 style="fontSize:11pt; bold:true; color:#1F4E79; marginTop:10px; marginBottom:4px">'三、备注'</h3>
<list style="paddingLeft:16px; fontSize:9pt" listType="ordered">
<li style="marginBottom:3px">'本报告仅对来样负责,未经本院书面同意不得部分复制。'</li>
<li style="marginBottom:3px">'检验依据为委托方提供的产品标准,标准变更时结果解释权归本院。'</li>
<li style="marginBottom:3px">'如对报告有异议,应于收到报告之日起 15 日内向本院提出书面复核申请。'</li>
</list>
// ---------- 签字区:三栏 + 声明小字 ----------
<div style="display:flex; justifyContent:space-between; marginTop:16px">
<div style="width:160px; borderTop:1px solid #5A6B7C; paddingTop:4px">
<p style="fontSize:9pt; color:#5A6B7C; textAlignment:center">'主检人(签字)'</p>
</div>
<div style="width:160px; borderTop:1px solid #5A6B7C; paddingTop:4px">
<p style="fontSize:9pt; color:#5A6B7C; textAlignment:center">'审核人(签字)'</p>
</div>
<div style="width:160px; borderTop:1px solid #5A6B7C; paddingTop:4px">
<p style="fontSize:9pt; color:#5A6B7C; textAlignment:center">'批准人(签字)'</p>
</div>
</div>
<div style="marginTop:10px">
<lineSeparator style="strokeColor:#C3CDD8"></lineSeparator>
<p style="fontSize:8pt; color:#8a939b; textAlignment:center; marginTop:3px">'联系地址:××省××市××区检测路 8 号 联系电话:0000-88888888 报告查询:www.example-jy.cn'</p>
</div>
</body>
</pdf>同分组示例
本页 PDF、预览图、模板与源码均由 JQuick-Pdf 真实运行产出,可自由复制到你的项目中使用。