使用JDOM處理XML資料之PDF篇(一) (轉)
使用J處理資料之PDF篇(一)microsoft-com::office" />
處理XML資料的三種方式我們已經都介紹過了,
(直接讀取/Develop/read_article.?id=20720">
和使用T )現在來看一下第三種方式即轉化成二進位制格式(一般為PDF)的操作。這個操作要藉助.org/"> 提供的包fop來完成。其實這個是我整理的,不是自己寫的:)它的程式碼如下:
package XML;
import .io.*;
import org.xml.sax.Input;
import org.xml.sax.XMLReader;
import org.apache.fop.apps.;
import org.apache.fop.apps.Version;
import javax.xml.tranorm.*;
import javax.xml.transform.stream.*;
/**
* Class to convert an XML document to PDF via
* XSLT and XSL formatting s.
*/
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("JAconfiguration 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 ba= 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部落格 ” ,連結:http://blog.itpub.net/10752019/viewspace-984952/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 使用JDOM處理XML資料之PDF篇(二) (轉)XML
- 使用JDOM處理XML資料之XSLT篇(一) (轉)XML
- 使用JDOM處理XML資料之XSLT篇(二) (轉)XML
- 用JDOM處理XML文件 (轉)XML
- 使用jdom解析XMLXML
- 在.NET Framework中輕鬆處理XML資料(一) (轉)FrameworkXML
- Python資料處理(一):處理 JSON、XML、CSV 三種格式資料PythonJSONXML
- 使用JDom從Java後臺給Flex前端傳遞xml資料JavaFlex前端XML
- 在.NET Framework中輕鬆處理XML資料(五) (轉)FrameworkXML
- 第四篇:資料預處理(一) - 缺失值處理
- JSP筆記-XML 資料處理JS筆記XML
- 處理XML資料應用實踐XML
- Flex 3處理資料 訪問 XML 資料4FlexXML
- jdom解析xml檔案XML
- 有關jdom解析xmlXML
- Flex3處理資料 訪問 XML 資料1FlexXML
- Linux上使用python處理docx轉pdf教程LinuxPython
- 機器學習 第3篇:資料預處理(使用插補法處理缺失值)機器學習
- 使用JAXB處理JAVA和XMLJavaXML
- 海量資料處理 (轉)
- 用JDOM操作XML檔案XML
- JDom讀寫XML(程式碼)XML
- ASP.NET 2.0中XML資料的處理ASP.NETXML
- [轉]使用 SAX 處理 XML 文件 和與DOM的區別XML
- ASP+SQL Server之圖象資料處理 (轉)SQLServer
- Sklearn之資料預處理——StandardScaler歸一化
- 資料處理及跳轉
- SqlServer 2005處理xml格式 (轉)SQLServerXML
- 第五篇:資料預處理(二) - 異常值處理
- java 如何簡單快速處理 xml 中的資料JavaXML
- 深度學習——資料預處理篇深度學習
- JavaScript 資料處理 - 對映表篇JavaScript
- 用JDOM讀取XML檔案XML
- 資料處理之去除重複資料
- 使用Spark和Cassandra進行資料處理(一)Spark
- 使用Excel高效處理資料Excel
- 使用openpyxl處理表格資料
- ■ 例項學習 PHP 之表單處理篇 (轉)PHP