Java 生成「红字发票信息表」PDF 示例(Template)
Template1 页 A4 · 31 KB
红字增值税发票信息表版式,冲红申请场景。

输出规格
- PDF 页数
- 1
- 图表类型
- Template
- 模板行数
- 157
- 源码行数
- 206
Java 源码 · JQuickBiz118RedInvoiceInfoTable.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.finance;
import com.github.paohaijiao.config.JPdfConfig;
import com.github.paohaijiao.executor.JQuickPdfFactory;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* 单据 118:红字增值税发票信息表(finance 领域第 19 项)。
*
* <p>本类为独立可运行 Demo:自带 {@code main} 方法、不依赖任何公共工具类或实体类,
* 数据以 key-value 形式通过 {@link JQuickPdfFactory#bindAll(Map)} 注入,
* 版式全部落在模板文件 {@code demo/biz/finance/118_red_invoice_info_table.txt} 中。</p>
*
* <p>视觉主题「秋香·密排·典式」:主色 #5F7E2A、辅色 #273091、浅底 #F4F6F1;
* 版式为 classic 抬头 + strip 信息区 + compact 明细表 + right 签章区 + right 落款,
* 与同领域其它单据互不重样。</p>
*
* <p>红字发票信息表:蓝字发票代码号码与开票日期、申请方与申请原因、</p>
* <p>逐行红冲数量与负数金额税额、红冲合计与税务机关受理栏。</p>
*
* <p>运行后输出:{@code D:/test/pdf/118_red_invoice_info_table.pdf}。</p>
*
* @author Martin
* @version 1.0.0
*/
public class JQuickBiz118RedInvoiceInfoTable {
private static final String PDF_DIR = "D:/test/pdf/";
private static final String OUT_FILE = PDF_DIR + "118_red_invoice_info_table.pdf";
private static final String TEMPLATE = "demo/biz/finance/118_red_invoice_info_table.txt";
/** 模板固定明细行数,不足时补空行,避免占位符原样输出。 */
private static final int ITEM_ROWS = 5;
public static void main(String[] args) throws IOException {
new File(PDF_DIR).mkdirs();
// ---------- 1. 数据(无实体类,直接 bind)----------
String[][] items = {
{"工业级伺服电机", "SVM-750W", "台", "12", "-2", "-3720.00", "-483.60"},
{"精密减速机", "RV-40E", "台", "12", "-1", "-1240.00", "-161.20"},
{"运动控制卡", "MC-8020", "块", "6", "-1", "-2350.00", "-305.50"},
{"设备安装调试服务", "—", "项", "1", "0", "0.00", "0.00"},
{"技术服务费", "—", "项", "1", "0", "0.00", "0.00"}
};
Map<String, Object> data = new LinkedHashMap<String, Object>();
data.put("infoNo", "330100261000881234");
data.put("issueDate", "2026年10月18日");
data.put("blueInvoiceCode", "011002100311");
data.put("blueInvoiceNo", "08876325");
data.put("blueIssueDate", "2026年09月18日");
data.put("sellerName", "××自动化科技有限公司");
data.put("sellerTaxNo", "91320100MA1Y2P94QD");
data.put("buyerName", "××智能装备制造有限公司");
data.put("applicant", "购货方(已抵扣)");
data.put("reasonType", "发生销货退回");
data.put("remark", "购货方已抵扣的,由购货方填开《红字增值税发票信息表》;信息表编号经系统校验通过后,销货方据此开具红字发票。");
data.put("payee", "崔行知");
data.put("reviewer", "柳文渊");
data.put("drawer", "××区税务局");
// 明细逐格展开,并累计金额
BigDecimal itemTotal = BigDecimal.ZERO;
for (int i = 0; i < ITEM_ROWS; i++) {
int n = i + 1;
if (i >= items.length) {
data.put("itemName" + n, "—");
data.put("itemSpec" + n, "—");
data.put("itemUnit" + n, "—");
data.put("itemQty" + n, "—");
data.put("itemPrice" + n, "—");
data.put("itemAmount" + n, "—");
data.put("itemTax" + n, "—");
continue;
}
data.put("itemName" + n, items[i][0]);
data.put("itemSpec" + n, items[i][1]);
data.put("itemUnit" + n, items[i][2]);
data.put("itemQty" + n, items[i][3]);
data.put("itemPrice" + n, items[i][4]);
itemTotal = itemTotal.add(new BigDecimal(items[i][5]));
data.put("itemAmount" + n, items[i][5]);
data.put("itemTax" + n, items[i][6]);
}
data.put("totalAmount", itemTotal.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString());
data.put("totalAmountInWords", toChineseAmount(itemTotal));
data.put("totalLine", "红冲金额合计(大写)负" + toChineseAmount(itemTotal.abs()) + " (小写)¥" + itemTotal.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString());
data.put("totalTax", new BigDecimal("-950.30").setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString());
// ---------- 2. 渲染 ----------
JPdfConfig config = new JPdfConfig();
config.getLayoutConfig().setFlexLayout(true);
config.getDoc().setMargins(Arrays.asList(44, 44, 44, 44));
config.getFontConfig().setDefaultFontName("Noto Sans CJK SC");
config.getHeaderConfig().setEnabled(true);
config.getHeaderConfig().setText("××自动化科技有限公司 红字增值税发票信息表 330100261000881234");
config.getHeaderConfig().setFontSize(8f);
config.getHeaderConfig().setHeight(34);
config.getFooterConfig().setEnabled(true);
config.getFooterConfig().setFontSize(8);
config.getFooterConfig().setPageNumberFormat("— %d —");
JQuickPdfFactory factory = new JQuickPdfFactory(config);
factory.bindAll(data);
byte[] pdf = factory.executeResource(TEMPLATE);
Files.write(new File(OUT_FILE).toPath(), pdf);
System.out.println("已生成:" + OUT_FILE + "(" + pdf.length + " 字节)");
}
/** 人民币金额转票据大写(元角分,末尾补「整」)。 */
private static String toChineseAmount(BigDecimal amount) {
String[] digits = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
String[] units = {"", "拾", "佰", "仟"};
String[] sections = {"", "万", "亿", "万亿"};
long cents = amount.setScale(2, BigDecimal.ROUND_HALF_UP)
.movePointRight(2).longValue();
if (cents == 0) {
return "零元整";
}
long yuan = cents / 100;
int jiao = (int) (cents % 100 / 10);
int fen = (int) (cents % 10);
StringBuilder sb = new StringBuilder();
if (yuan > 0) {
sb.append(toChineseSection(yuan, digits, units, sections)).append("元");
}
if (jiao == 0 && fen == 0) {
sb.append("整");
} else {
if (jiao > 0) {
sb.append(digits[jiao]).append("角");
} else if (yuan > 0) {
sb.append("零");
}
if (fen > 0) {
sb.append(digits[fen]).append("分");
}
}
return sb.toString();
}
/** 整数部分按四位分节转大写。 */
private static String toChineseSection(long value, String[] digits, String[] units, String[] sections) {
StringBuilder sb = new StringBuilder();
int sectionIndex = 0;
boolean needZero = false;
while (value > 0) {
int section = (int) (value % 10000);
StringBuilder part = new StringBuilder();
int unitIndex = 0;
boolean zeroInSection = false;
int temp = section;
while (temp > 0) {
int digit = temp % 10;
if (digit == 0) {
zeroInSection = true;
} else {
if (zeroInSection || (needZero && part.length() == 0 && sb.length() > 0)) {
part.insert(0, digits[0]);
}
part.insert(0, units[unitIndex]);
part.insert(0, digits[digit]);
zeroInSection = false;
needZero = false;
}
unitIndex++;
temp /= 10;
}
if (part.length() > 0) {
part.append(sections[sectionIndex]);
}
sb.insert(0, part);
needZero = section < 1000 && section > 0;
value /= 10000;
sectionIndex++;
}
return sb.toString();
}
}最小调用(3 行)
不想读完整测试类?核心就是这三行:绑定图表 SVG,执行模板,拿到 PDF 字节流。可直接复制到你的项目里。
// config 可省略;svg 来自 JChartRendererFactory.renderChart(...),见下方完整源码
byte[] pdf = new JQuickPdfFactory(config).executeResource("demo/biz/finance/118_red_invoice_info_table.txt");模板文件 · 118_red_invoice_info_table.txt
类 HTML + 内联 CSS 的模板语法,绑定数据后渲染为 PDF。下方为完整模板,可直接复制,也可下载原始文件。
<pdf>
<body>
// 118 红字增值税发票信息表(finance 领域第 19 项) 视觉主题:秋香·密排·典式
// 版式组合:抬头=classic 信息区=strip 明细表=compact 段落=box 签章=right 落款=right
// 主色 #5F7E2A 辅色 #273091 浅底 #F4F6F1
// 红字增值税发票信息表:蓝字发票信息、红冲原因与逐项负数金额税额。
// 抬头:classic
<h1 style="fontSize:18pt; bold:true; color:#415D13; textAlignment:center; characterSpacing:6pt">'红字增值税发票信息表'</h1>
<p style="fontSize:7.5pt; color:#78838C; textAlignment:center; marginBottom:3px">'信息表编号 '${infoNo}' 填开日期 '${issueDate}' 蓝字发票号码 '${blueInvoiceNo}</p>
<lineSeparator style="color:#5F7E2A; thickness:1.5f; marginBottom:8px"></lineSeparator>
// 信息区:grid2
<table style="width:100%; fontSize:8.5pt; marginBottom:8px">
<tr>
<td style="width:16%; padding:4px; backgroundColor:#F4F6F1; bold:true; border:1px solid #CDD5C1">'信息表编号'</td>
<td style="width:34%; padding:4px; border:1px solid #CDD5C1">${infoNo}</td>
<td style="width:16%; padding:4px; backgroundColor:#F4F6F1; bold:true; border:1px solid #CDD5C1">'填开日期'</td>
<td style="padding:4px; border:1px solid #CDD5C1">${issueDate}</td>
</tr>
<tr>
<td style="width:16%; padding:4px; backgroundColor:#F4F6F1; bold:true; border:1px solid #CDD5C1">'蓝字发票代码'</td>
<td style="width:34%; padding:4px; border:1px solid #CDD5C1">${blueInvoiceCode}</td>
<td style="width:16%; padding:4px; backgroundColor:#F4F6F1; bold:true; border:1px solid #CDD5C1">'蓝字发票号码'</td>
<td style="padding:4px; border:1px solid #CDD5C1">${blueInvoiceNo}</td>
</tr>
<tr>
<td style="width:16%; padding:4px; backgroundColor:#F4F6F1; bold:true; border:1px solid #CDD5C1">'蓝字开票日期'</td>
<td style="width:34%; padding:4px; border:1px solid #CDD5C1">${blueIssueDate}</td>
<td style="width:16%; padding:4px; backgroundColor:#F4F6F1; bold:true; border:1px solid #CDD5C1">'销货方名称'</td>
<td style="padding:4px; border:1px solid #CDD5C1">${sellerName}</td>
</tr>
<tr>
<td style="width:16%; padding:4px; backgroundColor:#F4F6F1; bold:true; border:1px solid #CDD5C1">'销货方纳税人识别号'</td>
<td style="width:34%; padding:4px; border:1px solid #CDD5C1">${sellerTaxNo}</td>
<td style="width:16%; padding:4px; backgroundColor:#F4F6F1; bold:true; border:1px solid #CDD5C1">'购货方名称'</td>
<td style="padding:4px; border:1px solid #CDD5C1">${buyerName}</td>
</tr>
<tr>
<td style="width:16%; padding:4px; backgroundColor:#F4F6F1; bold:true; border:1px solid #CDD5C1">'申请方'</td>
<td style="width:34%; padding:4px; border:1px solid #CDD5C1">${applicant}</td>
<td style="width:16%; padding:4px; backgroundColor:#F4F6F1; bold:true; border:1px solid #CDD5C1">'申请原因'</td>
<td style="padding:4px; border:1px solid #CDD5C1">${reasonType}</td>
</tr>
</table>
<p style="fontSize:10pt; bold:true; color:#5F7E2A; marginBottom:3px; characterSpacing:2pt">'红冲货物明细'</p>
<table style="width:100%; fontSize:7.5pt; marginBottom:8px">
<tr>
<th style="width:24%; padding:2px; backgroundColor:#415D13; color:white; bold:true; textAlign:center; border:1px solid #415D13">'货物名称'</th>
<th style="width:14%; padding:2px; backgroundColor:#415D13; color:white; bold:true; textAlign:center; border:1px solid #415D13">'规格型号'</th>
<th style="width:8%; padding:2px; backgroundColor:#415D13; color:white; bold:true; textAlign:center; border:1px solid #415D13">'单位'</th>
<th style="width:10%; padding:2px; backgroundColor:#415D13; color:white; bold:true; textAlign:center; border:1px solid #415D13">'原数量'</th>
<th style="width:10%; padding:2px; backgroundColor:#415D13; color:white; bold:true; textAlign:center; border:1px solid #415D13">'红冲数量'</th>
<th style="width:16%; padding:2px; backgroundColor:#415D13; color:white; bold:true; textAlign:center; border:1px solid #415D13">'金额(负数)'</th>
<th style="padding:2px; backgroundColor:#415D13; color:white; bold:true; textAlign:center; border:1px solid #415D13">'税额(负数)'</th>
</tr>
<tr>
<td style="padding:2px; textAlign:left; border:1px solid #E2E5DF">${itemName1}</td>
<td style="padding:2px; textAlign:center; border:1px solid #E2E5DF">${itemSpec1}</td>
<td style="padding:2px; textAlign:center; border:1px solid #E2E5DF">${itemUnit1}</td>
<td style="padding:2px; textAlign:center; border:1px solid #E2E5DF">${itemQty1}</td>
<td style="padding:2px; textAlign:center; border:1px solid #E2E5DF">${itemPrice1}</td>
<td style="padding:2px; textAlign:right; border:1px solid #E2E5DF">${itemAmount1}</td>
<td style="padding:2px; textAlign:right; border:1px solid #E2E5DF">${itemTax1}</td>
</tr>
<tr>
<td style="padding:2px; textAlign:left; border:1px solid #E2E5DF">${itemName2}</td>
<td style="padding:2px; textAlign:center; border:1px solid #E2E5DF">${itemSpec2}</td>
<td style="padding:2px; textAlign:center; border:1px solid #E2E5DF">${itemUnit2}</td>
<td style="padding:2px; textAlign:center; border:1px solid #E2E5DF">${itemQty2}</td>
<td style="padding:2px; textAlign:center; border:1px solid #E2E5DF">${itemPrice2}</td>
<td style="padding:2px; textAlign:right; border:1px solid #E2E5DF">${itemAmount2}</td>
<td style="padding:2px; textAlign:right; border:1px solid #E2E5DF">${itemTax2}</td>
</tr>
<tr>
<td style="padding:2px; textAlign:left; border:1px solid #E2E5DF">${itemName3}</td>
<td style="padding:2px; textAlign:center; border:1px solid #E2E5DF">${itemSpec3}</td>
<td style="padding:2px; textAlign:center; border:1px solid #E2E5DF">${itemUnit3}</td>
<td style="padding:2px; textAlign:center; border:1px solid #E2E5DF">${itemQty3}</td>
<td style="padding:2px; textAlign:center; border:1px solid #E2E5DF">${itemPrice3}</td>
<td style="padding:2px; textAlign:right; border:1px solid #E2E5DF">${itemAmount3}</td>
<td style="padding:2px; textAlign:right; border:1px solid #E2E5DF">${itemTax3}</td>
</tr>
<tr>
<td style="padding:2px; textAlign:left; border:1px solid #E2E5DF">${itemName4}</td>
<td style="padding:2px; textAlign:center; border:1px solid #E2E5DF">${itemSpec4}</td>
<td style="padding:2px; textAlign:center; border:1px solid #E2E5DF">${itemUnit4}</td>
<td style="padding:2px; textAlign:center; border:1px solid #E2E5DF">${itemQty4}</td>
<td style="padding:2px; textAlign:center; border:1px solid #E2E5DF">${itemPrice4}</td>
<td style="padding:2px; textAlign:right; border:1px solid #E2E5DF">${itemAmount4}</td>
<td style="padding:2px; textAlign:right; border:1px solid #E2E5DF">${itemTax4}</td>
</tr>
<tr>
<td style="padding:2px; textAlign:left; border:1px solid #E2E5DF">${itemName5}</td>
<td style="padding:2px; textAlign:center; border:1px solid #E2E5DF">${itemSpec5}</td>
<td style="padding:2px; textAlign:center; border:1px solid #E2E5DF">${itemUnit5}</td>
<td style="padding:2px; textAlign:center; border:1px solid #E2E5DF">${itemQty5}</td>
<td style="padding:2px; textAlign:center; border:1px solid #E2E5DF">${itemPrice5}</td>
<td style="padding:2px; textAlign:right; border:1px solid #E2E5DF">${itemAmount5}</td>
<td style="padding:2px; textAlign:right; border:1px solid #E2E5DF">${itemTax5}</td>
</tr>
<tr>
<td style="padding:2px; bold:true; backgroundColor:#F4F6F1; textAlign:center; border:1px solid #5F7E2A">'红冲合计'</td>
<td style="padding:2px; backgroundColor:#F4F6F1; border:1px solid #5F7E2A">'—'</td>
<td style="padding:2px; backgroundColor:#F4F6F1; border:1px solid #5F7E2A">'—'</td>
<td style="padding:2px; backgroundColor:#F4F6F1; border:1px solid #5F7E2A">'—'</td>
<td style="padding:2px; backgroundColor:#F4F6F1; border:1px solid #5F7E2A">'—'</td>
<td style="padding:2px; bold:true; backgroundColor:#F4F6F1; textAlign:right; border:1px solid #5F7E2A">'¥'${totalAmount}</td>
<td style="padding:2px; bold:true; backgroundColor:#F4F6F1; textAlign:right; border:1px solid #5F7E2A">'¥'${totalTax}</td>
</tr>
</table>
// 段落:box
<table style="width:100%; marginBottom:6px">
<tr>
<td style="width:150px; padding:5px; backgroundColor:#F4F6F1; bold:true; fontSize:9.5pt; color:#415D13; border:1px solid #CDD5C1">'红冲金额合计'</td>
<td style="padding:5px; fontSize:9pt; border:1px solid #CDD5C1">${totalLine}</td>
</tr>
</table>
<table style="width:100%; marginBottom:6px">
<tr>
<td style="width:150px; padding:5px; backgroundColor:#F4F6F1; bold:true; fontSize:9.5pt; color:#415D13; border:1px solid #CDD5C1">'备注'</td>
<td style="padding:5px; fontSize:9pt; border:1px solid #CDD5C1">${remark}</td>
</tr>
</table>
// 签章区:right
<!-- 签章占位区:后续加盖电子公章 / 骑缝章 / 打印二维码 -->
<table style="width:100%; fontSize:8pt; marginBottom:8px">
<tr>
<td style="width:32%; padding:5px; backgroundColor:#F4F6F1; fontSize:7.5pt; color:#78838C; border:1px solid #CDD5C1">'本联次加盖印章后有效,扫码可验真'</td>
<td style="padding:3px; textAlign:center; color:#5F7E2A; border:1px dashed #5F7E2A">'(公章 / 财务专用章占位)' '(骑缝章 / 二维码占位)'</td>
</tr>
<tr>
<td style="width:32%%; padding:4px">' '</td>
<td style="padding:3px; textAlign:center; color:#78838C; border:1px dashed #5F7E2A">'单位名称:'${sellerName} '骑缝章沿右边线骑缝加盖'</td>
</tr>
<tr>
<td style="width:32%%; padding:4px">' '</td>
<td style="padding:3px; textAlign:center; color:#78838C; border:1px dashed #5F7E2A">'签章日期:'${issueDate} '二维码打印于右上角方框内'</td>
</tr>
</table>
// 落款:right
<!-- 落款栏 -->
<table style="width:100%; fontSize:9pt; marginTop:6px">
<tr>
<td style="width:24%; padding:4px">' '</td>
<td style="width:38%; padding:4px; textAlign:center; color:#415D13">'申请方经办人:'${payee}</td>
<td style="width:38%; padding:4px; textAlign:center; color:#415D13">'复核:'${reviewer}</td>
<td style="padding:4px; textAlign:center; color:#415D13">'税务机关受理:'${drawer}</td>
</tr>
</table>
</body>
</pdf>同分组示例
本页 PDF、预览图、模板与源码均由 JQuick-Pdf 真实运行产出,可自由复制到你的项目中使用。