XSLT中的页脚(xml-html-pdf转换)

时间:2016-06-16 06:43:09

标签: java xslt

有没有办法使用xslt转换在生成的pdf文件上添加页脚?

我使用此代码将对象转换为xml,将xml转换为html,将html转换为pdf:

    ObjectModel obj = new Object();
    File file = new File("test.xml");
    JAXBContext jaxbContext = JAXBContext.newInstance(ObjectModel.class);
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();


    // output pretty printed
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    jaxbMarshaller.marshal(obj, file);
    jaxbMarshaller.marshal(obj, System.out);



    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer(new StreamSource("cert.xsl"));
    transformer.transform(new StreamSource("test.xml"),new StreamResult(new FileOutputStream("sample.html")));
    String File_To_Convert = "sample.html";
    String url = new File(File_To_Convert).toURI().toURL().toString();
    System.out.println(""+url);
    String HTML_TO_PDF = "ConvertedFile.pdf";
    OutputStream os = new FileOutputStream(HTML_TO_PDF);       
    ITextRenderer renderer = new ITextRenderer();
    renderer.setDocument(url);      
    renderer.layout();
    renderer.createPDF(os);        
    os.close();

xsl文件如下:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns:java="http://xml.apache.org/xalan/java"
            exclude-result-prefixes="java"
            version="2.0">
<xsl:output method="xml" indent="yes" encoding="utf-8" doctype-public="-//W3C//DTD XHTML 1.1//EN"
            doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" omit-xml-declaration="no"/>
<xsl:template match="/">
    <html>
        <body style="font-family:Book Antiqua;">
            //content here


        </body>
    </html>
</xsl:template>

我想知道是否可以在此转换中添加页脚。我找到了这个thread,但我仍然不知道如何做到这一点。

1 个答案:

答案 0 :(得分:0)

您可以通过Apache FOP进行直接的XML到PDF转换。

使用XSL-FO,static-content允许您制作一些页眉和页脚。

<fo:page-sequence master-reference="main">
  <fo:static-content flow-name="header">
    <xsl:call-template name="MyHeader"/>
  </fo:static-content>
  <fo:static-content flow-name="footer">
    <xsl:call-template name="MyFooter"/>
  </fo:static-content>
  <fo:flow>
    ...
  </fo:flow>
</fo:page-sequence>