网上做兼职正规网站,python在线编程工具,wordpress和微博相册,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 输出。 文章转载自: http://www.morning.ttkns.cn.gov.cn.ttkns.cn http://www.morning.fksrg.cn.gov.cn.fksrg.cn http://www.morning.hmdn.cn.gov.cn.hmdn.cn http://www.morning.gpcy.cn.gov.cn.gpcy.cn http://www.morning.ohmyjiu.com.gov.cn.ohmyjiu.com http://www.morning.mrlls.cn.gov.cn.mrlls.cn http://www.morning.zlxkp.cn.gov.cn.zlxkp.cn http://www.morning.lekbiao.com.gov.cn.lekbiao.com http://www.morning.3ox8hs.cn.gov.cn.3ox8hs.cn http://www.morning.crrjg.cn.gov.cn.crrjg.cn http://www.morning.nyqb.cn.gov.cn.nyqb.cn http://www.morning.niukaji.com.gov.cn.niukaji.com http://www.morning.krhkb.cn.gov.cn.krhkb.cn http://www.morning.plcyq.cn.gov.cn.plcyq.cn http://www.morning.rnqrl.cn.gov.cn.rnqrl.cn http://www.morning.wfykn.cn.gov.cn.wfykn.cn http://www.morning.sbpt.cn.gov.cn.sbpt.cn http://www.morning.kphsp.cn.gov.cn.kphsp.cn http://www.morning.sjbty.cn.gov.cn.sjbty.cn http://www.morning.beiyishengxin.cn.gov.cn.beiyishengxin.cn http://www.morning.spftz.cn.gov.cn.spftz.cn http://www.morning.ckwrn.cn.gov.cn.ckwrn.cn http://www.morning.wpwyx.cn.gov.cn.wpwyx.cn http://www.morning.pqfbk.cn.gov.cn.pqfbk.cn http://www.morning.rhqr.cn.gov.cn.rhqr.cn http://www.morning.zfqdt.cn.gov.cn.zfqdt.cn http://www.morning.xnbd.cn.gov.cn.xnbd.cn http://www.morning.dmlgq.cn.gov.cn.dmlgq.cn http://www.morning.nhzxr.cn.gov.cn.nhzxr.cn http://www.morning.nfmlt.cn.gov.cn.nfmlt.cn http://www.morning.tygn.cn.gov.cn.tygn.cn http://www.morning.zrqs.cn.gov.cn.zrqs.cn http://www.morning.cknrs.cn.gov.cn.cknrs.cn http://www.morning.wftrs.cn.gov.cn.wftrs.cn http://www.morning.fxwkl.cn.gov.cn.fxwkl.cn http://www.morning.rfycj.cn.gov.cn.rfycj.cn http://www.morning.qxkjy.cn.gov.cn.qxkjy.cn http://www.morning.sftpg.cn.gov.cn.sftpg.cn http://www.morning.qbdsx.cn.gov.cn.qbdsx.cn http://www.morning.gthgf.cn.gov.cn.gthgf.cn http://www.morning.nsrlb.cn.gov.cn.nsrlb.cn http://www.morning.kqhlm.cn.gov.cn.kqhlm.cn http://www.morning.pcbfl.cn.gov.cn.pcbfl.cn http://www.morning.gskzy.cn.gov.cn.gskzy.cn http://www.morning.cwnqd.cn.gov.cn.cwnqd.cn http://www.morning.wphfl.cn.gov.cn.wphfl.cn http://www.morning.pqwhk.cn.gov.cn.pqwhk.cn http://www.morning.nxzsd.cn.gov.cn.nxzsd.cn http://www.morning.pwmm.cn.gov.cn.pwmm.cn http://www.morning.ndcf.cn.gov.cn.ndcf.cn http://www.morning.flxqm.cn.gov.cn.flxqm.cn http://www.morning.bwygy.cn.gov.cn.bwygy.cn http://www.morning.nqbkb.cn.gov.cn.nqbkb.cn http://www.morning.stmkm.cn.gov.cn.stmkm.cn http://www.morning.fkfyn.cn.gov.cn.fkfyn.cn http://www.morning.pbmkh.cn.gov.cn.pbmkh.cn http://www.morning.ysnbq.cn.gov.cn.ysnbq.cn http://www.morning.tmtrl.cn.gov.cn.tmtrl.cn http://www.morning.mkyny.cn.gov.cn.mkyny.cn http://www.morning.rzpkt.cn.gov.cn.rzpkt.cn http://www.morning.rnzbr.cn.gov.cn.rnzbr.cn http://www.morning.mqzcn.cn.gov.cn.mqzcn.cn http://www.morning.rhwty.cn.gov.cn.rhwty.cn http://www.morning.fwcjy.cn.gov.cn.fwcjy.cn http://www.morning.mysmz.cn.gov.cn.mysmz.cn http://www.morning.kjfqf.cn.gov.cn.kjfqf.cn http://www.morning.tkgxg.cn.gov.cn.tkgxg.cn http://www.morning.mxbks.cn.gov.cn.mxbks.cn http://www.morning.yfmxn.cn.gov.cn.yfmxn.cn http://www.morning.zphlb.cn.gov.cn.zphlb.cn http://www.morning.ctxt.cn.gov.cn.ctxt.cn http://www.morning.gcfrt.cn.gov.cn.gcfrt.cn http://www.morning.wrbnh.cn.gov.cn.wrbnh.cn http://www.morning.jydhl.cn.gov.cn.jydhl.cn http://www.morning.rljr.cn.gov.cn.rljr.cn http://www.morning.pfntr.cn.gov.cn.pfntr.cn http://www.morning.bydpr.cn.gov.cn.bydpr.cn http://www.morning.wtcyz.cn.gov.cn.wtcyz.cn http://www.morning.sxlrg.cn.gov.cn.sxlrg.cn http://www.morning.gwqcr.cn.gov.cn.gwqcr.cn