I need to generate invoice pdf using the given template (I'm using kognoz`s invoicing app and due to certain circumstances it's functionality is not sufficient). The template is basically a bunch of data in different tables in DB. To do so I must gather the template data (it contains class variable references) as well as filler data and generate an apex page from it which will be then rendered as pdf.

The problem is that as I see it Salesforce does not support dynamic generation of VF pages. I've found Dynamic Visualforce components but it does not do the trick as I have variable amount of said components. I've also seen the thing called Tooling API however it seems way to complicated for such task.

Is there an easier way to dynamically create an VF page and render it as PDF?

  • I am pretty sure that you can build something like "general template" for your invoice pdf, and bunch of settings, stored on some object, starting from concrete field value and ending via blocks rendering. Then, using constructor on that page controller, you would be able set those variables. I have seen that approach for few times, and in those case, that would good decision. Another way -- 3rd party web server, your own, where you send pdf template data, and it returns pdf for you. That is more flexible, but harder to implement – kurunve May 19 '16 at 10:40

You can have a Visualforce page of this form:

<apex:page controller="MyController" showHeader="false"
        applyHtmlTag="false" readOnly="true" renderAs="pdf">
<apex:stylesheet value="{!$Resource.MyPdfCss}"/>

where Body__c can be a "Text Area (Long)" or a "Text Area (Rich)". Build the HTML content in that field in your Apex code from whatever SObjects you need and it will be output in PDF format.

  • Thanks for your answer! Through it I was able to discover that Blob.toPdf() accepts and evaluates HTML with CSS but does not render images, maybe you also know a solution for that? :) – Tsukihime May 19 '16 at 13:30
  • @Tsukihime Never used that Blob.toPdf so not aware of its limitations/benefits... – Keith C May 19 '16 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.