ITPub博客

首页 > 应用开发 > IT综合 > 使用JDOM处理XML数据之PDF篇(一) (转)

使用JDOM处理XML数据之PDF篇(一) (转)

原创 IT综合 作者:amyz 时间:2007-11-26 16:13:42 0 删除 编辑
使用JDOM处理XML数据之PDF篇(一) (转)[@more@]

使用Jdom处理XML数据之PDF篇(一)microsoft-com:Office:office" />

 处理XML数据的三种方式我们已经都介绍过了,

 (直接读取.NET/Develop/read_article.ASP?id=20720">http://www.csdn.net/Develop/read_article.asp?id=20720

和使用xslT http://www.csdn.net/Develop/read_article.asp?id=20733)现在来看一下第三种方式即转化成二进制格式(一般为PDF)的操作。这个操作要借助apache.org/">http://xml.apache.org/ 提供的包fop来完成。其实这个是我整理的,不是自己写的:)它的代码如下:

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 {

  this( 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 {

  StreamSource xmlSource = new StreamSource( new FileInputStream(xmlFilePath) );

  return generatePDF(xmlSource);

  }

  /** Does the interesting 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 xmlFilePath, String outputPDFPath) throws Exception {

  FileOutputStream fos = new FileOutputStream(outputPDFPath);

  createPDFFromXML(xslFilePath, new FileInputStream(xmlFilePath), fos);

  fos.close();

  }

 

  /** To be called when there is no spoon (XML or PDF file) */

  public static void createPDFFromXML(String xslFilePath, InputStream xmlIn, OutputStream pdfOut) throws Exception {

  PDFWriter writer = new PDFWriter(xslFilePath);

  byte[] PDFbytes = writer.generatePDF( new StreamSource(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();

  }

  }

}

 

 

这个javabean输入一个XSL-FO的文件的位置,一个XML文件的位置,输出一个PDF文件。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/10752019/viewspace-984952/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
  • 博文量
    3982
  • 访问量
    7891760