如何计算 <fo:region-before /> 高度?

Posted

技术标签:

【中文标题】如何计算 <fo:region-before /> 高度?【英文标题】:How to calculate the <fo:region-before /> height? 【发布时间】:2021-12-18 20:03:12 【问题描述】:

在我的 pdf 文档中,我希望在所有页面上重复一些数据,所以我将它们放在 &lt;fo:region-before/&gt; 中。它导致区域的高度不固定。我想计算这个区域的高度以将&lt;fo:region-body/&gt;的数据放在适当的位置,但我不知道该怎么做?这是我的代码:

<#assign imageBorderWidth=0.4/>
<#assign logoHeight=0.5/>
<#assign regionAfterBlockHeight=0.22/>
<#assign regionAfterHeight=regionAfterBlockHeight * 4/>

<#setting locale="en_US">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" font-family="ArialUni, ArialUniBold, ArialUniItalic" font-size="10pt">
    <fo:layout-master-set>
        <fo:simple-page-master master-name="letter-portrait" page- page-
                               margin-top="0" margin-bottom="0" margin-left="0in" margin-right="0in">
            <fo:region-body margin-top="5in" margin-bottom="$regionAfterHeightin"
                            margin-left="$imageBorderWidthin" margin-right="$imageBorderWidthin" background-color="yellow"/>
            <fo:region-before extent="2in" />
            <fo:region-after extent="$regionAfterHeightin"/>
        </fo:simple-page-master>
    </fo:layout-master-set>

    <fo:page-sequence master-reference="letter-portrait" id="mainSequence">
        <fo:static-content flow-name="xsl-region-before">
            <fo:block-container absolute-position="absolute" top="0" left="0">
                <fo:block>
                    <fo:external-graphic src="$backgroundLocation.getLocation()" content- content- scaling="non-uniform"/>
                </fo:block>
            </fo:block-container>
            <#--Header Hóa đơn GTGT, logo, mẫu số-->
            <fo:table table-layout="fixed" >
                <fo:table-body>
                    <fo:table-row>
                        <fo:table-cell padding="0.5in 0.5in 0in 0.5in" text-align="center" >
                            <fo:block>
                                <#if logoLocation?has_content>
                                    <fo:external-graphic src="$logoLocation.getLocation()" content- content- scaling="non-uniform"/>
                                <#else >
                                    <fo:leader />
                                </#if>
                            </fo:block>
                        </fo:table-cell>
                        <fo:table-cell text-align="center"  padding-top="$imageBorderWidthin" display-align="center ">
                            <fo:block-container >
                                <fo:block font-size="14pt" font-family="ArialUniBold" text-align="center">
                                    HÓA ĐƠN GIÁ TRỊ GIA TĂNG
                                </fo:block>
                            </fo:block-container>
                            <fo:block-container >
                                <fo:block>
                                    <fo:inline font-style="italic">(Bản thể hiện của hóa đơn điện tử)</fo:inline>
                                </fo:block>
                            </fo:block-container>
                            <fo:block-container >
                                <fo:block>
                                    <fo:inline font-style="italic">Ngày tháng năm</fo:inline>
                                </fo:block>
                            </fo:block-container>
                        </fo:table-cell>
                        <fo:table-cell text-align="center"  padding-top="$imageBorderWidthin" display-align="center">
                            <fo:table table-layout="fixed"  >
                                <fo:table-column column-/>
                                <fo:table-column column-/>
                                <fo:table-body>
                                    <fo:table-row>
                                        <fo:table-cell text-align="left">
                                            <fo:block-container >
                                                <fo:block><fo:inline>Form no:</fo:inline></fo:block>
                                            </fo:block-container>
                                        </fo:table-cell>
                                        <fo:table-cell text-align="left">
                                            <fo:block-container >
                                                <fo:block><fo:inline>01GTKT0/001</fo:inline></fo:block>
                                            </fo:block-container>
                                        </fo:table-cell>
                                    </fo:table-row>
                                    <fo:table-row>
                                        <fo:table-cell text-align="left">
                                            <fo:block-container >
                                                <fo:block><fo:inline>Sign:</fo:inline></fo:block>
                                            </fo:block-container>
                                        </fo:table-cell>
                                        <fo:table-cell text-align="left">
                                            <fo:block-container >
                                                <fo:block><fo:inline>AB/21E</fo:inline></fo:block>
                                            </fo:block-container>
                                        </fo:table-cell>
                                    </fo:table-row>
                                    <fo:table-row>
                                        <fo:table-cell text-align="left">
                                            <fo:block-container >
                                                <fo:block><fo:inline>No:</fo:inline></fo:block>
                                            </fo:block-container>
                                        </fo:table-cell>
                                        <fo:table-cell text-align="left">
                                            <fo:block-container >
                                                <fo:block><fo:inline>000001</fo:inline></fo:block>
                                            </fo:block-container>
                                        </fo:table-cell>
                                    </fo:table-row>
                                </fo:table-body>
                            </fo:table>
                        </fo:table-cell>
                    </fo:table-row>
                </fo:table-body>
            </fo:table>
            <fo:block border-bottom- margin="0 $imageBorderWidthin 0 $imageBorderWidthin " border-bottom-style="solid" margin-top="3mm" border-bottom-color="#b7b7b7" />
            <fo:table table-layout="fixed"  margin-top="0.1in" font-size="10pt">
                <fo:table-column column-/>
                <fo:table-column column-/>
                <fo:table-body>
                    <fo:table-row>
                        <fo:table-cell text-align="left" padding-left="$imageBorderWidthin">
                            <fo:block font-size="15pt" font-family="ArialUniBold" line-><fo:inline>CÔNG TY TNHH QUẢN LÝ NỢ VÀ KHAI THÁC TÀI SẢN NGÂN HÀNG TMCP BẮC Á - CHI NHÁNH THÀNH PHỐ HỒ CHÍ MINH</fo:inline></fo:block>
                        </fo:table-cell>
                        <fo:table-cell text-align="center" number-rows-spanned="5" display-align="before" padding-right="$imageBorderWidthin">
                            <fo:block>
                                <#assign qrContentWidth=(8.5*0.2) - imageBorderWidth/>
                                <fo:external-graphic src="$qrCodeImgLocation.getLocation()" content- content- scaling="uniform"/>
                            </fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                    <fo:table-row>
                        <fo:table-cell text-align="left" padding-left="$imageBorderWidthin">
                            <fo:block line-><fo:inline>Taxcode: 0101243150-999</fo:inline></fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                    <fo:table-row>
                        <fo:table-cell text-align="left" padding-left="$imageBorderWidthin">
                            <fo:block line->
                                <fo:table inline-progression-dimension="auto" table-layout="auto">
                                    <fo:table-column column-number="1" column- />
                                    <fo:table-column column-number="2"/>
                                    <fo:table-body>
                                        <fo:table-row>
                                            <fo:table-cell text-align="left" padding="0">
                                                <fo:block>Address:</fo:block>
                                            </fo:table-cell>
                                            <fo:table-cell text-align="left" padding="0">
                                                <fo:block>Số 36A Phan Đăng Lưu, Phường 05, Quận Phú Nhuận, Thành phố Hồ Chí Minh, Việt Nam</fo:block>
                                            </fo:table-cell>
                                        </fo:table-row>
                                    </fo:table-body>
                                </fo:table>
                            </fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                    <fo:table-row>
                        <fo:table-cell text-align="left" padding-left="$imageBorderWidthin">
                            <fo:block line-><fo:inline>Phone: 028.38229812</fo:inline></fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                    <fo:table-row>
                        <fo:table-cell text-align="left" padding-left="$imageBorderWidthin">
                            <fo:block line-><fo:inline>Bank Account:</fo:inline></fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                </fo:table-body>
            </fo:table>
            <fo:block border-bottom- margin="0 $imageBorderWidthin 0 $imageBorderWidthin " border-bottom-style="solid" margin-top="1mm" border-bottom-color="#b7b7b7" />
        </fo:static-content>

        <fo:static-content flow-name="xsl-region-after" font-size="9pt" >
            <fo:table table-layout="fixed"  >
                <fo:table-column column-/>
                <fo:table-column column-/>
                <fo:table-body>
                    <fo:table-row>
                        <fo:table-cell text-align="left" padding-left="$imageBorderWidthin">
                            <fo:block-container ><fo:block text-align="left">Tra cứu tại Website: https://www.meinvoice.vn/tra-cuu - Mã tra cứu hóa đơn:</fo:block></fo:block-container>
                        </fo:table-cell>
                        <fo:table-cell/>
                    </fo:table-row>
                    <fo:table-row>
                        <fo:table-cell >
                            <fo:block-container ><fo:block text-align="center">(Cần kiểm tra, đối chiếu khi lập, giao, nhận hóa đơn)</fo:block></fo:block-container>
                        </fo:table-cell>
                        <fo:table-cell/>
                    </fo:table-row>
                    <fo:table-row>
                        <fo:table-cell>
                            <fo:block-container ><fo:block text-align="center">Phát hành bởi phần mềm MISA meInvoice - Công ty Cổ phần MISA (www.misa.com.vn) - MST: 0101243150</fo:block></fo:block-container>
                        </fo:table-cell>
                        <fo:table-cell padding-right="$imageBorderWidthin">
                            <fo:block-container >
                                <fo:block>
                                    <fo:block text-align="right"><fo:page-number/>/<fo:page-number-citation-last ref-id="endContent"/></fo:block>
                                </fo:block>
                            </fo:block-container>
                        </fo:table-cell>
                    </fo:table-row>
                </fo:table-body>
            </fo:table>
        </fo:static-content>

        <fo:flow flow-name="xsl-region-body" >
            <fo:block>aaaaa</fo:block>
            <fo:block>aaaaa</fo:block>
            <fo:block>aaaaa</fo:block>
            <fo:block>aaaaa</fo:block>
            <fo:block>aaaaa</fo:block>
            <fo:block>aaaaa</fo:block>
            <fo:block>aaaaa</fo:block>
            <fo:block>aaaaa</fo:block>
            <fo:block>aaaaa</fo:block>
            <fo:block>aaaaa</fo:block>
            <fo:block>aaaaa</fo:block>
            <fo:block>aaaaa</fo:block>
            .......  
            <fo:block id="endContent">aaaaa</fo:block>
        </fo:flow>
    </fo:page-sequence>
</fo:root>

我的结果,黄色区域&lt;fo:region-body/&gt;红色矩形&lt;fo:region-before/&gt;,我想要黄色区域是在 红色矩形红色矩形 在所有页面上重复之后立即继续

【问题讨论】:

【参考方案1】:

最简单的答案是将所有内容放在fo:table 中,将当前页眉放在fo:table-header 中,并在每一页上重复。

一个更复杂的解决方案是使用来自打印和页面布局社区组https://github.com/pplcg/XSLTExtensions 的 XSLT 扩展在 XSLT 转换期间运行格式化程序以获得标题内容的区域树,以便您可以准确地计算它需要多少高度。但是,&lt;#assign 和其他标记让我认为您可能没有太多访问 XSLT 处理器的权限,并且可能无法将其配置为使用扩展。

【讨论】:

谢谢托尼,我已经把它放在fo:table-header 并且问题解决了

以上是关于如何计算 <fo:region-before /> 高度?的主要内容,如果未能解决你的问题,请参考以下文章

如何计算 Map<> 中每个列表的总和?

计算机如何进行浮点运算?

JavaScript 如何编写计算器

如何计算具有特定类的 DIV

如何在php中计算上传文件的数量

如何计算节点中的不同值?