如何使用 XSL-FO 将源 XML 打印成 PDF? [英] How to print source XML into a PDF using XSL-FO?

查看:39
本文介绍了如何使用 XSL-FO 将源 XML 打印成 PDF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天!我需要编写一个 xsl-fo 模板,但我无权访问源 XML.有没有办法如何将源 XML 打印到 PDF 中,以便我可以从 PDF 复制它并粘贴到文件中?它应该与源 xml 具有相同的结构,包括属性.请问怎么做?先感谢您!沃捷

Good day! I need to write a xsl-fo template but I don't have access to the source XML. Is there a way how top print the source XML into a PDF so that I can copy it from the PDF then and paste into a file? It should have the same structure as the source xml including attributes. Please how to do it? Thank you in advance! Vojtech

已我有一个 Web 界面,可以在其中粘贴模板并生成 PDF.但我不完全知道用作数据源的 XML 的结构是什么.所以我需要编写另一个模板来读取输入的 XML(元素、属性、结构)并将其写入 PDF.我想复制 PDF 的内容,然后将其保存到 file.xml 中,以便我可以研究它.

Edited: I have a web interface where I can paste my template and a PDF is generated. But I don't exactly know what is the structure of the XML used as data source. So I need to write another template which will read input XML (elements, attributes, structure) and write it into a PDF. I'd like to copy content of the PDF then and save it into a file.xml so that I can study it.

推荐答案

这是另一个大大简化的选项;只需打印整个 XML 的副本.

Here's another option that is greatly simplified; just print a copy of the entire XML.

示例:

XML 输入

<doc attr="test">
  <a>Lorem ipsum dolor sit amet...</a>
  <b>
    <c>Lorem ipsum dolor sit amet...</c>
    <d>
      <e attr="another test"/>
      <f>Lorem ipsum dolor sit amet...</f>
    </d>
  </b>
</doc>

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/">
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
      <fo:layout-master-set>
        <fo:simple-page-master master-name="my-page" page-width="8.5in" page-height="11in">
          <fo:region-body margin="1in" margin-top="1.5in" margin-bottom="1.5in"/>
        </fo:simple-page-master>
      </fo:layout-master-set>
      <fo:page-sequence master-reference="my-page">
        <fo:flow flow-name="xsl-region-body" font-family="monospace">
          <fo:block white-space-collapse="false" white-space-treatment="preserve" linefeed-treatment="preserve">
            <xsl:text disable-output-escaping="yes">
              &lt;![CDATA[
            </xsl:text>
            <xsl:copy-of select="/*"/>
            <xsl:text disable-output-escaping="yes">
              ]]&gt;
            </xsl:text>
          </fo:block>
        </fo:flow>
      </fo:page-sequence>
    </fo:root>
  </xsl:template>

</xsl:stylesheet>

XSL-FO 输出

<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
   <fo:layout-master-set>
      <fo:simple-page-master master-name="my-page" page-width="8.5in" page-height="11in">
         <fo:region-body margin="1in" margin-top="1.5in" margin-bottom="1.5in"/>
      </fo:simple-page-master>
   </fo:layout-master-set>
   <fo:page-sequence master-reference="my-page">
      <fo:flow flow-name="xsl-region-body" font-family="monospace">
         <fo:block white-space-collapse="false" white-space-treatment="preserve" linefeed-treatment="preserve">
              <![CDATA[
            <doc attr="test">
               <a>Lorem ipsum dolor sit amet...</a>
               <b>
                  <c>Lorem ipsum dolor sit amet...</c>
                  <d>
                     <e attr="another test"/>
                     <f>Lorem ipsum dolor sit amet...</f>
                  </d>
               </b>
            </doc>
              ]]>
            </fo:block>
      </fo:flow>
   </fo:page-sequence>
</fo:root>

PDF 输出 (Apache FOP)

PDF Output (Apache FOP)

这篇关于如何使用 XSL-FO 将源 XML 打印成 PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆