Bookmark Topic Watch Topic
  • New Topic

Storing a pdf file in database

 
Shyam Sunder
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34839
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Report post to moderator
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?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34839
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Report post to moderator
I see you've asked this question in another thread. In the future, please do not cross post.
 
    Bookmark Topic Watch Topic
  • New Topic