Pablo (archive)
Usenet Archive
- Joined
- Nov 10, 2000
- Messages
- 26
- Reaction score
- 0
Dear all,
I am trying to use Apache FOP to create PDF from URLs, but something is going wrong.
I am using JTidy to convert a URL to XHTML, and then FOP to transform this XHTML to PDF.
I am using a xslt file that I have downloaded here to transform xhtml to FO format: http://www.antennahouse.com/XSLsample/XSLsample.htm and http://www-128.ibm.com/developerworks/library/x-xslfo2app/
I am wondering if anyone have a working example of FOP to generate PDFs from HTML that could be shared here.
My code is bellow:
public static void main(String args) {
PDFMaker pdf = new PDFMaker;
pdf.setPdfPath("teste.pdf");
pdf.setUrl("http://validator.w3.org/"); pdf.setXmlPath("test.xml"); pdf.setXsltPath("xhtml2fo-ms.xsl"); pdf.convert2PDF; }
public void convert2PDF {
try {
// Setup directories
File baseDir = new File("e:/pdf-temp/");
File outDir = new File(baseDir, "out");
outDir.mkdirs;
// Setup input and output files
File xmlfile = this.convert2Xhtml(baseDir,
this.getXmlPath); //input xml using JTidy
File xsltfile = new File(baseDir, this.getXsltPath); // xslt style sheet File pdffile = new File(outDir, this.getPdfPath); // output pdf
// configure fopFactory as desired
FopFactory fopFactory = FopFactory.newInstance;
// configure foUserAgent as desired
FOUserAgent foUserAgent = fopFactory.newFOUserAgent;
// Setup output
OutputStream out = new java.io.FileOutputStream(pdffile); out = new java.io.BufferedOutputStream(out);
try {
// Construct fop with desired output format Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
// Setup XSLT
TransformerFactory factory =
TransformerFactory.newInstance;
Transformer transformer = factory.newTransformer(new StreamSource(xsltfile));
// Setup input for XSLT transformation
Source src = new StreamSource(xmlfile);
// Resulting SAX events (the generated FO) must be piped through to FOP Result res = new SAXResult(fop.getDefaultHandler);
//set transformer parameters
transformer.setParameter("title-text","PDFTester"); transformer.setParameter("docid-text","JK130759"); transformer.setParameter("version-text","V0.0"); transformer.setParameter("status-text","Onbekend"); transformer.setParameter("authorizor-text","Jacques Kors"); transformer.setParameter("date-text",(new SimpleDateFormat("dd/MM/yyyy")).format(new Date)); transformer.setParameter("copytype-text","Test kopie");
// Start XSLT transformation and FOP processing transformer.transform(src, res); } finally { out.close; } } catch (Exception e) { e.printStackTrace(System.err); System.exit(-1); } }
I am trying to use Apache FOP to create PDF from URLs, but something is going wrong.
I am using JTidy to convert a URL to XHTML, and then FOP to transform this XHTML to PDF.
I am using a xslt file that I have downloaded here to transform xhtml to FO format: http://www.antennahouse.com/XSLsample/XSLsample.htm and http://www-128.ibm.com/developerworks/library/x-xslfo2app/
I am wondering if anyone have a working example of FOP to generate PDFs from HTML that could be shared here.
My code is bellow:
public static void main(String args) {
PDFMaker pdf = new PDFMaker;
pdf.setPdfPath("teste.pdf");
pdf.setUrl("http://validator.w3.org/"); pdf.setXmlPath("test.xml"); pdf.setXsltPath("xhtml2fo-ms.xsl"); pdf.convert2PDF; }
public void convert2PDF {
try {
// Setup directories
File baseDir = new File("e:/pdf-temp/");
File outDir = new File(baseDir, "out");
outDir.mkdirs;
// Setup input and output files
File xmlfile = this.convert2Xhtml(baseDir,
this.getXmlPath); //input xml using JTidy
File xsltfile = new File(baseDir, this.getXsltPath); // xslt style sheet File pdffile = new File(outDir, this.getPdfPath); // output pdf
// configure fopFactory as desired
FopFactory fopFactory = FopFactory.newInstance;
// configure foUserAgent as desired
FOUserAgent foUserAgent = fopFactory.newFOUserAgent;
// Setup output
OutputStream out = new java.io.FileOutputStream(pdffile); out = new java.io.BufferedOutputStream(out);
try {
// Construct fop with desired output format Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
// Setup XSLT
TransformerFactory factory =
TransformerFactory.newInstance;
Transformer transformer = factory.newTransformer(new StreamSource(xsltfile));
// Setup input for XSLT transformation
Source src = new StreamSource(xmlfile);
// Resulting SAX events (the generated FO) must be piped through to FOP Result res = new SAXResult(fop.getDefaultHandler);
//set transformer parameters
transformer.setParameter("title-text","PDFTester"); transformer.setParameter("docid-text","JK130759"); transformer.setParameter("version-text","V0.0"); transformer.setParameter("status-text","Onbekend"); transformer.setParameter("authorizor-text","Jacques Kors"); transformer.setParameter("date-text",(new SimpleDateFormat("dd/MM/yyyy")).format(new Date)); transformer.setParameter("copytype-text","Test kopie");
// Start XSLT transformation and FOP processing transformer.transform(src, res); } finally { out.close; } } catch (Exception e) { e.printStackTrace(System.err); System.exit(-1); } }