Generate a PDF from salesforce.com

One of the questions I get asked very frequently is, "How do I generate a PDF from within salesforce.com?".

Well I'm not sure if everyone knows this, but in fact, Visualforce has a built in PDF renderer.


So this means that you can have buttons on your pages in salesforce.com, which when clicked, generate a PDF that you've designed.

This particular feature is available to all users of salesforce.com, from Group and Professional to Enterprise and Unlimited.

But if you do have Enterprise or Unlimited, you can even use the PDF & Email API's to automatically send emails with a PDF attachment.

Once you've built the class, it is then accessible via your standard workflow rules, so you can use it in many scenarios.

We have deployed this for a number of clients, and we use it ourselves internally.

To achieve this in Group or Professional Edition, try creating a new Visual Force Page, Call it "PDF" and use the following code:

< standardcontroller="Opportunity" renderas="pdf" standardstylesheets="false" sidebar="false" showheader="false">
Hey Hey!
< / apex:page>

Then you are done - well kind of! You can now view this new page at:
https://emea.salesforce.com/apex/PDF

But it's a bit useless right now, so why not decorate it with data from salesforce.com. You can place any standard HTML code within the center of the page block and it will render it nicely.

Then create a custom button on the Opportunity Page which links to:
https://emea.salesforce.com/apex/PDF&id={!Opportunity.Id}
Probably best to have this custom button open in a new screen.

So if you want to pull across any fields from the salesforce.com opportunity, just place references within your HTML Code:

E.g.
{!Opportunity.Name} - Brings back the opportunity name
{!Opportunity.Account__r.Name} - Goes to the related account, and brings back account name.

The fist part of this parameter references the object you are currently on, so in this case, we defined this VF Page to use the Opportunity Controller, the next part is a field name, or it can be a relationship, so you can traverse through more objects.

So hope this gives you a bit of an idea, get someone to design up your forms in HTML, replace all the dynamic data with {!} dot syntax, and add a button and VF page to render it as a PDF.

A couple of caveats, this uses the standard controller, which means we can access any fields on the opportunity or on related objects. In some cases, you may want to pull data from multiple objects at once, in this case you'd use a Controller Extension Class or Custom Controller class, alas only available in EE/UE.

Also, I've written a lot now, so I'll put another post later on how you can use the Email API to trigger emails with PDF attachments.

6 comments:

Anonymous said...

Thanks, ideal for a beginner, can you recommend any more useful resource like this for someone just starting out with Salesforce?

Thanks again!

Ian said...

Hey just wanted to see if you ever posted about what a class may look like that generates a PDF via apex then attach it to an email.

Anonymous said...

So I kept getting errors with this line of code:
< standardcontroller="Opportunity" renderas="pdf" standardstylesheets="false" sidebar="false" showheader="false">


to make it work, you have to put it in with the apex:page:

< apex:page standardcontroller="Opportunity" renderas="pdf" standardstylesheets="false" sidebar="false" showheader="false">

and remember to at __c to the end of custom objects.

Anonymous said...

also, the link should be
https://emea.salesforce.com/apex/PDF?id={!Opportunity.Id}

with ? instead of &

Unknown said...

This articles seem really useful however as a non-developer I missing some last little tips. e.g. how can I render a "view" as pdf. what html would I place where? And is the pdf specific to that view?

habertanderson said...

You  have got some great posts in your blog. Keep up with the good work.
Salesforce Melbourne

Post a Comment