Oh my god. It's full of code!

Visualforce Force Download of PDF or Other Content

Hey everyone,

This next trick is one I’ve kind been keeping under my hat since it’s a nice polishing touch for some of my contest entries, but I figured I should probably share it with the world now (information must be free, etc). So we all know we can create Visualforce pages that render as PDF documents. It’s a pretty cool feature especially because business people love PDF files more than I love being a cynical ass (which is like… a lot). Though the one little annoyance is that normally when you create that PDF visualforce page the user is brought to it to view it where they then can download it. Many times they simply want to download it and attach it to an email or something, the viewing isn’t required and is generally just an extra few wasted seconds waiting for it to load so they can hit file->save as. I have found/built a nifty way to force download of the file using a combination of Apex and some tricky DOM manipulation. As an added bonus I’ll show you how to conditionally render the page as a PDF based on a URL param. Here we go!

The first thing we’ll need of course is our Visualforce page, we’ll keep it simple for this example. So here is our visualforce page

<apex:page controller="forceDownloadPDF" renderAs="{!renderAs}">
<h2>PDF Download example</h2>

<p>This is some content that could be displayed as a PDF or a regular web page depedning on the URL params. The valid URL params are as follows</p>
<table width="100%" cellpadding="5" cellspacing="5">
        <td>String with a boolean value</td>
        <td>if passed in as a true the page will be rendered as a PDF. Otherwise displayed as HTML</td>
        <td>String with a boolean value</td>
        <td>If true the user will be prompted to download the contents of the page. Suggested to be paired with pdf=true</td>
        <td>String (valid file name)</td>
        <td>'My PDF Report [todays date].pdf'</td>
        <td>A name for the file. Only used if force_download=true</td>


And now our controller

public class forceDownloadPDF {

    public string renderAs{get;set;}

    public forceDownloadPDF()

        //figure out if the user passed in the pdf url variable and if it is set to true.
        if(ApexPages.currentPage().getParameters().get('pdf') != null && ApexPages.currentPage().getParameters().get('pdf') == 'true') 
            //if so, we are rendering this thing as a pdf. If there were other renderas options that were valid we could consider allowing the user to pass
            //in the actual renderAs type in the url, but as it stands the only options are pdf and null so no reason to allow the user to pass that in directly.
            renderAs = 'pdf';

            //figure out if we are forcing download or not.
            if(ApexPages.currentPage().getParameters().get('force_download') != null && ApexPages.currentPage().getParameters().get('force_download') == 'true') 
                //setup a default file name
                string fileName = 'My PDF Report '+date.today()+'.pdf';

                //we can even get more created and allow the user to pass in a filename via the URL so it can be customized further
                if(apexPages.currentPage().getParameters().get('filename') != null)
                    fileName = apexPages.currentPage().getParameters().get('filename') +'.pdf';
                //here is were the magic happens. We have to set the content disposition as attachment.
                Apexpages.currentPage().getHeaders().put('content-disposition', 'attachemnt; filename='+fileName);

As noted in the comments the real secret here is setting the content disposition use the Apex getHeaders method. Now you are saying,

‘But Kenji if I call that page from a link it still opens in a  new window it just forces the user to download the file. That’s not much better!’

Oh ye of little faith, of course I got you covered. You think I’d leave you with a half done solution like that? Hell no. Lets take this mutha to the next level. Here is what we are going to do. Using a custom button with onClick javascript we are going to create an iframe with the source set as that visualofrce page (with the force_download=true param) and inject it into the DOM. When the frame loads (which will have 0 width, and height so it’s not visible) that code still runs prompting the user to download the file. They are non the wiser that a frame got injected, all they see is a happy little download dialog prompt. So go create a custom button on an object that you want to prompt the user to download your file from. Make it a detail page button (you could do a list button to, but that’s a topic for another day). Make it onClick javascript. Then slap this code in there.

ifrm = document.createElement("IFRAME"); 
ifrm.setAttribute("src", "/apex/yourPage?pdf=true&force_download=true&filename=My Happy File"); 
ifrm.style.width = 0+"px"; 
ifrm.style.height = 0+"px"; 

Of course replace the ‘yourPage’ with the name of your visualforce page. The filename of course can be changed to be details from the record, or whatever you like. Now when the user clicks that button the javascript creates an invisible iframe and injects it into the DOM. Once it loads the user is prompted to download the file. Pretty slick eh?

Hope you dig it. Catch ya next time.

11 responses

  1. Reblogged this on Sutoprise Avenue, A SutoCom Source.

    August 29, 2013 at 2:18 pm

  2. Reblogged this on Sutoprise Avenue, A SutoCom Source.

    August 29, 2013 at 2:35 pm

  3. Reblogged this on Sutoprise Avenue, A SutoCom Source.

    September 5, 2013 at 9:45 am

  4. ForceDev

    Kenji you’re truely awesome!!! Learnt so much from your blogs!! Great work

    September 5, 2013 at 10:30 am

  5. Jen

    Can this work for a Word Doc. I have have a VF Page created that renders a Word Doc. Everything works great, but after you open it and go to save it the Save As Type is Web Page. I have searched high and low, and tried everything. I am beginning to think it is not possible.

    March 1, 2014 at 3:50 pm

  6. Everything is very open with a precise clarification of
    the issues. It was definitely informative. Your website is very helpful.
    Many thanks for sharing!

    May 19, 2014 at 7:52 pm

  7. Howdy! I simply want to give you a huge thumbs up for the excellent information you have
    got here on this post. I’ll be returning to your website for more soon.

    May 19, 2014 at 11:07 pm

  8. Hello to every body, it’s my first pay a visit of this website; this web site contains
    awesome and truly fine data in favor of readers.

    June 9, 2014 at 12:15 pm

  9. Wow! At last I got a webpage from where I can actually obtain valuable
    information concerning my study and knowledge.

    June 27, 2014 at 7:04 pm

  10. Its such as you learn my mind! You appear to grasp so much about
    this, like you wrote the book in it or something.
    I think that you just could do with some percent to power the message home a bit, however instead of that, this is fantastic blog.
    An excellent read. I’ll certainly be back.

    July 4, 2014 at 1:58 am

  11. have u done for the list view button click?

    January 4, 2016 at 1:32 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s