【问题标题】:unable to generate the PDF from the XML string using xsl-fo in Fop无法在 Fop 中使用 xsl-fo 从 XML 字符串生成 PDF
【发布时间】:2012-04-26 05:02:03
【问题描述】:

我正在使用 Fop 生成 PDF 文件,我有 xml 和 xsl 源来转换 xsl-fo 对象,但 PDF 文件显示为空白,请帮助我...

这是我作为字符串的 XML 响应:

 <enqResponse>…<cols><c>ID</c><c>DESCRIPTION</c></cols>….<r><c><cap>1111</cap><i>4</i></c><c><cap>Banks</cap></c></r>… </enqResponse>

这是我的 Xslt 文件:

 <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:java="http://xml.apache.org/xslt/java" exclude-result-prefixes="java">
    <xsl:output method="xml" version="1.0" omit-xml-declaration="no" indent="yes"/>
    <xsl:template match="enqResponse">
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:fox="http://xml.apache.org/fop/extensions">
      <fo:layout-master-set>
        <fo:simple-page-master master-name="my-page" page-width="50in" page-height="80in">
          <fo:region-body  margin-top="1in" margin-bottom="1in" />
          <fo:region-before extent="2in"/>
        </fo:simple-page-master>
      </fo:layout-master-set>
      <fo:page-sequence master-reference="my-page">
          <fo:static-content flow-name="xsl-region-before">
               <fo:block font-size="12pt" font-weight="bold" text-align="center"><xsl:value-of select="title"/></fo:block>
          </fo:static-content>
          <fo:flow flow-name="xsl-region-body">
          <fo:block><xsl:apply-templates select="cols"/></fo:block>
          <fo:block><xsl:apply-templates select="r"/></fo:block>     
        </fo:flow>
        </fo:page-sequence>
    </fo:root>
    </xsl:template>
    <xsl:template match="cols">
                <fo:table table-layout="fixed" width="100%">
                <!--<fo:table-column column-width="2in"/>
                <fo:table-column column-width="4in"/>
                  --><fo:table-body>
                            <fo:table-row>
                             <xsl:for-each select="c">
                            <fo:table-cell>
                                        <fo:block><xsl:value-of select="."/></fo:block>
                            </fo:table-cell> 
                     </xsl:for-each>                
                    </fo:table-row>
              </fo:table-body>
                </fo:table>
    </xsl:template>

    <xsl:template match="r">
         <fo:table table-layout="fixed" width="100%">
                <!--<fo:table-column column-width="12in"/>
                <fo:table-column column-width="12in"/>
                     --><fo:table-body>
                     <fo:table-row>
                            <xsl:for-each select="c">
                                  <fo:table-cell>
                                  <fo:block><xsl:value-of select="cap"/></fo:block>
                            </fo:table-cell>
                     </xsl:for-each>  
                </fo:table-row>
                </fo:table-body>
                </fo:table>
    </xsl:template>
    </xsl:stylesheet>

当我将 xml 下载为 PDF 时,它会生成空白 pdf 文件,请就此提出您的建议,并让我知道 xslt 文件中的任何错误。

我有带有静态内容的 xslt 文件,并且它以 PDF 正确显示为带有静态内容的 Xslt 文件

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:java="http://xml.apache.org/xslt/java" exclude-result-prefixes="java">
    <xsl:output method="xml" version="1.0" omit-xml-declaration="no" indent="yes"/>
    <xsl:template match="enqResponse">
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:fox="http://xml.apache.org/fop/extensions">
      <fo:layout-master-set>
        <fo:simple-page-master master-name="my-page">
          <fo:region-body margin="2in"/>
          <fo:region-before extent="1.0cm"/>
        </fo:simple-page-master>
      </fo:layout-master-set>
      <fo:page-sequence master-reference="my-page">
          <fo:static-content flow-name="xsl-region-before" >
               <fo:block font-size="12pt" text-align="center">Page-1/1</fo:block>
          </fo:static-content>
          <fo:flow flow-name="xsl-region-body">
         <fo:block space-after="1.0cm">Welcome</fo:block>
          <fo:block>
                <fo:table table-layout="fixed" width="100%">
                       <fo:table-body>
                            <fo:table-row>
                      <fo:table-cell>
                            <fo:block>No</fo:block>
                      </fo:table-cell>
                      <fo:table-cell>
                            <fo:block>Name</fo:block>
                      </fo:table-cell>
                      <fo:table-cell>
                            <fo:block>Phone Number</fo:block>
                      </fo:table-cell>
                    </fo:table-row>

                    <fo:table-row>
                      <fo:table-cell>
                            <fo:block>Test1</fo:block>
                      </fo:table-cell>
                      <fo:table-cell>
                            <fo:block>Test2</fo:block>
                      </fo:table-cell>
                      <fo:table-cell>
                            <fo:block>Test3</fo:block>
                      </fo:table-cell>
                    </fo:table-row>
                       </fo:table-body>
                </fo:table>
          </fo:block>
        </fo:flow>
      </fo:page-sequence>
    </fo:root>
    </xsl:template>
    </xsl:stylesheet>

请给出从给定的xml字符串中提取动态数据生成PDF文件的解决方案......

【问题讨论】:

  • 你使用的是哪个版本的XSL-FO处理器,运行时的错误输出是什么?
  • @Woody - 处理器是 FOP。它在标题和问题的第一句话中提到。
  • 抱歉——我的错,我没有看到在这两个地方使用的是什么版本的 FOP 处理器——我猜我的阅读能力不如你!由于 0.2/0.9x 和 1.0 版本之间的输出和处理发生了巨大变化,这有点相关。

标签: java xslt xsl-fo


【解决方案1】:

使用 FOP 或 XEP (RenderX),我得到一个包含生成文本的 pdf。 (不过,有些文本不在左边距。)

您确定您的 PDF 是空白的吗?由于您正在生成一个 50 英寸 x 80 英寸的页面(哇!),也许您只是看不到文字。

【讨论】:

    猜你喜欢
    • 2018-06-22
    • 1970-01-01
    • 2010-11-09
    • 1970-01-01
    • 1970-01-01
    • 2011-04-09
    • 1970-01-01
    • 2019-08-26
    • 1970-01-01
    相关资源
    最近更新 更多