How can I modify my XSL-FO stylesheet to efficiently place multiple invoices in the same PDF page using Apache FOP in A4 size page

huangapple go评论62阅读模式
英文:

How can I modify my XSL-FO stylesheet to efficiently place multiple invoices in the same PDF page using Apache FOP in A4 size page

问题

以下是已翻译的内容:

我有一个XML文档的格式:
XSl:

我构建了一个XSL-FO样式表用于XML文档,并使用Apache FOP将其转换为PDF。问题是它将每个发票打印在单独的页面上,而不是在一页上放置尽可能多的发票,只要有空间(使用A4大小的页面)。 每张发票只占大约1/3的页面,其余的2/3页面是空白的。 我如何修改我的XSL-FO样式表,以便有效地利用页面上的空间,添加尽可能多的发票,而不会打破发票?

我尝试将这个放置:

但输出仍然相同。 这是我第一次使用XSL-FO和Apache FOP工作,无法在任何地方找到更多信息。 任何帮助都将不胜感激。
英文:

I have an XML document in the format:

 <root> 
   <Invoice>..</Invoice>
   <Invoice>..</Invoice>
   <Invoice>..</Invoice>
   <Invoice>..</Invoice>
 </root>
XSl :

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
      <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    
      <xsl:template match="/">
      
        <fo:root>
          <fo:layout-master-set>
            <fo:simple-page-master master-name="invoice-page" page-width="21cm" margin="0.5cm">
              <fo:region-body margin="1cm" />
              <fo:region-before extent="3cm" />
              <fo:region-after extent="0.5cm" />
            </fo:simple-page-master>
          </fo:layout-master-set>
    
          <xsl:apply-templates select="Download/Invoice_Download"/>
        </fo:root>
      </xsl:template>
    
      <xsl:template match="Invoice_Download" keep-together.within-page="always">
        <fo:page-sequence master-reference="invoice-page" >
          <fo:static-content flow-name="xsl-region-before">
            <fo:block text-align="center" font-size="18pt">
              <xsl:if test="position() = 1">INVOICES</xsl:if>
            </fo:block>
          </fo:static-content>
          <fo:flow flow-name="xsl-region-body">
            ...........
          </fo:flow>
        </fo:page-sequence>
      </xsl:template>
    </xsl:stylesheet>

I constructed and XSL-FO stylesheet for the xml document and it converts to PDF using Apache FOP.The problem is it prints each invoice in a single page rather than placing multiple invoices in the same page as long as the space is available(using A4 size page). Each invoice takes only about 1/3rd page and remaining 2/3rds page is blank. How can I modify my XSL-FO stylesheet such that it uses the space in the page efficiently by adding as many invoices it can fit in without breaking the invoice?

I tried placing this

<fo:page-sequence master-reference="invoice-page" keep-together="always"force-page-count="no-force"> 

but still the output is the same. This is my first time working with XSL-FO and Apache FOP and can't find much about this anywhere.
Any help is appreciated.

答案1

得分: 1

没有看到您完整的XSL或至少相关部分的情况下,通常有人会这样做:

<xsl:template match="invoice">
    <fo:block-container keep-together.within-page="always">
       <fo:block>
        <xsl:apply-templates/>
       </fo:block>
    </fo:block-container>
</xsl:template>

这将使用一个总是保持在单页内的块容器包围每个发票。如果两个适合,那么就会有两个,如果适合三个,就会有三个。无法完全适应一页的一个将被移动到下一页,因为内部内容总是保持在一起。唯一的注意事项是,您应该确保没有一个发票的长度超过一页。如果超过一页,它可能会导致一个空白页,然后保持条件就会被打破。

注意:我在fo:block-container中添加了一个fo:block,以防您在发票内的匹配中有非块级内容。

英文:

Without seeing your whole XSL or at least the relevant parts, normally someone would do something like this:

&lt;xsl:template match=&quot;invoice&quot;&gt;
    &lt;fo:block-container keep-together.within-page=&quot;always&quot;&gt;
       &lt;fo:block&gt;
        &lt;xsl:apply-templates/&gt;
       &lt;/fo:block&gt;
    &lt;/fo:block-container&gt;
&lt;/xsl:template&gt;

This would surround each invoice with a block-container that is always kept together within a single page. If two fit, then you would have two, if three fit then three. One that cannot fit in total is moved to the next page because the inside content is always kept together. The only caveat here is that you should be sure that no one invoice is longer than one page. If it is, it will likely cause an empty page and then the keep condition would be broken.

Note: I added an fo:block inside the fo:block-container in case you have non block-level content in the matches inside of invoice.

huangapple
  • 本文由 发表于 2023年6月5日 13:19:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76403644.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定