Invoice layout sample — generate this PDF with Java (Template)
Template1 页 A4 · 53 KB
A VAT invoice layout sample for demonstration only — not a real invoice.

Output specs
- PDF pages
- 1
- Chart type
- Template
- Template lines
- 141
- Source lines
- 214
Java source · JQuickBiz80InvoiceSample.java
This is the complete test class that produced the PDF above — copy it and run as-is; see the quick-start section for the Maven dependency.
/*
* 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.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class JQuickBiz80InvoiceSample {
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 + "80_invoice_sample.pdf";
private static final String TEMPLATE = "report/biz/80_invoice_sample.txt";
public static void main(String[] args) throws Exception {
new File(PDF_DIR).mkdirs();
new File(IMG_DIR).mkdirs();
// ---------- 1. 生成 logo 与发票专用章(模板 image src 按字面量引用固定路径) ----------
generateLogo(IMG_DIR + "80_logo.png");
generateInvoiceSeal(IMG_DIR + "80_seal.png");
// ---------- 2. 组织数据(普通 Map,无实体类)----------
Map<String, Object> data = new LinkedHashMap<String, Object>();
data.put("invoiceNo", "00000000");
data.put("invoiceDate", "2026年10月19日");
data.put("buyerName", "××城市商业银行股份有限公司");
data.put("buyerTaxNo", "91310000MA1FL8XXXX");
data.put("buyerAddressPhone", "××市浦东新区××路 88 号 021-6123-4567");
data.put("buyerBankAccount", "工商银行××支行 1001 2345 6789 0123");
data.put("sellerName", "××数字科技有限公司");
data.put("sellerTaxNo", "91310115MA1K9XXXX");
data.put("sellerAddressPhone", "××市××区科技园 8 号楼 021-6666-5678");
data.put("sellerBankAccount", "建设银行××支行 3100 1234 5678 9012");
// 明细行:名称 / 规格 / 单位 / 数量 / 不含税单价
List<String[]> items = new ArrayList<String[]>();
items.add(new String[]{"*软件*PDF 报表引擎企业版", "JQ-ENT-5N", "套", "5", "10619.47"});
items.add(new String[]{"*服务*模板迁移服务", "MIGRATE", "项", "1", "28318.58"});
items.add(new String[]{"*服务*现场培训服务", "TRAINING", "场", "2", "3773.58"});
BigDecimal taxRate = new BigDecimal("0.06");
BigDecimal sum = BigDecimal.ZERO;
String[] keys = {"name", "spec", "unit", "qty", "price"};
for (int i = 0; i < items.size(); i++) {
String[] it = items.get(i);
for (int j = 0; j < keys.length; j++) {
data.put("d" + (i + 1) + "_" + keys[j], it[j]);
}
BigDecimal amount = new BigDecimal(it[4]).multiply(new BigDecimal(it[3])).setScale(2, RoundingMode.HALF_UP);
data.put("d" + (i + 1) + "_amount", amount.toPlainString());
sum = sum.add(amount);
}
BigDecimal taxAmount = sum.multiply(taxRate).setScale(2, RoundingMode.HALF_UP);
BigDecimal total = sum.add(taxAmount).setScale(2, RoundingMode.HALF_UP);
data.put("taxRateText", "6%");
data.put("amountWithoutTax", sum.toPlainString());
data.put("taxAmount", taxAmount.toPlainString());
data.put("total", total.toPlainString());
data.put("totalInWords", amountInWords(total));
data.put("remark", "合同编号 HT-2026-0918,票样仅供样式验证");
// ---------- 3. 渲染 ----------
JPdfConfig config = new JPdfConfig();
config.getLayoutConfig().setFlexLayout(true);
config.getDoc().setMargins(Arrays.asList(46, 46, 50, 46));
// 票样水印:避免被误用
config.getWatermarkConfig().setEnabled(true);
config.getWatermarkConfig().setWatermarkText("票 样 非 报 销 凭 证");
config.getWatermarkConfig().setFillOpacity(0.12f);
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);
FileOutputStream out = new FileOutputStream(OUT_FILE);
out.write(pdf);
out.close();
System.out.println("已生成(票样):" + OUT_FILE);
}
/**
* 人民币金额大写。仅处理到「亿」,覆盖发票场景;小数部分按角分输出。
* 例:11010.00 → 壹万壹仟零壹拾元整。
*/
public static String amountInWords(BigDecimal amount) {
String[] digits = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
String[] units = {"", "拾", "佰", "仟"};
String[] groups = {"", "万", "亿"};
BigDecimal scaled = amount.setScale(2, RoundingMode.HALF_UP);
long integer = scaled.longValue();
int fraction = scaled.subtract(new BigDecimal(integer)).movePointRight(2).intValue();
String number = String.valueOf(integer);
StringBuilder result = new StringBuilder();
int groupIndex = 0;
boolean pendingZero = false;
while (number.length() > 0) {
int end = Math.max(0, number.length() - 4);
String group = number.substring(end);
number = number.substring(0, end);
if (Integer.parseInt(group) == 0) {
pendingZero = result.length() > 0;
groupIndex++;
continue;
}
StringBuilder part = new StringBuilder();
for (int i = 0; i < group.length(); i++) {
int d = group.charAt(i) - '0';
int pos = group.length() - 1 - i;
if (d == 0) {
if (part.length() > 0 && !part.toString().endsWith("零")) {
part.append("零");
}
} else {
part.append(digits[d]).append(units[pos]);
}
}
String text = part.toString();
if (text.endsWith("零")) {
text = text.substring(0, text.length() - 1);
}
if (pendingZero) {
result.insert(0, "零");
pendingZero = false;
}
result.insert(0, text + groups[groupIndex]);
groupIndex++;
}
if (result.length() == 0) {
result.append("零");
}
StringBuilder words = new StringBuilder(result).append("元");
if (fraction == 0) {
words.append("整");
} else {
int jiao = fraction / 10;
int fen = fraction % 10;
if (jiao > 0) {
words.append(digits[jiao]).append("角");
} else if (fen > 0) {
words.append("零");
}
if (fen > 0) {
words.append(digits[fen]).append("分");
}
}
return words.toString();
}
private static void generateLogo(String path) throws IOException {
BufferedImage image = new BufferedImage(88, 88, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(new Color(0x8C, 0x3B, 0x3B));
g.fillOval(2, 2, 84, 84);
g.setColor(Color.WHITE);
g.setFont(new Font("Microsoft YaHei", Font.BOLD, 26));
g.drawString("JQ", 24, 56);
g.dispose();
ImageIO.write(image, "png", new File(path));
}
private static void generateInvoiceSeal(String path) throws IOException {
int size = 200;
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, 15));
g.drawString("发票专用章", size / 2 - 40, 70);
g.setFont(new Font("Microsoft YaHei", Font.PLAIN, 11));
g.drawString("××数字科技有限公司", 22, 100);
g.setFont(new Font("Microsoft YaHei", Font.PLAIN, 10));
g.drawString("(票样)", size / 2 - 16, 150);
g.dispose();
ImageIO.write(image, "png", new File(path));
}
}Minimal invocation (3 lines)
Skip the full test class — the core is these three lines: bind the chart SVG, execute the template, get the PDF bytes. Copy them straight into your project.
// config 可省略;svg 来自 JChartRendererFactory.renderChart(...),见下方完整源码
byte[] pdf = new JQuickPdfFactory(config).executeResource("report/biz/80_invoice_sample.txt");Template file · 80_invoice_sample.txt
An HTML-like template with inline CSS that renders to PDF once bound to data. The full template is below — copy it or download the raw file.
<pdf>
<body>
<div style="border:2px solid #8C3B3B; padding:10px">
// ---------- 标题与编号 ----------
<div style="display:flex; justifyContent:space-between">
<div style="width:100px">
<image src='D:/test/image/80_logo.png' style="width:44px; height:44px"></image>
</div>
<div style="width:300px; textAlignment:center">
<h1 style="fontSize:19pt; bold:true; color:#8C3B3B; characterSpacing:5pt">'增值税普通发票'</h1>
<p style="fontSize:9pt; color:#8C3B3B; textAlignment:center; characterSpacing:2pt">'(票 样)'</p>
</div>
<div style="width:110px; textAlignment:right">
<p style="fontSize:8pt; color:#5A6B7C">'发票代码:000000000000'</p>
<p style="fontSize:8pt; color:#5A6B7C; marginTop:3px">'发票号码:'${invoiceNo}</p>
<p style="fontSize:8pt; color:#5A6B7C; marginTop:3px">'开票日期:'${invoiceDate}</p>
</div>
</div>
// ---------- 购买方(不支持 colspan,统一两列结构) ----------
<table style="width:100%; fontSize:8.5pt; marginTop:6px">
<tr>
<td style="width:52px; padding:4px; backgroundColor:#F7EFEF; bold:true; color:#8C3B3B; border:1px solid #8C3B3B">'名 称'</td>
<td style="padding:4px; border:1px solid #8C3B3B; bold:true">${buyerName}</td>
</tr>
<tr>
<td style="width:52px; padding:4px; border:1px solid #8C3B3B">'税 号'</td>
<td style="padding:4px; border:1px solid #8C3B3B">${buyerTaxNo}</td>
</tr>
<tr>
<td style="width:52px; padding:4px; border:1px solid #8C3B3B">'地址电话'</td>
<td style="padding:4px; border:1px solid #8C3B3B">${buyerAddressPhone}</td>
</tr>
<tr>
<td style="width:52px; padding:4px; border:1px solid #8C3B3B">'开户行'</td>
<td style="padding:4px; border:1px solid #8C3B3B">${buyerBankAccount}</td>
</tr>
<tr>
<td style="width:52px; padding:4px; backgroundColor:#F7EFEF; bold:true; color:#8C3B3B; border:1px solid #8C3B3B">'名 称'</td>
<td style="padding:4px; border:1px solid #8C3B3B; bold:true">${sellerName}</td>
</tr>
<tr>
<td style="width:52px; padding:4px; border:1px solid #8C3B3B">'税 号'</td>
<td style="padding:4px; border:1px solid #8C3B3B">${sellerTaxNo}</td>
</tr>
<tr>
<td style="width:52px; padding:4px; border:1px solid #8C3B3B">'地址电话'</td>
<td style="padding:4px; border:1px solid #8C3B3B">${sellerAddressPhone}</td>
</tr>
<tr>
<td style="width:52px; padding:4px; border:1px solid #8C3B3B">'开户行'</td>
<td style="padding:4px; border:1px solid #8C3B3B">${sellerBankAccount}</td>
</tr>
</table>
// ---------- 明细 ----------
<table style="width:100%; fontSize:8.5pt; marginTop:2px">
<tr>
<th style="width:150px; padding:4px; backgroundColor:#F7EFEF; color:#8C3B3B; textAlign:center; border:1px solid #8C3B3B">'货物或应税劳务名称'</th>
<th style="width:70px; padding:4px; backgroundColor:#F7EFEF; color:#8C3B3B; textAlign:center; border:1px solid #8C3B3B">'规格型号'</th>
<th style="width:34px; padding:4px; backgroundColor:#F7EFEF; color:#8C3B3B; textAlign:center; border:1px solid #8C3B3B">'单位'</th>
<th style="width:40px; padding:4px; backgroundColor:#F7EFEF; color:#8C3B3B; textAlign:center; border:1px solid #8C3B3B">'数量'</th>
<th style="width:60px; padding:4px; backgroundColor:#F7EFEF; color:#8C3B3B; textAlign:center; border:1px solid #8C3B3B">'单价'</th>
<th style="width:70px; padding:4px; backgroundColor:#F7EFEF; color:#8C3B3B; textAlign:center; border:1px solid #8C3B3B">'金额'</th>
<th style="padding:4px; backgroundColor:#F7EFEF; color:#8C3B3B; textAlign:center; border:1px solid #8C3B3B">'税率'</th>
</tr>
<tr>
<td style="padding:4px; border:1px solid #8C3B3B">${d1_name}</td>
<td style="padding:4px; border:1px solid #8C3B3B">${d1_spec}</td>
<td style="padding:4px; textAlign:center; border:1px solid #8C3B3B">${d1_unit}</td>
<td style="padding:4px; textAlign:right; border:1px solid #8C3B3B">${d1_qty}</td>
<td style="padding:4px; textAlign:right; border:1px solid #8C3B3B">${d1_price}</td>
<td style="padding:4px; textAlign:right; border:1px solid #8C3B3B">${d1_amount}</td>
<td style="padding:4px; textAlign:center; border:1px solid #8C3B3B">${taxRateText}</td>
</tr>
<tr>
<td style="padding:4px; border:1px solid #8C3B3B">${d2_name}</td>
<td style="padding:4px; border:1px solid #8C3B3B">${d2_spec}</td>
<td style="padding:4px; textAlign:center; border:1px solid #8C3B3B">${d2_unit}</td>
<td style="padding:4px; textAlign:right; border:1px solid #8C3B3B">${d2_qty}</td>
<td style="padding:4px; textAlign:right; border:1px solid #8C3B3B">${d2_price}</td>
<td style="padding:4px; textAlign:right; border:1px solid #8C3B3B">${d2_amount}</td>
<td style="padding:4px; textAlign:center; border:1px solid #8C3B3B">${taxRateText}</td>
</tr>
<tr>
<td style="padding:4px; border:1px solid #8C3B3B">${d3_name}</td>
<td style="padding:4px; border:1px solid #8C3B3B">${d3_spec}</td>
<td style="padding:4px; textAlign:center; border:1px solid #8C3B3B">${d3_unit}</td>
<td style="padding:4px; textAlign:right; border:1px solid #8C3B3B">${d3_qty}</td>
<td style="padding:4px; textAlign:right; border:1px solid #8C3B3B">${d3_price}</td>
<td style="padding:4px; textAlign:right; border:1px solid #8C3B3B">${d3_amount}</td>
<td style="padding:4px; textAlign:center; border:1px solid #8C3B3B">${taxRateText}</td>
</tr>
// 合计行(不支持 colspan,中间列 '—' 占位)
<tr>
<td style="padding:4px; border:1px solid #8C3B3B; bold:true">'合计'</td>
<td style="padding:4px; textAlign:center; border:1px solid #8C3B3B">'—'</td>
<td style="padding:4px; textAlign:center; border:1px solid #8C3B3B">'—'</td>
<td style="padding:4px; textAlign:center; border:1px solid #8C3B3B">'—'</td>
<td style="padding:4px; textAlign:center; border:1px solid #8C3B3B">'—'</td>
<td style="padding:4px; border:1px solid #8C3B3B; bold:true; textAlign:right">${amountWithoutTax}</td>
<td style="padding:4px; textAlign:center; border:1px solid #8C3B3B">' '</td>
</tr>
<tr>
<td style="padding:4px; border:1px solid #8C3B3B">'价税合计(大写):'${totalInWords}</td>
<td style="padding:4px; textAlign:center; border:1px solid #8C3B3B">'—'</td>
<td style="padding:4px; textAlign:center; border:1px solid #8C3B3B">'—'</td>
<td style="padding:4px; textAlign:center; border:1px solid #8C3B3B">'—'</td>
<td style="padding:4px; textAlign:center; border:1px solid #8C3B3B">'—'</td>
<td style="padding:4px; border:1px solid #8C3B3B; bold:true; textAlign:right; color:#8C3B3B">'¥'${total}</td>
<td style="padding:4px; textAlign:center; border:1px solid #8C3B3B">'—'</td>
</tr>
</table>
// ---------- 税额与备注 ----------
<table style="width:100%; fontSize:8.5pt">
<tr>
<td style="padding:4px; border:1px solid #8C3B3B; width:130px">'不含税金额:¥'${amountWithoutTax}</td>
<td style="padding:4px; border:1px solid #8C3B3B; width:110px">'税额:¥'${taxAmount}</td>
<td style="padding:4px; border:1px solid #8C3B3B">'备注:'${remark}</td>
</tr>
</table>
// ---------- 底部:开票人 + 销售方章 ----------
<div style="display:flex; justifyContent:space-between; marginTop:8px">
<div style="width:250px">
<p style="fontSize:8.5pt; color:#5A6B7C">'收款人:王芳 复核:李静 开票人:陈立'</p>
<p style="fontSize:8.5pt; color:#5A6B7C; marginTop:4px">'销售方:(盖章)'</p>
<p style="fontSize:7.5pt; color:#8a939b; marginTop:10px">'本票样由系统生成,仅供开发调试与样式验证,不具有任何税务效力。'</p>
</div>
<div style="width:150px; textAlignment:center">
<image src='D:/test/image/80_seal.png' style="width:92px; height:92px"></image>
<p style="fontSize:8pt; color:#8C3B3B; textAlignment:center">'发票专用章(票样)'</p>
</div>
</div>
</div>
</body>
</pdf>More demos in this group
The PDF, preview image, template and source code on this page were all produced by a real JQuick-Pdf run and are free to copy into your own project.