跳转到内容

Java 生成「二手车销售统一发票」PDF 示例(Template)

JQuick-Pdf财税票据 Demo

Template1 页 A4 · 26 KB

二手车交易发票票样,含车辆过户信息栏。

下载 PDF 文件查看模板原始文件返回全部示例

二手车销售统一发票 — Template
渲染效果

输出规格

PDF 页数
1
图表类型
Template
模板行数
148
源码行数
200

Java 源码 · JQuickBiz104UsedCarSalesInvoice.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;

/**
 * 单据 104:二手车销售统一发票(finance 领域第 5 项)。
 *
 * <p>本类为独立可运行 Demo:自带 {@code main} 方法、不依赖任何公共工具类或实体类,
 * 数据以 key-value 形式通过 {@link JQuickPdfFactory#bindAll(Map)} 注入,
 * 版式全部落在模板文件 {@code demo/biz/finance/104_used_car_sales_invoice.txt} 中。</p>
 *
 * <p>视觉主题「赭红·厚框·公文」:主色 #A86833、辅色 #279190、浅底 #F6F3F1;
 * 版式为 doc 抬头 + grid2 信息区 + solid 明细表 + three 签章区 + table 落款,
 * 与同领域其它单据互不重样。</p>
 *
 * <p>对标二手车销售统一发票票样:买卖双方身份信息、车辆识别代号、牌照号码、</p>
 * <p>车价款与代办费用明细、合计金额大小写与市场公章占位。</p>
 *
 * <p>运行后输出:{@code D:/test/pdf/104_used_car_sales_invoice.pdf}。</p>
 *
 * @author Martin
 * @version 1.0.0
 */
public class JQuickBiz104UsedCarSalesInvoice {

    private static final String PDF_DIR = "D:/test/pdf/";
    private static final String OUT_FILE = PDF_DIR + "104_used_car_sales_invoice.pdf";
    private static final String TEMPLATE = "demo/biz/finance/104_used_car_sales_invoice.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 = {
                {"二手车车价款", "小型轿车", "××牌 SVW71810", "2021-05-18", "4.60", "86800.00"},
                {"过户手续费", "—", "—", "—", "—", "420.00"},
                {"评估费", "—", "—", "—", "—", "380.00"},
                {"代办服务费", "—", "—", "—", "—", "600.00"},
                {"整备清洗费", "—", "—", "—", "—", "350.00"}
        };

        Map<String, Object> data = new LinkedHashMap<String, Object>();
        data.put("invoiceCode", "031002100511");
        data.put("invoiceNo", "00551883");
        data.put("issueDate", "2026年09月26日");
        data.put("sellerName", "××二手车经销有限公司 / 91310100MA1K3T92XG");
        data.put("buyerName", "崔行知 / 3201××××××××××××××");
        data.put("marketName", "××市旧机动车交易市场");
        data.put("vinNo", "LSVAM4182N2190876");
        data.put("plateNo", "苏A·8Z76K");
        data.put("remark", "二手车销售统一发票由二手车交易市场或经销企业开具,车价款按差额征税,过户时须与车辆登记证书一并提交。");
        data.put("drawer", "苏见宁");
        data.put("payee", "邵怀信");
        data.put("buyerSign", "崔行知");

        // 明细逐格展开,并累计金额
        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, "—");
                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("totalAmount", itemTotal.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString());
        data.put("totalAmountInWords", toChineseAmount(itemTotal));

        data.put("totalLine", "合计金额(大写)" + toChineseAmount(itemTotal) + "  (小写)¥" + itemTotal.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString());

        // ---------- 2. 渲染 ----------
        JPdfConfig config = new JPdfConfig();
        config.getLayoutConfig().setFlexLayout(true);
        config.getDoc().setMargins(Arrays.asList(36, 36, 36, 36));
        config.getFontConfig().setDefaultFontName("Noto Sans CJK SC");
        config.getHeaderConfig().setEnabled(true);
        config.getHeaderConfig().setText("××市旧机动车交易市场 二手车销售统一发票 00551883");
        config.getHeaderConfig().setFontSize(8.5f);
        config.getHeaderConfig().setHeight(28);
        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/104_used_car_sales_invoice.txt");

模板文件 · 104_used_car_sales_invoice.txt

类 HTML + 内联 CSS 的模板语法,绑定数据后渲染为 PDF。下方为完整模板,可直接复制,也可下载原始文件。

<pdf>
<body>
  // 104 二手车销售统一发票(finance 领域第 5 项) 视觉主题:赭红·厚框·公文
  // 版式组合:抬头=doc 信息区=grid2 明细表=solid 段落=bar 签章=three 落款=table
  // 主色 #A86833 辅色 #279190 浅底 #F6F3F1
  // 二手车销售统一发票:含车辆识别代号、牌照号、初次登记日期与里程,按差额征税。

  // 抬头:doc
  <p style="fontSize:20pt; bold:true; color:#A86833; textAlignment:center; characterSpacing:9pt; marginBottom:2px">'二手车销售统一发票'</p>
  <p style="fontSize:8pt; color:#78838C; textAlignment:center; marginBottom:4px">'发票代码 '${invoiceCode}'  发票号码 '${invoiceNo}'  开票日期 '${issueDate}</p>
  <lineSeparator style="color:#A86833; thickness:2f; marginBottom:8px"></lineSeparator>

  // 信息区:grid2
  <table style="width:100%; fontSize:8.5pt; marginBottom:8px">
    <tr>
      <td style="width:16%; padding:4px; backgroundColor:#F6F3F1; bold:true; border:1px solid #D5CAC0">'发票代码'</td>
      <td style="width:34%; padding:4px; border:1px solid #D5CAC0">${invoiceCode}</td>
      <td style="width:16%; padding:4px; backgroundColor:#F6F3F1; bold:true; border:1px solid #D5CAC0">'发票号码'</td>
      <td style="padding:4px; border:1px solid #D5CAC0">${invoiceNo}</td>
    </tr>
    <tr>
      <td style="width:16%; padding:4px; backgroundColor:#F6F3F1; bold:true; border:1px solid #D5CAC0">'开票日期'</td>
      <td style="width:34%; padding:4px; border:1px solid #D5CAC0">${issueDate}</td>
      <td style="width:16%; padding:4px; backgroundColor:#F6F3F1; bold:true; border:1px solid #D5CAC0">'卖方名称 / 身份证号'</td>
      <td style="padding:4px; border:1px solid #D5CAC0">${sellerName}</td>
    </tr>
    <tr>
      <td style="width:16%; padding:4px; backgroundColor:#F6F3F1; bold:true; border:1px solid #D5CAC0">'买方名称 / 身份证号'</td>
      <td style="width:34%; padding:4px; border:1px solid #D5CAC0">${buyerName}</td>
      <td style="width:16%; padding:4px; backgroundColor:#F6F3F1; bold:true; border:1px solid #D5CAC0">'二手车交易市场'</td>
      <td style="padding:4px; border:1px solid #D5CAC0">${marketName}</td>
    </tr>
    <tr>
      <td style="width:16%; padding:4px; backgroundColor:#F6F3F1; bold:true; border:1px solid #D5CAC0">'车辆识别代号'</td>
      <td style="width:34%; padding:4px; border:1px solid #D5CAC0">${vinNo}</td>
      <td style="width:16%; padding:4px; backgroundColor:#F6F3F1; bold:true; border:1px solid #D5CAC0">'牌照号码'</td>
      <td style="padding:4px; border:1px solid #D5CAC0">${plateNo}</td>
    </tr>
  </table>

  <p style="fontSize:10.5pt; bold:true; color:#8A4D19; marginBottom:4px">'车辆及收费明细'</p>
  <table style="width:100%; fontSize:8pt; marginBottom:8px">
    <tr>
      <th style="width:24%; padding:4px; backgroundColor:#A86833; color:white; bold:true; textAlign:center; border:1px solid #A86833">'项目'</th>
      <th style="width:14%; padding:4px; backgroundColor:#A86833; color:white; bold:true; textAlign:center; border:1px solid #A86833">'车辆类型'</th>
      <th style="width:18%; padding:4px; backgroundColor:#A86833; color:white; bold:true; textAlign:center; border:1px solid #A86833">'厂牌型号'</th>
      <th style="width:14%; padding:4px; backgroundColor:#A86833; color:white; bold:true; textAlign:center; border:1px solid #A86833">'初次登记日期'</th>
      <th style="width:12%; padding:4px; backgroundColor:#A86833; color:white; bold:true; textAlign:center; border:1px solid #A86833">'里程(万公里)'</th>
      <th style="padding:4px; backgroundColor:#A86833; color:white; bold:true; textAlign:center; border:1px solid #A86833">'金额'</th>
    </tr>
    <tr>
      <td style="padding:4px; textAlign:left; border:1px solid #D5CAC0">${itemName1}</td>
      <td style="padding:4px; textAlign:center; border:1px solid #D5CAC0">${itemSpec1}</td>
      <td style="padding:4px; textAlign:center; border:1px solid #D5CAC0">${itemUnit1}</td>
      <td style="padding:4px; textAlign:center; border:1px solid #D5CAC0">${itemQty1}</td>
      <td style="padding:4px; textAlign:center; border:1px solid #D5CAC0">${itemPrice1}</td>
      <td style="padding:4px; textAlign:right; border:1px solid #D5CAC0">${itemAmount1}</td>
    </tr>
    <tr>
      <td style="padding:4px; textAlign:left; border:1px solid #D5CAC0">${itemName2}</td>
      <td style="padding:4px; textAlign:center; border:1px solid #D5CAC0">${itemSpec2}</td>
      <td style="padding:4px; textAlign:center; border:1px solid #D5CAC0">${itemUnit2}</td>
      <td style="padding:4px; textAlign:center; border:1px solid #D5CAC0">${itemQty2}</td>
      <td style="padding:4px; textAlign:center; border:1px solid #D5CAC0">${itemPrice2}</td>
      <td style="padding:4px; textAlign:right; border:1px solid #D5CAC0">${itemAmount2}</td>
    </tr>
    <tr>
      <td style="padding:4px; textAlign:left; border:1px solid #D5CAC0">${itemName3}</td>
      <td style="padding:4px; textAlign:center; border:1px solid #D5CAC0">${itemSpec3}</td>
      <td style="padding:4px; textAlign:center; border:1px solid #D5CAC0">${itemUnit3}</td>
      <td style="padding:4px; textAlign:center; border:1px solid #D5CAC0">${itemQty3}</td>
      <td style="padding:4px; textAlign:center; border:1px solid #D5CAC0">${itemPrice3}</td>
      <td style="padding:4px; textAlign:right; border:1px solid #D5CAC0">${itemAmount3}</td>
    </tr>
    <tr>
      <td style="padding:4px; textAlign:left; border:1px solid #D5CAC0">${itemName4}</td>
      <td style="padding:4px; textAlign:center; border:1px solid #D5CAC0">${itemSpec4}</td>
      <td style="padding:4px; textAlign:center; border:1px solid #D5CAC0">${itemUnit4}</td>
      <td style="padding:4px; textAlign:center; border:1px solid #D5CAC0">${itemQty4}</td>
      <td style="padding:4px; textAlign:center; border:1px solid #D5CAC0">${itemPrice4}</td>
      <td style="padding:4px; textAlign:right; border:1px solid #D5CAC0">${itemAmount4}</td>
    </tr>
    <tr>
      <td style="padding:4px; textAlign:left; border:1px solid #D5CAC0">${itemName5}</td>
      <td style="padding:4px; textAlign:center; border:1px solid #D5CAC0">${itemSpec5}</td>
      <td style="padding:4px; textAlign:center; border:1px solid #D5CAC0">${itemUnit5}</td>
      <td style="padding:4px; textAlign:center; border:1px solid #D5CAC0">${itemQty5}</td>
      <td style="padding:4px; textAlign:center; border:1px solid #D5CAC0">${itemPrice5}</td>
      <td style="padding:4px; textAlign:right; border:1px solid #D5CAC0">${itemAmount5}</td>
    </tr>
    <tr>
      <td style="padding:4px; bold:true; backgroundColor:#F6F3F1; textAlign:center; border:1px solid #A86833">'合 计'</td>
      <td style="padding:4px; backgroundColor:#F6F3F1; border:1px solid #A86833">'—'</td>
      <td style="padding:4px; backgroundColor:#F6F3F1; border:1px solid #A86833">'—'</td>
      <td style="padding:4px; backgroundColor:#F6F3F1; border:1px solid #A86833">'—'</td>
      <td style="padding:4px; backgroundColor:#F6F3F1; border:1px solid #A86833">'—'</td>
      <td style="padding:4px; bold:true; backgroundColor:#F6F3F1; textAlign:right; border:1px solid #A86833">'¥'${totalAmount}</td>
    </tr>
  </table>

  // 段落:bar
  <table style="width:100%; marginBottom:2px">
    <tr>
      <td style="width:4px; padding:0px; backgroundColor:#279190">' '</td>
      <td style="padding:2px; bold:true; fontSize:10.5pt; color:#8A4D19">'金额合计'</td>
    </tr>
  </table>
  <p style="fontSize:9pt; marginBottom:6px">${totalLine}</p>
  <table style="width:100%; marginBottom:2px">
    <tr>
      <td style="width:4px; padding:0px; backgroundColor:#279190">' '</td>
      <td style="padding:2px; bold:true; fontSize:10.5pt; color:#8A4D19">'备注'</td>
    </tr>
  </table>
  <p style="fontSize:9pt; marginBottom:6px">${remark}</p>

  // 签章区:three
  <!-- 签章占位区:后续加盖电子公章 / 骑缝章 / 打印二维码 -->
  <table style="width:100%; fontSize:8pt; marginBottom:8px">
    <tr>
      <td style="width:33%; padding:4px; bold:true; textAlign:center; color:#A86833; border:1px dashed #A86833">'(公章 / 财务专用章占位)'</td>
      <td style="width:33%; padding:4px; bold:true; textAlign:center; color:#A86833; border:1px dashed #A86833">'(骑缝章 / 二维码占位)'</td>
      <td style="width:33%; padding:4px; bold:true; textAlign:center; color:#A86833; border:1px dashed #A86833">'(二维码占位)'</td>
    </tr>
    <tr>
      <td style="width:33%; padding:4px; textAlign:center; color:#78838C; border:1px dashed #A86833">'单位名称:'${marketName}</td>
      <td style="width:33%; padding:4px; textAlign:center; color:#78838C; border:1px dashed #A86833">'骑缝章沿右边线骑缝加盖'</td>
      <td style="width:33%; padding:4px; textAlign:center; color:#78838C; border:1px dashed #A86833">'扫码验真'</td>
    </tr>
    <tr>
      <td style="width:33%; padding:4px; textAlign:center; color:#78838C; border:1px dashed #A86833">'签章日期:'${issueDate}</td>
      <td style="width:33%; padding:4px; textAlign:center; color:#78838C; border:1px dashed #A86833">'二维码打印于右上角方框内'</td>
      <td style="width:33%; padding:4px; textAlign:center; color:#78838C; border:1px dashed #A86833">'尺寸 22mm×22mm'</td>
    </tr>
  </table>

  // 落款:table
  <!-- 落款栏 -->
  <table style="width:100%; fontSize:8.5pt">
    <tr>
      <td style="width:33%; padding:5px; border:1px solid #D5CAC0">'开票人:'${drawer}</td>
      <td style="width:33%; padding:5px; border:1px solid #D5CAC0">'收款人:'${payee}</td>
      <td style="padding:5px; border:1px solid #D5CAC0">'买方签收:'${buyerSign}</td>
    </tr>
  </table>
</body>
</pdf>

同分组示例

本页 PDF、预览图、模板与源码均由 JQuick-Pdf 真实运行产出,可自由复制到你的项目中使用。