阜南县建设局网站,wordpress防伪码,金华住房与城乡建设部网站,网站一直百度上搜不到是怎么回事目录 使用 XSL-FO 生成电子发票 PDF#xff1a;从布局设计到优化为什么选择 XSL-FO#xff1f;1. 初始设置2. 标题区块3. 买卖方信息4. 商品明细表格5. 合计信息6. 优化代码结构与布局7. 生成 PDF 文件8. 示例总结 使用 XSL-FO 生成电子发票 PDF#xff1a;从布局设计到优化… 目录 使用 XSL-FO 生成电子发票 PDF从布局设计到优化为什么选择 XSL-FO1. 初始设置2. 标题区块3. 买卖方信息4. 商品明细表格5. 合计信息6. 优化代码结构与布局7. 生成 PDF 文件8. 示例总结 使用 XSL-FO 生成电子发票 PDF从布局设计到优化
在电子商务和财务系统中生成规范化的发票 PDF 文件是一个常见需求。利用 XSL-FOExtensible Stylesheet Language Formatting Objects我们可以设计和生成格式规范的发票 PDF。本文将介绍如何使用 XSL-FO 构建电子发票的 PDF 文件并提供一些优化布局的建议以实现更清晰美观的发票。
为什么选择 XSL-FO
XSL-FO 是 W3C 的标准用于将 XML 文档格式化为 PDF、PostScript 等格式的高质量输出文档。它尤其适合结构化数据的排版需求。结合 Apache FOPFormatting Objects Processor等工具我们可以从 XML 模板生成发票 PDF。
1. 初始设置
首先在根节点 fo:root 中设置页面布局
fo:layout-master-setfo:simple-page-master master-nameinvoicefo:region-body margin5mm//fo:simple-page-master
/fo:layout-master-set这里指定了页面的边距和布局便于后续的内容排版。
2. 标题区块
发票的顶部通常包含发票标题、二维码、公司信息和发票号。可以通过 fo:table 元素来布局这些信息利用列宽度来控制内容的对齐和分布
fo:table table-layoutfixed width100%fo:table-column column-width22%/fo:table-column column-width56%/fo:table-column column-width22%/...
/fo:table在这个表格中我们在左右两侧显示二维码和发票信息中间展示发票标题。通过设置 column-width可以确保不同元素之间的布局合理。
3. 买卖方信息
发票中买方和卖方的信息是关键内容。我们使用 fo:table 元素创建一个两列的表格来放置这些信息
fo:table-column column-width5%/
fo:table-column column-width45%/每个列块中包括公司名称、税号等信息并使用 writing-modetb-rl 为标签列设置垂直方向书写从而增强布局的清晰度。
4. 商品明细表格
商品明细表格包含了商品的名称、规格、单位、数量、单价、金额、税率、税额等。可以通过固定列宽设置确保不同商品数据的显示一致
fo:table-column column-width20%/
...对于每个商品的明细行我们使用了 fo:table-body 标签包含具体的内容。可以进一步通过 min-height50pt 设置行高度确保无数据时也有足够的空间。
5. 合计信息
在表格的下方使用一个新的表格展示“价税合计”信息包括大小写金额
fo:table-bodyfo:table-rowfo:table-cell text-aligncenterfo:block价税合计大写/fo:block/fo:table-cell.../fo:table-row
/fo:table-body通过将列宽和内容对齐调整为居中可以确保整体布局对齐美观。
6. 优化代码结构与布局
在发票生成过程中统一样式设置和简化代码结构可以提高代码的可读性和可维护性以下是几点优化建议
统一表格样式将表格边框样式、颜色等属性集中设置避免重复代码。简化内容布局对于标签文本等内容使用 writing-modetb-rl 实现垂直对齐避免复杂的布局调整。表格布局的合理分配使用 table-layoutfixed 和精确的 column-width 设置确保表格布局稳定。
7. 生成 PDF 文件
在完成 XSL-FO 模板后可以使用 Apache FOP 工具将其转换为 PDF 文件
fop -xml invoice.xml -xsl invoice.xsl -pdf invoice.pdf这样即可获得一份规范、清晰的电子发票 PDF 文件。
8. 示例
?xml version1.0 encodingutf-8?
fo:root xmlns:fohttp://www.w3.org/1999/XSL/Formatxmlns:foxhttp://xmlgraphics.apache.org/fop/extensionsxmlns:xehttp://www.x-easypdf.cn/nsxmlns:svghttp://www.w3.org/2000/svgfo:layout-master-setfo:simple-page-master master-nameinvoicefo:region-body margin5mm//fo:simple-page-master/fo:layout-master-setfo:page-sequence master-referenceinvoicefo:flow flow-namexsl-region-body colorrgb(188, 119, 76)fo:block font-familySimSun space-before10ptfo:table table-layoutfixed width100%fo:table-column column-width22%/fo:table-column column-width56%/fo:table-column column-width22%/fo:table-bodyfo:table-rowfo:table-cellfo:block text-alignleft!-- 二维码图片所在位置 --fo:external-graphic srcimage/qrcode.png content-width60pt content-height60pt//fo:block/fo:table-cellfo:table-cell display-aligncenterfo:block text-aligncenter font-familySimSun font-size20pt电子发票增值税专用发票/fo:blockfo:block text-aligncenter font-familySimSun font-size14pt/fo:blockfo:block text-aligncenter space-before-50pt !-- 使用负值移动位置 --!-- 章图片所在位置 --fo:external-graphic srcimage/seal.png content-width70pt content-height70pt//fo:block/fo:table-cellfo:table-cell font-size10pt display-aligncenter text-alignleftfo:block发票号码123456789/fo:blockfo:block开票日期2024年01月01日/fo:block/fo:table-cell/fo:table-row/fo:table-body/fo:table/fo:block!-- Buyer and Seller Information --fo:block font-familySimSun font-size10ptfo:table table-layoutfixed width100% border-stylesolid border-width0.5ptfo:table-column column-width5% border-stylesolid border-colorrgb(188, 119, 76) /fo:table-column column-width45% border-stylesolid border-colorrgb(188, 119, 76) /fo:table-column column-width5% border-stylesolid border-colorrgb(188, 119, 76) /fo:table-column column-width45% border-stylesolid border-colorrgb(188, 119, 76) /fo:table-bodyfo:table-rowfo:table-cell padding5pt writing-modetb-rlfo:block购买方信息/fo:block/fo:table-cellfo:table-cell display-aligncenterfo:block名称/fo:blockfo:block统一社会信用代码/纳税人识别号/fo:block/fo:table-cellfo:table-cell padding5pt writing-modetb-rlfo:block销售方信息/fo:block/fo:table-cellfo:table-cell display-aligncenterfo:block名称/fo:blockfo:block统一社会信用代码/纳税人识别号/fo:block/fo:table-cell/fo:table-row/fo:table-body/fo:table/fo:block!-- Item Details Table --fo:block font-familySimSun font-size10ptfo:table table-layoutfixed width100% border-stylesolid border-colorrgb(188, 119, 76) border-width0.5ptfo:table-column column-width20% border-left-stylesolid border-colorrgb(188, 119, 76)/fo:table-column column-width10% border-colorrgb(188, 119, 76)/fo:table-column column-width10% border-colorrgb(188, 119, 76)/fo:table-column column-width10% border-colorrgb(188, 119, 76)/fo:table-column column-width15% border-colorrgb(188, 119, 76)/fo:table-column column-width10% border-colorrgb(188, 119, 76)/fo:table-column column-width15% border-colorrgb(188, 119, 76)/fo:table-column column-width10% border-right-stylesolid border-colorrgb(188, 119, 76)/fo:table-headerfo:table-rowfo:table-cell text-aligncenterfo:block项目名称/fo:block/fo:table-cellfo:table-cell text-aligncenterfo:block规格型号/fo:block/fo:table-cellfo:table-cell text-aligncenterfo:block单位/fo:block/fo:table-cellfo:table-cell text-aligncenterfo:block数量/fo:block/fo:table-cellfo:table-cell text-aligncenterfo:block单价/fo:block/fo:table-cellfo:table-cell text-aligncenterfo:block金额/fo:block/fo:table-cellfo:table-cell text-aligncenterfo:block税率/征收率/fo:block/fo:table-cellfo:table-cell text-aligncenterfo:block税额/fo:block/fo:table-cell/fo:table-row/fo:table-headerfo:table-body min-height50ptfo:table-rowfo:table-cell text-aligncenter padding-top5ptfo:block商品A/fo:block/fo:table-cellfo:table-cell text-aligncenter padding-top5ptfo:block型号A/fo:block/fo:table-cellfo:table-cell text-aligncenter padding-top5ptfo:block件/fo:block/fo:table-cellfo:table-cell text-aligncenter padding-top5ptfo:block1/fo:block/fo:table-cellfo:table-cell text-aligncenter padding-top5ptfo:block100.00/fo:block/fo:table-cellfo:table-cell text-aligncenter padding-top5ptfo:block100.00/fo:block/fo:table-cellfo:table-cell text-aligncenter padding-top5ptfo:block10%/fo:block/fo:table-cellfo:table-cell text-aligncenter padding-top5ptfo:block10.00/fo:block/fo:table-cell/fo:table-rowfo:table-rowfo:table-cell text-aligncenter padding-top5ptfo:block合 计/fo:block/fo:table-cellfo:table-cell text-aligncenter padding-top5ptfo:block/fo:block/fo:table-cellfo:table-cell text-aligncenter padding-top5ptfo:block/fo:block/fo:table-cellfo:table-cell text-aligncenter padding-top5ptfo:block/fo:block/fo:table-cellfo:table-cell text-aligncenter padding-top5ptfo:block/fo:block/fo:table-cellfo:table-cell text-aligncenter padding-top5ptfo:block¥100.00/fo:block/fo:table-cellfo:table-cell text-aligncenter padding-top5ptfo:block/fo:block/fo:table-cellfo:table-cell text-aligncenter padding-top5ptfo:block¥10.00/fo:block/fo:table-cell/fo:table-row/fo:table-body/fo:table/fo:block!-- Summary Section --fo:block font-familySimSun font-size10ptfo:table table-layoutfixed width100% border-stylesolid border-width0.5pt border-colorrgb(188, 119, 76)fo:table-column column-width25% border-stylesolid border-colorrgb(188, 119, 76)/fo:table-column column-width40% border-top-stylesolid border-colorrgb(188, 119, 76)/fo:table-column column-width10% border-top-stylesolid border-colorrgb(188, 119, 76)/fo:table-column column-width25% border-top-stylesolid border-right-stylesolid border-colorrgb(188, 119, 76)/fo:table-bodyfo:table-rowfo:table-cell text-aligncenter padding-top5pt padding-bottom5ptfo:block价税合计大写/fo:block/fo:table-cellfo:table-cell text-alignleft padding-top5pt padding-bottom5ptfo:block壹佰壹拾元整/fo:block/fo:table-cellfo:table-cell text-aligncenter padding-top5pt padding-bottom5ptfo:block小写/fo:block/fo:table-cellfo:table-cell text-alignleft padding-top5pt padding-bottom5ptfo:block¥110.00/fo:block/fo:table-cell/fo:table-row/fo:table-body/fo:table/fo:block!-- Footer Section --fo:block font-familySimSun font-size10pt space-after10pt border-colorrgb(188, 119, 76)fo:table table-layoutfixed width100% border-stylesolid border-width0.5ptfo:table-column column-width5% border-stylesolid border-colorrgb(188, 119, 76)/fo:table-column column-width95% border-stylesolid border-colorrgb(188, 119, 76)/fo:table-bodyfo:table-rowfo:table-cell padding5pt writing-modetb-rlfo:block备注/fo:block/fo:table-cell/fo:table-row/fo:table-body/fo:tablefo:block开票人/fo:block/fo:block/fo:flow/fo:page-sequence
/fo:root总结
XSL-FO 提供了一个强大的格式化模型适合用来生成电子发票等结构化文档。通过灵活的表格布局和样式控制可以实现专业的 PDF 输出。