首页 > 解决方案 > 从 XML 页数生成 PDF 是错误的

问题描述

我正在从 XML 生成 PDF,一切都按预期进行,唯一的问题是,它额外生成了一页。

我在下面详细解释了这个问题。

页数错误。如果我在一个表中有 20 行并且每页显示 5 行(带有页眉和页脚)。因此,它应该生成 4 个页面,而不是生成 5 个 PDF 页面,最后一页只有页眉和页脚,这是错误的。

请在下面找到我的 XSL 样式表代码

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:js="urn:extra-functions">
    <xsl:output method="html" indent="yes"/>
    <xsl:template match="Data">
            <html>
                <head>
                    <title>Invoice</title>
            </head>


                <body>

                    <xsl:call-template name="Filler">
                        <xsl:with-param name="fillercount" select="1" />
                    </xsl:call-template>


                    <xsl:copy-of select="$OrderHeader"/>


                    <xsl:copy-of select="$OrderRowsHeader"/>

                    <xsl:for-each select="Order/OrderRows/OrderRow">

                        <table class="tabledetails" cellspacing="0" style="table-layout:fixed">
                            <tr>
                                <td class="tdmargin" />
                                <td style="width:70px" align="right" class="blueline">
                                    <xsl:value-of select="ProductID" />
                                    <xsl:value-of select="translate(' ', ' ', '&#160;')"/>
                                </td>

                                <td class="tdmargin" />
                            </tr>
                        </table>
                        <xsl:if test="(position() mod 40) = 0 ">
                            <!--40 rows per page-->
                            <xsl:call-template name="Filler">
                                <xsl:with-param name="fillercount" select="1" />
                            </xsl:call-template>

                            <br class="pagebreak" /> <br />

                            <xsl:copy-of select="$ReportHeader" />



                            <xsl:copy-of select="$OrderRecipient"/>

                            <xsl:call-template name="Filler">
                                <xsl:with-param name="fillercount" select="1" />
                            </xsl:call-template>

                            <xsl:copy-of select="$OrderHeader"/>



                            <xsl:copy-of select="$OrderRowsHeader"/>

                        </xsl:if>
                    </xsl:for-each>
                  <!--Filler -->
                <xsl:choose>
                    <!-- case of only one page-->
                    <xsl:when test="count(delivery_receipt/order_items) &lt;= 5">
                        <xsl:call-template name="Filler">
                            <xsl:with-param name="fillercount" select="5 - (count(delivery_receipt/order_items))"/>
                        </xsl:call-template>
                    </xsl:when>
                    <!-- case of more than one page-->
                    <xsl:otherwise>
                        <xsl:call-template name="Filler">
                            <!--(Rows per page = 5) -  (Rows in current page) - (Total section rows = 1 ) + (Filler Row = 1)-->
                            <xsl:with-param name="fillercount" select="5 - ( ( count(delivery_receipt/order_items)-5 ) mod 5 ) - 3 + 1"/>
                        </xsl:call-template>
                    </xsl:otherwise>
                </xsl:choose>
                <!--End Filler -->

                </body>
            </html>


        </xsl:template>
    <!-- variable OrderHeader-->
    <xsl:variable name="OrderHeader">
        <table class="tabledetails" cellspacing="0" >
            <tr>
                <td class="tdmargin" />
                <th>
                    Order ID:
                </th>

                <td class="tdmargin" />
            </tr>
            <tr>
                <td class="tdmargin" />
                <td class="tdorderHeader">
                    <xsl:value-of select="/Data/Order/OrderID" />
                    <xsl:value-of select="translate(' ', ' ', '&#160;')"/>
                </td>

                <td class="tdmargin" />
            </tr>
        </table>
    </xsl:variable>

    <!--variable OrderRowsHeader-->
    <xsl:variable name="OrderRowsHeader">
        <table class="tabledetails" cellspacing="0" style="table-layout:fixed">
            <tr>
                <td class="tdmargin" />
                <th style="width:70px">
                    Product ID:
                </th>

            </tr>
        </table>
    </xsl:variable>

   <!-- Template Filler-->
<xsl:template name="Filler">
    <xsl:param name="fillercount" select="1"/>
    <xsl:if test="$fillercount > 0">
        <table class="tabledetails">
            <tr>
                <td>
                    <xsl:value-of select="translate(' ', ' ', '&#160;')"/>
                </td>
            </tr>
        </table>
        <xsl:call-template name="Filler">
            <xsl:with-param name="fillercount" select="$fillercount - 1"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>
<!-- Template Filler End-->

</xsl:stylesheet>

标签: htmlxmlxsltxslt-1.0

解决方案


修改了下面的行

<xsl:if test="(position() mod 40) = 0 ">

<xsl:if test="(position() mod 40) = 0 and ( position() !=  last() )">

因此,此问题已得到修复。


推荐阅读