跳转到内容

Java 生成「通行费电子发票」PDF 示例(Template)

JQuick-Pdf财税票据 Demo

Template1 页 A4 · 25 KB

通行费电子发票票样,适合 ETC / 高速通行费场景。

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

通行费电子发票 — Template
渲染效果

输出规格

PDF 页数
1
图表类型
Template
模板行数
132
源码行数
202

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

/**
 * 单据 105:通行费电子发票(finance 领域第 6 项)。
 *
 * <p>本类为独立可运行 Demo:自带 {@code main} 方法、不依赖任何公共工具类或实体类,
 * 数据以 key-value 形式通过 {@link JQuickPdfFactory#bindAll(Map)} 注入,
 * 版式全部落在模板文件 {@code demo/biz/finance/105_toll_e_invoice.txt} 中。</p>
 *
 * <p>视觉主题「赭红·斑马·通栏」:主色 #963812、辅色 #27917E、浅底 #F8F2EF;
 * 版式为 banner 抬头 + chip 信息区 + striped 明细表 + right 签章区 + plain 落款,
 * 与同领域其它单据互不重样。</p>
 *
 * <p>对标通行费电子发票票样:通行日期、出入口收费站、车型、里程、通行费金额,</p>
 * <p>以及按 3% 征收率计算的可抵扣进项税额与发票专用章占位。</p>
 *
 * <p>运行后输出:{@code D:/test/pdf/105_toll_e_invoice.pdf}。</p>
 *
 * @author Martin
 * @version 1.0.0
 */
public class JQuickBiz105TollEInvoice {

    private static final String PDF_DIR = "D:/test/pdf/";
    private static final String OUT_FILE = PDF_DIR + "105_toll_e_invoice.pdf";
    private static final String TEMPLATE = "demo/biz/finance/105_toll_e_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 = {
                {"2026-09-03", "××北收费站", "××西收费站", "货三", "126.40", "218.00"},
                {"2026-09-09", "××西收费站", "××南收费站", "货三", "98.70", "176.50"},
                {"2026-09-14", "××南收费站", "××北收费站", "货三", "153.20", "264.80"},
                {"2026-09-19", "××东收费站", "××西收费站", "货三", "87.50", "152.30"},
                {"2026-09-24", "××西收费站", "××北收费站", "货三", "126.40", "218.00"}
        };

        Map<String, Object> data = new LinkedHashMap<String, Object>();
        data.put("invoiceCode", "091002300611");
        data.put("invoiceNo", "11880276");
        data.put("issueDate", "2026年09月28日");
        data.put("buyerName", "××物流有限公司");
        data.put("buyerTaxNo", "91320500MA1M8T3X7Y");
        data.put("sellerName", "××高速公路管理有限公司");
        data.put("sellerTaxNo", "91310000MA1FL2Q88K");
        data.put("tollMonth", "2026年09月");
        data.put("remark", "通行费电子发票由收费公路经营管理单位开具,ETC 用户可通过票根平台按月汇总开票。");
        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, "—");
                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());

        data.put("taxLine", "可抵扣进项税额:¥" + itemTotal.divide(new BigDecimal("1.03"), 2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("0.03")).setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "  征收率 3%");

        // ---------- 2. 渲染 ----------
        JPdfConfig config = new JPdfConfig();
        config.getLayoutConfig().setFlexLayout(true);
        config.getDoc().setMargins(Arrays.asList(40, 40, 40, 40));
        config.getFontConfig().setDefaultFontName("Noto Sans CJK SC");
        config.getHeaderConfig().setEnabled(true);
        config.getHeaderConfig().setText("××高速公路管理有限公司 通行费电子发票 11880276");
        config.getHeaderConfig().setFontSize(7.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/105_toll_e_invoice.txt");

模板文件 · 105_toll_e_invoice.txt

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

<pdf>
<body>
  // 105 通行费电子发票(finance 领域第 6 项) 视觉主题:赭红·斑马·通栏
  // 版式组合:抬头=banner 信息区=chip 明细表=striped 段落=plain 签章=right 落款=plain
  // 主色 #963812 辅色 #27917E 浅底 #F8F2EF
  // 通行费电子发票:按通行月份汇总,含入口 / 出口站、车型、里程与可抵扣进项税额。

  // 抬头:banner
  <table style="width:100%; marginBottom:6px">
    <tr>
      <td style="width:58%; padding:8px; backgroundColor:#963812; color:white; bold:true; fontSize:15pt; characterSpacing:4pt">'通行费电子发票'</td>
      <td style="padding:8px; backgroundColor:#6D2203; color:white; textAlign:right; fontSize:7.5pt">'发票代码 '${invoiceCode}'  发票号码 '${invoiceNo}'  开票日期 '${issueDate}</td>
    </tr>
  </table>

  // 信息区:chip
  <table style="width:100%; fontSize:8.5pt; marginBottom:8px">
    <tr>
      <td style="width:25%; padding:4px; border:1px solid #DAC4BB">'发票代码:'${invoiceCode}</td>
      <td style="width:25%; padding:4px; border:1px solid #DAC4BB">'发票号码:'${invoiceNo}</td>
      <td style="width:25%; padding:4px; border:1px solid #DAC4BB">'开票日期:'${issueDate}</td>
      <td style="padding:4px; border:1px solid #DAC4BB">'购买方名称:'${buyerName}</td>
    </tr>
    <tr>
      <td style="width:25%; padding:4px; border:1px solid #DAC4BB">'购买方纳税人识别号:'${buyerTaxNo}</td>
      <td style="width:25%; padding:4px; border:1px solid #DAC4BB">'销售方名称:'${sellerName}</td>
      <td style="width:25%; padding:4px; border:1px solid #DAC4BB">'销售方纳税人识别号:'${sellerTaxNo}</td>
      <td style="padding:4px; border:1px solid #DAC4BB">'通行月份:'${tollMonth}</td>
    </tr>
  </table>

  <table style="width:100%; marginBottom:3px">
    <tr>
      <td style="padding:4px; backgroundColor:#963812; color:white; bold:true; fontSize:10pt; characterSpacing:2pt">'通行明细'</td>
    </tr>
  </table>
  <table style="width:100%; fontSize:8pt; marginBottom:8px">
    <tr>
      <th style="width:14%; padding:4px; backgroundColor:#963812; color:white; bold:true; textAlign:center; border:1px solid #963812">'通行日期'</th>
      <th style="width:18%; padding:4px; backgroundColor:#963812; color:white; bold:true; textAlign:center; border:1px solid #963812">'入口站'</th>
      <th style="width:18%; padding:4px; backgroundColor:#963812; color:white; bold:true; textAlign:center; border:1px solid #963812">'出口站'</th>
      <th style="width:10%; padding:4px; backgroundColor:#963812; color:white; bold:true; textAlign:center; border:1px solid #963812">'车型'</th>
      <th style="width:12%; padding:4px; backgroundColor:#963812; color:white; bold:true; textAlign:center; border:1px solid #963812">'里程(公里)'</th>
      <th style="padding:4px; backgroundColor:#963812; color:white; bold:true; textAlign:center; border:1px solid #963812">'通行费金额'</th>
    </tr>
    <tr>
      <td style="padding:4px; textAlign:center; border:1px solid #DAC4BB">${itemName1}</td>
      <td style="padding:4px; textAlign:center; border:1px solid #DAC4BB">${itemSpec1}</td>
      <td style="padding:4px; textAlign:center; border:1px solid #DAC4BB">${itemUnit1}</td>
      <td style="padding:4px; textAlign:center; border:1px solid #DAC4BB">${itemQty1}</td>
      <td style="padding:4px; textAlign:center; border:1px solid #DAC4BB">${itemPrice1}</td>
      <td style="padding:4px; textAlign:right; border:1px solid #DAC4BB">${itemAmount1}</td>
    </tr>
    <tr>
      <td style="backgroundColor:#F8F2EF; padding:4px; textAlign:center; border:1px solid #DAC4BB">${itemName2}</td>
      <td style="backgroundColor:#F8F2EF; padding:4px; textAlign:center; border:1px solid #DAC4BB">${itemSpec2}</td>
      <td style="backgroundColor:#F8F2EF; padding:4px; textAlign:center; border:1px solid #DAC4BB">${itemUnit2}</td>
      <td style="backgroundColor:#F8F2EF; padding:4px; textAlign:center; border:1px solid #DAC4BB">${itemQty2}</td>
      <td style="backgroundColor:#F8F2EF; padding:4px; textAlign:center; border:1px solid #DAC4BB">${itemPrice2}</td>
      <td style="backgroundColor:#F8F2EF; padding:4px; textAlign:right; border:1px solid #DAC4BB">${itemAmount2}</td>
    </tr>
    <tr>
      <td style="padding:4px; textAlign:center; border:1px solid #DAC4BB">${itemName3}</td>
      <td style="padding:4px; textAlign:center; border:1px solid #DAC4BB">${itemSpec3}</td>
      <td style="padding:4px; textAlign:center; border:1px solid #DAC4BB">${itemUnit3}</td>
      <td style="padding:4px; textAlign:center; border:1px solid #DAC4BB">${itemQty3}</td>
      <td style="padding:4px; textAlign:center; border:1px solid #DAC4BB">${itemPrice3}</td>
      <td style="padding:4px; textAlign:right; border:1px solid #DAC4BB">${itemAmount3}</td>
    </tr>
    <tr>
      <td style="backgroundColor:#F8F2EF; padding:4px; textAlign:center; border:1px solid #DAC4BB">${itemName4}</td>
      <td style="backgroundColor:#F8F2EF; padding:4px; textAlign:center; border:1px solid #DAC4BB">${itemSpec4}</td>
      <td style="backgroundColor:#F8F2EF; padding:4px; textAlign:center; border:1px solid #DAC4BB">${itemUnit4}</td>
      <td style="backgroundColor:#F8F2EF; padding:4px; textAlign:center; border:1px solid #DAC4BB">${itemQty4}</td>
      <td style="backgroundColor:#F8F2EF; padding:4px; textAlign:center; border:1px solid #DAC4BB">${itemPrice4}</td>
      <td style="backgroundColor:#F8F2EF; padding:4px; textAlign:right; border:1px solid #DAC4BB">${itemAmount4}</td>
    </tr>
    <tr>
      <td style="padding:4px; textAlign:center; border:1px solid #DAC4BB">${itemName5}</td>
      <td style="padding:4px; textAlign:center; border:1px solid #DAC4BB">${itemSpec5}</td>
      <td style="padding:4px; textAlign:center; border:1px solid #DAC4BB">${itemUnit5}</td>
      <td style="padding:4px; textAlign:center; border:1px solid #DAC4BB">${itemQty5}</td>
      <td style="padding:4px; textAlign:center; border:1px solid #DAC4BB">${itemPrice5}</td>
      <td style="padding:4px; textAlign:right; border:1px solid #DAC4BB">${itemAmount5}</td>
    </tr>
    <tr>
      <td style="padding:4px; bold:true; backgroundColor:#F8F2EF; textAlign:center; border:1px solid #963812">'合 计'</td>
      <td style="padding:4px; backgroundColor:#F8F2EF; border:1px solid #963812">'—'</td>
      <td style="padding:4px; backgroundColor:#F8F2EF; border:1px solid #963812">'—'</td>
      <td style="padding:4px; backgroundColor:#F8F2EF; border:1px solid #963812">'—'</td>
      <td style="padding:4px; backgroundColor:#F8F2EF; border:1px solid #963812">'—'</td>
      <td style="padding:4px; bold:true; backgroundColor:#F8F2EF; textAlign:right; border:1px solid #963812">'¥'${totalAmount}</td>
    </tr>
  </table>

  // 段落:plain
  <p style="fontSize:10.5pt; bold:true; color:#6D2203; marginBottom:3px">'金额合计'</p>
  <p style="fontSize:9pt; marginBottom:6px">${totalLine}</p>
  <p style="fontSize:10.5pt; bold:true; color:#6D2203; marginBottom:3px">'进项税额'</p>
  <p style="fontSize:9pt; marginBottom:6px">${taxLine}</p>
  <p style="fontSize:10.5pt; bold:true; color:#6D2203; marginBottom:3px">'备注'</p>
  <p style="fontSize:9pt; marginBottom:6px">${remark}</p>

  // 签章区:right
  <!-- 签章占位区:后续加盖电子公章 / 骑缝章 / 打印二维码 -->
  <table style="width:100%; fontSize:8pt; marginBottom:8px">
    <tr>
      <td style="width:32%; padding:5px; backgroundColor:#F8F2EF; fontSize:7.5pt; color:#78838C; border:1px solid #DAC4BB">'本联次加盖印章后有效,扫码可验真'</td>
      <td style="padding:3px; textAlign:center; color:#963812; border:1px dashed #963812">'(公章 / 财务专用章占位)' '(骑缝章 / 二维码占位)'</td>
    </tr>
    <tr>
      <td style="width:32%%; padding:4px">' '</td>
      <td style="padding:3px; textAlign:center; color:#78838C; border:1px dashed #963812">'单位名称:'${sellerName} '骑缝章沿右边线骑缝加盖'</td>
    </tr>
    <tr>
      <td style="width:32%%; padding:4px">' '</td>
      <td style="padding:3px; textAlign:center; color:#78838C; border:1px dashed #963812">'签章日期:'${issueDate} '二维码打印于右上角方框内'</td>
    </tr>
  </table>

  // 落款:plain
  <!-- 落款栏 -->
  <table style="width:100%; fontSize:9pt; marginTop:6px">
    <tr>
      <td style="width:33%; padding:4px; color:#6D2203; borderBottom:1px solid #DAC4BB">'收款人:'${payee}</td>
      <td style="width:33%; padding:4px; color:#6D2203; borderBottom:1px solid #DAC4BB">'复核:'${reviewer}</td>
      <td style="padding:4px; color:#6D2203; borderBottom:1px solid #DAC4BB">'开票人:'${drawer}</td>
    </tr>
  </table>
</body>
</pdf>

同分组示例

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