PDF of XML data using JDOM (1)

zhaozj2021-02-17  50

PDF of XML data using JDOM (1)

Three ways to handle XML data We have already introduced,

(Directly read http://www.9cbs.net/develop/read_article.asp?id=20720

And using xslt http://www.9cbs.net/develop/read_article.asp?id=20733) Now let's take a look at the third mode to convert operations to binary formats (generally PDF). This operation is done with the HTTP: //xml.apache.org/ of the package FOP. In fact, this is what I sorted out, not written by :) Its code is as follows:

Package XML;

Import java.io. *;

Import org.xml.sax.inputsource;

Import org.xml.sax.xmlreader;

Import org.apache.fop.apps.driver;

Import org.apache.fop.apps.version;

Import javax.xml.transform. *;

Import javax.xml.transform.stream. *;

/ **

* Class to Convert An XML Document To PDF VIA

* Xslt and xsl formatting objects.

* /

Public class pdfwriter {

Protected Transformer Transformer = NULL;

Public pdfwriter () {};

Public pdfwriter (streamsource Source) throws transformerconfigurationException {

// try {

TransformerFactory Factory = TransformerFactory.newinstance ();

Transformer = factory.newtransformer (SOURCE);

/ *} Catch (TransformerConfigurationException TCE) {

Throw New IllegalStateException ("Stylesheet CompiLation Problem: Tce.getMessage ());

} catch (TransformerFactoryConfigurationException TFCE) {

Throw New ILLEGALSTATEEXCEPTION ("JAXP Configuration Problem: Tce.getMessage ());

}

* /

}

Public Pdfwriter (String XslFilePath)

Throws TransformerConfigurationException, FileNotFoundException {

NEW StreamSource (New FileInputStream (XslFilePath));

}

/ ** Invoke the asf FOP ENGINE to CREATE THE PDF * /

Protected byte [] invokefop (InputSource Fosource) throws exception {

ByteArrayoutputstream out = new byteArrayoutputStream ();

Driver Driver = New Driver (Fosource, OUT);

Driver.run ();

Return out.tobytearray ();

/ ** CREATE A PDF from an Xml File, Using The Stylesheet Passed to the Constructor. * /

Public Byte [] generatepdf (string xmlfilepath) throws exception {

StreetSource Xmlsource = New StreamSource (New FileInputStream (XMLFilePath));

Return generatepdf (xmlsource);

}

/ ** Does the intencement stuff * /

Public Byte [] generatepdf (streamsource xmlsource) throws exception {

BYTEARRAYOUTPUTSTREAM baos = new byteArrayoutputStream ();

StreamResult Foresult = New StreamResult (baos);

Transformer.Transform (Xmlsource, Foresult);

BYTEARRAYINPUTSTREAM bais = new byteArrayInputStream (baos.tobytearray ());

Return INVOKEFOP (New InputSource (bais));

}

/ ** to be used with files * /

Public static void createpdffromxml (String Xslfilepath, String outputpdfpath) throws exception {

FileOutputStream Fos = New FileOutputStream (Outputpdfpath);

CreatepdffRomXML (XslfilePath, New FileInputStream (XMLFilePath), FOS);

Fos.close ();

}

/ ** to be caled when it is no spoon (xml or pdf file) * /

Public static void createpdffromxml (String XslFilepath, InputStream Xmlin, OutputStream Pdfout) throws exception {

Pdfwriter Writer = New Pdfwriter;

Byte [] pdfbytes = Writer.Generate (XMLIN));

Pdfout.write (pdfbytes, 0, pdfbytes.length);

}

Public static void main (String [] args) {

String filebasepath = "." File.separator;

String XmlFilePath = filebasepath "watchlist.xml";

String xslfilepath = filebasepath "watchlist2pdf.xsl";

String outputpdfpath = filebasepath "watchlist.pdf";

Try {

Pdfwriter.createpdffromxml (Xslfilepath, XMLFilePath, Outputpdfpath);

} catch (exception e) {system.out.println (E.getMessage ());

E.PrintStackTrace ();

}

}

}

This JavaBean enters the location of the XSL-FO file, an XML file location, outputs a PDF file.

转载请注明原文地址:https://www.9cbs.com/read-29776.html

New Post(0)