Creating PDF File on Google App Engine

PDF_on_AppEngine

As part of an application I’m working on, I’d like to create documents in PDF format. I’ve mentioned this before, but it wasn’t until last weekend that I actually got started implementing my solution. The solution consists of generating HTML documents, exporting them to Google Docs and finally, exporting that newly created document to a PDF. Sounds complicated, but it turns out it was actually fairly easy.

download an example project

The example application consists of three parts:

  • There’s an RPC servlet that reacts to the user’s input. It will first use Apache Velocity to create a HTML file based on a template. There are a few things to take care of when running Velocity on App Engine, but nothing showstopping as with many of the other PDF and template libraries. I’ve used Jurgen van de Kamp’s explanation and classes. It worked flawlessly.
  • Together with the creation of the HTML, the RPC servlet will also create a new Google Document and send the HTML to the document. You’ll need to fill in a Google Docs user and password before this part works. Because of Google’s well documented API’s, this is a breeze:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public DocumentListEntry createNewDocument(String title, String content) throws ServiceException {
	try {
		URL feedUrl = new URL("https://docs.google.com/feeds/default/private/full/");
		DocumentListEntry newEntry = new DocumentEntry();
		newEntry.setTitle(new PlainTextConstruct(title));
		newEntry = client().insert(feedUrl, newEntry);
 
		newEntry.setMediaSource(new MediaByteArraySource(content.getBytes(), "text/html"));
		newEntry = newEntry.updateMedia(true);
		return newEntry;
	} catch (MalformedURLException e) {
		throw new RuntimeException(e);
	} catch (IOException e) {
		throw new RuntimeException(e);
	}
}
  • The final part is a normal (not GWT RPC) servlet that exports the PDF from Google Docs and returns it. I had to use a second servlet because the RPC format is limited. Again this code is very compact:
1
2
3
4
5
6
7
8
9
10
11
public InputStream getPdfInputStream(DocumentListEntry document) throws ServiceException {
	String exportUrl = ((MediaContent)document.getContent()).getUri() + "&exportFormat=pdf";
	MediaContent mc = new MediaContent();
	mc.setUri(exportUrl);
	try {
		MediaSource ms = client().getMedia(mc);
		return ms.getInputStream();
	} catch (IOException e) {
		throw new RuntimeException(e);
	}
}

A few things to note:

  • I’ve only shown a few highlights, you should really download the code to see how little there is to get such a fairly complicated result.
  • I haven’t really thought too hard about architecture and stuff like exception handling. So you might want to do some tuning if you actually use the code.
  • For this project, I haven’t use Maven, I just used the Eclipse plugins too quickly generate an application. In fact, it worked so quick I’m considering dumping Maven. Maven has some serious advantages, but the number of hoops I have to jump through keeps increasing exponentially. Especially since Google doesn’t offer their own Maven repositories.

download an example project

This entry was posted in Java and JavaScript. Bookmark the permalink. Both comments and trackbacks are currently closed.

One Trackback

  1. By Generating PDF files on Google App Engine on August 27, 2010 at 10:05 am

    [...] while ago, I explained the way I was going to create PDF files on Google AppEngine. Turns out that, once I started to really test this solution, there were a few holes in my [...]

  • Feedback or questions? Contact me right away.

    Comments have been disabled on my posts. Not because I don't want to hear from you, but because they were adding very little to the conversation (most of them were spam anyway). I do listen to you and try to keep as much posts as possible up-to-date and error free. So if you have a question, if something isn't working the way you hoped or you have general feedback, please use the contact form below. I guarantee an answer to every honest question or remark.
  • Get in touch
    1. (required)
    2. (valid email required)
     

    cforms contact form by delicious:days