【问题标题】:Display page number while generating PDF from XML using XSL stylesheet ( Its HTML output )使用 XSL 样式表从 XML 生成 PDF 时显示页码(其 HTML 输出)
【发布时间】:2018-05-08 09:26:35
【问题描述】:

我正在尝试从 XML 文件生成 PDF,并且我有一个包含近 100 行的数据表。每个 PDF 页面仅显示 40 行。 因此,PDF 正在为一个表中的 100 行生成 3 页。此外,在每一页上还显示页眉和页脚。

现在,我尝试在每一页的顶部显示页码。不知道如何显示页码

我有同样的占位符

 <!-- variable ReportHeader-->
<xsl:variable name="ReportHeader">
    <table class="tableReportHeader" cellspacing="0">
        <tr>
            <td>
                <img class="imglogo" src="image_header.png" />
            </td>
            <td>
                <h3 style="color:darkblue; font-family: Arial;">INVOICE</h3>
                <h3 style="color:darkblue; font-family: Arial;">Page : </h3>
            </td>
        </tr>
    </table>
</xsl:variable>

例如:

 Page No : 1/3
 Page No : 2/3
 Page No : 3/3

请找到我的完整 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:copy-of select="$ReportHeader"/>

                    <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>

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

                            <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>

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

                </body>
            </html>


        </xsl:template>


    <!-- variable ReportHeader-->
    <xsl:variable name="ReportHeader">
        <table class="tableReportHeader" cellspacing="0">
            <tr>
                <td>
                    <img class="imglogo" src="image_header.png" />
                </td>
                <td>
                    <h3 style="color:darkblue; font-family: Arial;">INVOICE</h3>
                    <h3 style="color:darkblue; font-family: Arial;">Page : </h3>
                </td>
            </tr>
        </table>
    </xsl:variable>
    <!-- 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 ReportFooter-->
    <xsl:variable name="ReportFooter">
        <table class="tableReportFooter">
            <tr>
                <td style="width:20px;"></td>
                <td>
                    <table>
                        <tr>
                            <td style="font-size: 5pt; text-align: justify;border-top: solid DarkBlue 1px;">
                                One Portals Way, Twin Points WA  98156 Phone: 1-206-555-1417   Fax: 1-206-555-5938
                            </td>
                        </tr>
                    </table>
                </td>
                <td style="width:20px;"></td>
            </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>
</xsl:stylesheet>

【问题讨论】:

  • 您是否使用某种包将 HTML 转换为 PDF?您可能需要查看相关文档,看看其中是否包含有关页码的任何内容。
  • 不,我没有使用任何包。我有用于解析的 XML 和 XSL 样式表。使用 Java 代码并生成 PDF

标签: html xml xslt xslt-1.0


【解决方案1】:

在您的for-each 中,您可以计算页码,这与分页符非常相似。

 <xsl:value-of select="ceiling(position() div  40)"/>

这将当前循环位置除以每页的项目数。结果四舍五入到下一个整数值。这样你就可以从迭代 1 到 40 得到 1,从迭代 41 到…得到2......你明白了。

但是,要在 ReportHeader 中使用此值,您必须将 ReportHeader 设为命名模板并将值作为参数传递给它。基本上与您对Filler 所做的相同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-20
    • 2015-11-24
    • 2017-02-06
    • 1970-01-01
    • 1970-01-01
    • 2014-10-10
    • 2016-03-05
    相关资源
    最近更新 更多