I have a sample PDF which is having some placeholders, from which I need to generate PDF with Dynamic values in place of 'placeholders'. I need to do this in Java technologies. What should be the approach?
(Note: sample PDF having placeholders, Table structures, paragraphs, Stylesheets).
We, as iText team, has been receiving this exact question more and more everyday since many people/organisations want to generate custom filled PDF files with data they have. Many of them design forms in PDF and then they fill in the form programmatically. However there are problems with this solution: 1. You need license for a product where you will design the form 2. You need license (if applicable) for the PDF library to fill in the form 3. You (with your team) need to spend time to write the code where you fill in the form 4. You need to repeat all those each time you want to update your form etc. 5. Your form is most likely a static form, meaning that, for example, in case of an invoice you need to foresee how many rows you need to put in your form when designing it. What happens if users have more items than you foresee? You can use XFA forms but you probably know the cost of the designer and all other problems that come along with this deprecated technology.
We, as iText, bring a solution to all those problem just like we did in other tools we have. The solution is called iText DITO in which you design your PDF template in a WYSIWYG browser based editor. No coding is needed. If you want extra styling in your PDF, you can add CSS at any time. You can specify data-binding to elements in your design. Once you are done with your design, your project is basically ready to be deployed. If you need to generate tons of high-quality PDF files based on your template, you will use iText DITO back-end SDK to push your custom data (linking to data binding you have in your template) and your PDF files are created instantly.
Related
I am currently working at a project which generates contracts. The idea is that I put the data in a form and save it in a simple database.
So long, this was my favorite place to search for good ideas and simple solutions.
Now I am facing another problem and I don't know how I can solve that. I want to create a PDF and replace some placeholders with some data from my form.
One idea was, that I use an existing Word template with some bookmarks and replace them with the data from my form. Maybe there is a way to do that, and I am just too stupid to find it.
Another idea was, that I am using XML. Therefore, I thought I was clever and just converted the Word template to an PDF, so I am able to convert that PDF to an XML. Attached, you find the XML file. But now I need the XSL file - is there an easy way to create the XSL file?
Or maybe there is another simple solution to solve my problem.
In these attachments you find the PDF file, the Word template and the XML:
Thank you a lot :)
Using a template is a good idea - it makes some changes much quicker to make and then deploy. The comments above are focused on conversion, but don't forget you need to merge your data in (population) first.
If you can use Adobe tools, you can have a PDF template and use the Adobe tools to populate. This saves a "conversion" stage.
You mentioned using Word for templates. This means you to run through two stages of processing:
population - docx is a zipped set of XML files - so you can process them with your own code or using a library.
conversion - you need pdf, so you have to convert the docx to pdf. You also have to watch out for fonts at this stage (ie make sure they are available on your host).
The population stage you could do yourself since you are familiar with XML. But it is definitely complicated. The conversion needs to use a tool that is ideal for it. There are a few mentioned in the comments already.
There are some free/os and commercial tools that can do both parts:
docx4j
JOD Reports
Libre Office (using the Java Uno API) (I blogged this once - Java Convert Word to PDF with UNO)
Docmosis (please note I work for Docmosis)
I suggest starting with the simple example you have attached and prove you can both populate and convert that. Then switch to a more complicated example to see if you can do the other things that might be required (eg repeating or conditions or other logic) during the population stage.
I have hundreds of rich PDFs that need to be generated from my application, they have images and colorful content. I was looking to build a framework which support a template and data model and can take care of rest, so adding anew pdfs would be just adding a new template. In the past i have used free-marker to generate HTML and that print HTML to PDFs, are there any better recent solution to solve this problem?
There are various things you could do:
generate xml data, apply xslt transformation to style it, and convert
the html document to a pdf
code a small class that converts whatever data format you have to a pdf document (you would need to do all the layout through code)
create a template (using whatever design program you want) pdf document, insert form fields, and have iText fill the form (several of our customers go for this approach)
Keep in mind that JasperReports uses a proprietary format. Whereas the approaches I suggested use only open and well-established formats.
Take a look at JasperReports.
I want to create a table programatically as like this PDF FCController.pdf.
As you see columns are Date, Name, Category, Amount. And I want to add a datepicker to change the list.
Can I generate a PDF as I said? Is it possible by Itext?
You want to embed a .swf file inside a PDF as a RichMedia annotation as explained in chapter 16 of "iText in Action - Second Edition". The book had two examples that showed you how to do this:
FestivalCalendar1 resulting in festival_calendar1.pdf
FestivalCalendar2 resulting in festival_calendar2.pdf
Although these examples work to some extend, I would urge you not to use this technology. Steve Jobs refused to support Flash on Apple devices and that started the decline of Flash as a technology. Adobe introduced support for Flash as RichMedia in PDF in one of their extensions to ISO-32000-1, but I know from insiders at Adobe that they regretted this. In any case: this functionality is not going to make it into ISO-32000-2 (PDF 2.0), which means that:
no other PDF viewers than Adobe Reader will have any interest in supporting this functionality, and
Adobe may decide to discontinue support for this functionality.
The example you've shared is nice, and it's not impossible to create such a PDF, but you should reconsider: it's not a future-proof solution.
In my application the user can configure their own table layouts to display the data on the screen, by choosing which colums in which order are to be shown. Now I want to give my users the possibility to export these tables to PDF. The tables should fill the page width completely and the columns should adjust their size depending on the content in the table, like e.g. HTML tables do.
Can you recomment a library or toolchain for this?
I checked Apache FOP, but their fo:tables do not support automatic table layout. Creating a Jasper Report dynamically also doesn't seem to fit, because I have to specify the exact column width there. So, does anyone have an idea how to achive this?
Open Source solutions with commercially friendly licenses like Apache or LGPL preferred.
ANSWER: There are no current tools that allow what I hoped for, so I mark this question as resolved.
iText is probably what you are looking for http://itextpdf.com/. Version 2.1.7 is free to use under the GPL lisence, for more info see this question What is latest version of itext that is not AGPL?. It will work for you need, I would recommend using their code samples on their web page. Most are applicable to version 2.1.7 although it is currently on version 5.
I would recommend you take a look at Flying Saucer. It is an open source project that uses iText PDF as its core rendering engine but it allows you to define very dynamic xhtml files as your rendering medium.
Benefits of using FS+iText using iText by itself
Allows for a much faster dev cycle for changes or new products
Very easy to add in consistent and complex styles using a language built exactly for it (HTML + CSS)
You can use the same HTML code to render your PDF as you use to view it online (if using web application)
Can render graphics using java.awt.Graphics class onto the PDF, meaning you can integrate any graphics library with FS to paint objects like graphs onto your PDF.
Downsides:
While it does a good job at rendering modern css styles it is not perfect. For instance when I used it the project required the border-radius style to be usable. It wasnt so I had to implement it.
Rendering times and memory consumption are increased (Although I have found it to be quite fast and memory efficient)
More libraires in your project.
Heres an example from the FS website if your ineterested.
https://github.com/flyingsaucerproject/flyingsaucer/blob/master/flying-saucer-examples/src/main/java/PDFRender.java
I wrote a web app for generating PDF by filling data into a pre-saved PDF template, template edited by acrobat, with some text-fields. But the context of those text-fields seems in a different layer and cannot affect other existing words in template.
... But I want it affect the existing words and make them flow base on how many data fill into the text-fields.
The solution maybe use program to generate a whole PDF instead of using template. But the template changes really often in my case, I don't want waste a lot of time to adjust the position and format by coding...
Do anyone know how to use text-field with auto flow in a PDF template? just like a Word document.
PDF doesn't work like that. You need to generate the whole PDF.
Ah... but from what?
There are quite a few HTML->PDF converters out there. You could fill in your template HTML, and convert it that way.
You could develop your own input format (for your template), and write an app that reads it and builds a PDF.
The later is similar enough to HTML->PDF, that unless you can't find a converter that handles some PDF feature or other you need, I'd just go that route. There are LOTS of html->pdf apps out there. You can search SO, google, whatever. Lots.