• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
How-to: on-demand PDF using Apache FOP
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How-to: on-demand PDF using Apache FOP


  • Subject: How-to: on-demand PDF using Apache FOP
  • From: Zak Burke <email@hidden>
  • Date: Tue, 06 Dec 2005 09:07:22 -0500

I saw the iText thread here a few days ago and thought I'd post an an on-demand PDF example using Apache FOP 0.90 (http://xmlgraphics.apache.org/fop/). Many FO examples are available here: http://www.dpawson.co.uk/xsl/sect3/.

I've heard that there are instructions on how to do this in the omnigroup archive, but that search engine seems to ignore many short keywords. (Searching for "apache fo xml pdf" I get "ignored: fo xml pdf" and then a bunch of helpful but irrelevant links about configuring Apache httpd, and nothing matches "fop".) I don't know how you'll ever find this post then, but I digress.

I am cross-posting this to the apple list because the only posts I found there refer to version (0.20.x) and those instructions do not work for version 0.90.x.

Finally, it is important to add "-Djava.awt.headless=true" to the "Additional Arguments" list to suppress a console window running WOBootstrap that will try to pop up and then fail to close, which seems to prevent the thread from returning, which causes the app to hang.

Enjoy,

zak.


private ByteArrayOutputStream pdf() { // binary container for PDF ByteArrayOutputStream out = null;

try
{
// assume these exist
String xsl, xml;

TransformerFactory transformerFactory = TransformerFactory.newInstance();

// configure FOP, apache's XML->PDF transformer
Fop fop = new Fop(MimeConstants.MIME_PDF);
out = new ByteArrayOutputStream();
fop.setOutputStream(out);

// configure XSLT transformer
Source xsltSrc = new StreamSource(new StringReader(xsl));
Transformer transformer = transformerFactory.newTransformer(xsltSrc);

// pipe XSL transformation through to FOP
Result res = new SAXResult(fop.getDefaultHandler());

// grab XML input stream
Source src = new StreamSource(new StringReader(xml));

// Start the transformation and rendering process
transformer.transform(src, res);
}
catch (Exception e)
{
	// actually, catch the following, one by one:
	// TransformerConfigurationException
	// FOPException
	// TransformerFactoryConfigurationError
	// TransformerException
}

return out;
}


public void appendToResponse(WOResponse response, WOContext context) { ByteArrayOutputStream out = pdf();

// without data, show the PDF page, which is just an error message.
if (out == null)
super.appendToResponse(response, context);

// assume this exists
String filename;

response.setHeader("application/pdf", "Content-Type");
response.setHeader("" + out.size() + "", "Content-Length");
response.setHeader("attachment;filename=" + filename, "Content-Disposition");
response.setContent(new NSData(out.toByteArray()));
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden
  • Follow-Ups:
    • Re: How-to: on-demand PDF using Apache FOP
      • From: Stefan Pantke <email@hidden>
  • Prev by Date: Tomcat Installer Update (Take 2)
  • Next by Date: Re: How to manage long running tasks
  • Previous by thread: Tomcat Installer Update (Take 2)
  • Next by thread: Re: How-to: on-demand PDF using Apache FOP
  • Index(es):
    • Date
    • Thread