0
votes

I am trying to print an XML file "AS-IS" i,e. with tags into PDF using Apache FOP. I am started with ExampleXML2PDF example file in src dir of FOP 1.1. I am able to print formatted XML objects, not able to print XML with tags.

XSL is as below,

<xsl:stylesheet version="2.0" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="testRaw" />
<xsl:template match="/">
    <fo:root>
        <fo:layout-master-set>
            <fo:simple-page-master master-name="my-page">
                <fo:region-body margin="0.5in" />
            </fo:simple-page-master>
        </fo:layout-master-set>
        <fo:page-sequence master-reference="my-page">
            <fo:flow flow-name="xsl-region-body" font="7pt Times">
                <fo:block break-before="page">
                    <xsl:value-of select="$testRaw" />
                </fo:block>
            </fo:flow>
        </fo:page-sequence>
    </fo:root>
</xsl:template>

Java code used to set parameters.

2
Are you using XSLT like the example? If so, use CDATA. If you give an example of your XSLT where you're trying to output literal XML, I can add show you.Daniel Haley
Thanks Daniel, I have edited question to have code sample.Tejas jain

2 Answers

1
votes

Try replacing your xsl:value-of with xsl:copy-of and wrap it in CDATA.

Example...

<xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
<xsl:copy-of select="$testRaw" />
<xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
0
votes

Use bufferwriter, it should look something like this:

public void readXML() {
            String name = "TheFile.xml";
            String path = "C:/User/Desktop/";
            File file = new File(path + name);
            if(file.canRead()) {
                String FileTxt = "";
                if((path + name).endsWith(".xml")) {
                    try {
                        Scanner read = new Scanner(new FileInputStream(file));
                        while (read.hasNextLine()) {
                            FileTxt += read.nextLine();
                        }
                        read.close();
                    } catch(FileNotFoundException e) {}
                    writePDF(FileTxt);
                }

            } else {
                System.out.print("Cannot Open File!");
            }
}

public void writePDF(String text) {
      String path = "C:/User/Desktop/"
      String name = "TheFile.pdf"
      if(!path.endsWith(name)) path += name;
                try {
                     BufferedWriter write = new BufferedWriter(new FileWriter(path));
                    write.write(text);
                    write.close();
                } catch(Exception e) {}
}