Author
Storing a pdf file in database
Shyam Sunder
Greenhorn
Joined: Dec 23, 2006
Posts: 24
posted Dec 24, 2006 23:20:00
0
Hi Friends, This is a class which is used to generate pdf . But actually it is storing in a particular hard disk location. My requirement is "it has to display on the screen" and at same time it should store in database also. waiting for your valueable suggestions. import java.io.FileNotFoundException ; import java.io.FileOutputStream ; import java.io.IOException ; import javax.xml.transform.Result ; import javax.xml.transform.Source ; import javax.xml.transform.Transformer ; import javax.xml.transform.TransformerConfigurationException ; import javax.xml.transform.TransformerException ; import javax.xml.transform.TransformerFactory ; import javax.xml.transform.sax.SAXResult ; import javax.xml.transform.stream.StreamSource ; import org.apache.avalon.framework.logger.ConsoleLogger; import org.apache.avalon.framework.logger.Logger; import org.apache.fop.apps.Driver; import org.apache.fop.apps.FOPException; import org.apache.fop.messaging.MessageHandler; import org.xml.sax.InputSource ; public class PDFCreator { public PDFCreator(String strSrcXMLPath, String strSrcXSLTPath, String strTarget) { createPDFFromXMLXSL(strSrcXMLPath, strSrcXSLTPath, strTarget); } public void simplePDFCreator(String strSrcPath, String strTarget) { Driver driver = null; Logger logger = null; try { driver = new Driver(); driver.setRenderer(Driver.RENDER_PDF); logger = new ConsoleLogger(ConsoleLogger.LEVEL_INFO); MessageHandler.setScreenLogger(logger); driver.setLogger(logger); driver.setInputSource(new InputSource (strSrcPath)); driver.setOutputStream(new FileOutputStream (strTarget)); driver.run(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (FOPException e) { e.printStackTrace(); } } public boolean createPDFFromXMLXSL(String strSrcPathXML, String strSrcPathXSLT, String strTarget) { boolean bCreated = false; Driver driver = null; Logger logger = null; try { driver = new Driver(); driver.setRenderer(Driver.RENDER_PDF); logger = new ConsoleLogger(ConsoleLogger.LEVEL_INFO); MessageHandler.setScreenLogger(logger); driver.setLogger(logger); driver.setOutputStream(new FileOutputStream (strTarget)); Result result = new SAXResult (driver.getContentHandler()); Source xmlSource = new StreamSource (strSrcPathXML); Source xsltSource = new StreamSource (strSrcPathXSLT); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(xsltSource); transformer.transform(xmlSource, result); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } return bCreated; } public static void main (String args[]) { new PDFCreator("D:/EclipseProjects/com/poc/xml/files/temp.xml", "D:/EclipseProjects/com/poc/xml/files/Sample.xsl", "D:/EclipseProjects/com/poc/xml/pdf/sample.pdf"); } }
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 26201
posted Dec 25, 2006 18:49:00
0
Shyam, It sounds like you have two requirements. You can do both, just one at a time within the code. 1) To write to database - use a BLOB and store it in the database through JDBC 2) To display on screen - Are you using a web browser, Java application, etc?
[Blog ] [JavaRanch FAQ ] [How To Ask Questions The Smart Way ] [Book Promos ]
Blogging on Certs: SCEA Part 1 , Part 2 & 3 , Core Spring 3 , OCAJP , OCPJP beta , TOGAF part 1 and part 2
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 26201
posted Dec 25, 2006 18:52:00
0
I see you've asked this question in another thread . In the future, please do not cross post.
subject: Storing a pdf file in database