[SalesForce] How to you populate a PDF form and display it within SFDC

Let's start with an existing PDF that has fillable form fields.

The end goal is to populate those fields with SFDC data and display the PDF in the browser.

What I've tried so far:

I've created an XFDF file that will populate the PDF. I can generate that file using apex. However, I'm not sure how to get from an XFDF file to a PDF file in the browser. It sounds like 3rd party tools or libraries are generally used to do that and I don't see an easy solution in SFDC.

Right now I'm thinking I'm going to have to recreate the existing PDF in VisualForce. I was hoping to avoid that but it doesn't sound like there is an easier solution out there.

Best Answer

It isn't necessary to go through a VF page in order to create a PDF. Instead, you can write your own custom PDF controller that's called from a button on an existing page (one that has queryable links to all the data objects you need) and then have the completed PDF output in a new window. If you're looking to automatically save it as a file, there are some autosave methods you might be able to use in conjunction with what I'm describing, but I've never attempted or had the need to do something along those lines with a PDF Controller.

Since you say you're able to create XFDF files using APEX, I'd expect that writing the VisualForce controller shouldn't be that difficult for you. The way I've done these in the past is to create an HTML template with custom CSS. Then it became a case of populating fields and using repeats wherever tabular data existed. There are also some "tricks" here and there in the CSS that can help with pagination if that's something you need to accomplish. Also, some people lay out the complete document in the controller, then send it to the page while others simply send it to the page. There's a tutorial in the SF documentation on writing a PDF controller using the former method. Unfortunately, I don't recall at the moment which workbook it's in.

Related Topic