tags:

views:

816

answers:

6

Hi all,

I tried to read the related questions and didn't find any new tool. Here is my problem : i generate some PDF files for an insurance from the data in my app. Stuff like, contracts, certificates, bills etc. The insurance guy gives me the templates files as .docs with fields in them and I have to fill in the fields with my data and generate the PDF. Quite a simple problem.

I don't have word, I'm in java swing.

I don't feel like re-creating he word doc in a report system (like BIRT or iReport) because it's long, painful and they often can't even do the same layout as word does.

I tried converting the .doc in .odt (OpenOffice), changing the field in form field, generating the PDF as a PDF form (non-decorated disabled fields, to look like normal text) and filing them with iText, but I get cheap results (I can't choose the font in the fields, width is fixed, I can't properly line up the baseline in the field and in the surrounding text etc.)

Do you have ideas/tools I can use for filling those fields without re-creating the doc file in a template editor ?

This question is quite open, I'm looking for ideas more than you doing my homework.

+1  A: 

I've created an entire application (actually it was ported 3 times) to solve just this case, and one of the versions was using OpenOffice, which has .Doc templates (with bookmakrs) the application then acted like a "server" which had its own built in folder watcher, it would process requirest via XML and render out PDF documents using the .doc/.dot templates.

Sadly this was run in a server environment (and caused issues, I never managed to get the headless mode to work)

But at least in theory it works.

Start at looking at the Open Office APIs, you should be able to change font/style/size etc, also why are you using fields when you could use bookmarks instead? this allows you to jump to a predefined cursor (named) and then insert anything (eg tables, pictures, text, etc)

The excellent part is that in terms of styling and modifying the templates, all you need its words/OpenOffice.

Dont know if that helps

Darknight
I don't know how bookmarks works, I will investigate that, thanks.Does the text re-layout (wrapping, hyphenation, page jumps) when I insert text in a bookmark ? Right now I'm investigating the API/live talking to OOo solution, but I'm not really confident I can do a robust stuff with that.
nraynaud
+1  A: 

If you can extract the data (maybe POI?), you could then use IText to create the PDFs.

mark stephens
Thaks. I'm wondering the width of the "seam" I would have to sew between the doc format and iText, I can't find any example on google. But the general idea is cool : having the layout engine inside java is a good thing I think.
nraynaud
+3  A: 

There are free applications for creating pdf forms, would it be possible to teach the insurance guys to use this and provide a pdf form template to your application instead of a word template? I've been involved in an application that used this approach successfully. The workflow for the user became:

  1. Create document using any application (word, photoshop, other...)
  2. Export or print as pdf
  3. Use pdf form editor to add fields in correct places. Use placeholders for contents.
  4. Upload to server

The placeholders (like ${context.person.firstname} ) were then replaced on the server, the pdf "flattened" (converts form fields to text) and the filled out pdf returned. We used a defined set of placeholders that the user could choose from and added more when there was a need for it. This was a good tradeoff between the users wanting to use their applications of choise for layout and we wanting to avoid a multitude of formats (and Office)

If I recall correctly we used Scribus as pdf form editor

henrik
That's almost what I do. But that "flattening" idea is new to me, I will investigate it, thanks.
nraynaud
Yeah, looks like it's not a complex code : http://1t3xt.info/api/com/lowagie/text/pdf/PdfStamper.html#setFormFlattening(boolean)
nraynaud
+1  A: 

You can convert the doc file into a rtf file : theses files use explicit "marks" to define the style (like HTML) : "{\b bold test}{\i italic test}", for example.

So, the file is quite "understandable" by a human.

In this file, you can put "tags", delimited by characters of your choice : "%%first_name%%" for example.

In java, you can dynamically replace theses tags by your information. To finish, you just have to convert this rtf file into a pdf file (API, virtual printer...).

With this method, you can replace the rtf format by any "mark" format easily understable.

Benoit Courtine
I tried this way in the beginning, because I prefer handling text formats that binary ones for obvious reasons, but my document's layout is too complex for RTF. Thanks.
nraynaud
A: 

Take a look here: http://www.dancrintea.ro/doc-to-pdf/

mj
+1  A: 

A bit late, but in case others are looking - try Docmosis. It uses documents as templates (doc or odt) and can populate and control the document from Java. It can spit out pdf,doc,odf etc. It's a productised version of Darknights solution (above) - and it's free!

jowierun