1

I want to build a web application for generating stylish PDF documents. The layout should be based on a design templates and the data should come dynamically from the database. Ideally I want to design the template in a "publishing like" tool with placeholders and replace these placeholders by the web application with the data from the database. Think of something like an invoice generator, where a customer could choose from different invoice templates and the invoice data itself coming from the DB. Thanks for your ideas!

1

3 Answers 3

2

I use perl (with HTML::Template) to generate an .ods document from database data, then I use unoconv to convert it to pdf. To be precise:

  • first, create an .ods document that looks good. Make sure the variable fields are recognizable.
  • unzip the .ods document and edit the file contents.xml.
  • in contents.xml, change all the variable fields to <TMPL_VAR> tags as per HTML::Template. This contents.xml file is going to be your template.
  • write a Perl program that reads the database, fills the TMPL_VAR fields, writes a new contents.xml file and re-zips the new contents.xml file into a new .ods document.
  • then use unoconv to convert the new document to pdf.

Easily extended to use more than one template.

1

I have used Java, Jasper Reports and iReport for a similar task. Hope that helps.

0

I wanted to post this as a comment as it's general advice, not a final solution, but it got slightly too long. I can of course help you to find your concrete solution in the comments.

To start off with the general rule: There is no single best solution. It mostly comes down to what you're comfortable to design the document with.

If you know LaTeX, that's an obvious possibility. LaTeX documents are beautiful, pretty much by definition.

If you don't know LaTeX and are comfortable with a few options less, but know org-mode, that's another nice way to go. org-mode documents can be converted to LaTeX which can the be converted to PDF, and they look really nice, too. pandoc might be able to do this directly but I didn't check that.

Of course, another possibility is to design your document in org-mode, let Emacs build the LaTeX code from it, and then use that LaTeX document to fill in the variables in every instance before you compile it to PDF. This way, you still don't have to design the document in LaTeX and don't have to build from org-mode documents but from LaTeX documents.

If you're okay with even less and don't want to do much, you can use markdown. Markdown offers by far the fewest options, both in design options and in content types. However, it is very easy to learn and use and you can design your document right here on StackExchange with a live preview. This should also give you an idea of its limits. There are nice documents in markdown but it usually involves a lot of tweaking it to the desired output format (in your case probably a multi-page DIN A4 PDF) which kind of defeats the purpose of it being easy to learn. Of course it can be a perfectly good solution for people who want to write a lot of – for example – documentation in markdown as it's super easy to write down, but in your case, the writing part is automated, so that advantage doesn't really count and the easy of using the language might not be worth the hassle of getting the documents to look like you want them to look.

If you're not comfortable with any of the above, you can use Jos' approach of designing your document in LibreOffice, unpacking it, changing its content.xml, packing it again, and then converting it to PDF. However, while this gives you a lot more options than markdown, it gives you a lot fewer options than LaTeX and getting up to the level of org-mode will be cumbersome. Actually, the whole thing seems to be hard to adapt quickly because it involves many steps when you want to change your document. It also seems to be the most error-prone. Not necessarily in the "a PDF coming to be, in the end" kind of way, but in the "it looks like it's supposed to" kind of way.

One important note, though: Don't try to switch out LibreOffice for Microsoft Office. While you may be able to find tools to convert those to PDF, it seems like a very bad idea to use their formats. The reason is that LibreOffice uses the OpenDocument Standard which uses standard zip files, therefore you can use any zip tool to extract and pack the file. Microsoft Office files, on the other hand, are merely based on zip. You will be able to extract them with normal zip tools but you won't be able to pack them using standard zip tools.

org-mode or LaTeX are particularly useful if you want a table of contents. It's possibly with the others, too, but more work. If you want a ton of options and are willing to learn if necessary, LaTeX definitely is the way to go.

You must log in to answer this question.