Skip to main content

Some PDF tricks on Visualforce: Landscape, A4, page number and more

The beauty of Visualforce is simplicity. Remember the shock you received when you were told the entire page renders as PDF if you just add renderAs=PDF to the Page tag.

For those who thought I spoke alien language right now, here is the trick, to render a page as PDF, we add a simple attribute to the <apex: page> tag

<apex: page renderAs='pdf'>
This will render the entire page as PDF.

Now, say we need to add some extra features to the PDF. Like a page number in the footer or we need to render the page in landscape mode. Faced with this problem, I put on my Indiana Jones hat and went hunting for it in the vast hay-sack of the internet (read: googled extensively). Imagine my happiness when i found a big big page with many big big examples to solve the problem. The document I am referring to is from W3C, paged Box media.

Long story short, I now possess the ultimate secret of rendering the page in any format I want. So here are few tricks I learned from the page. To perform this tricks simply add the following code to your visualforce PDF page and see it bend to your command.

1. Rendering page as landscape

@page {
/* Landscape orientation */
size:landscape;
}

2. Rending page in different size

@page {
  size: 8.5in 11in;/* width height */
}

3. Rendering page in landscape A4

@page {
  size: A4 landscape;
}
4. Adding stuff in footer of the page

@bottom-right {
content: Some content;
}

5. Adding page number to the page

@page {
  size: 8.5in 11in;
  margin: 10%;

  @top-left {
    content: "Hamlet";
  }
  @top-right {
    content: "Page " counter(page);
  }
}


The other variations in box-model are top-left, top-center, top-right, bottom-left, bottom-right, bottom-center.

Hope this helps creating those awesome looking PDFs in visualforce. More information on many more tricks in a lot of detail can be collected from the mother ship itself.

Some more information is also given by the Quinton Wall on force.com blog.

Toodles for now,
Hope it helps,
Sid

P.s. Those who could early book into DF11, I envy you.

Comments

  1. awesome! great post sid.

    ReplyDelete
  2. Also remember that if you do
    <apex:outputPanel layout="none" render="{!someboolean}">

    And have another output panel inside it will get wrapped with a span even if you put layout="none"

    </apex:outputPanel>


    Make PDF's error if you are putting that span in say before TR

    ReplyDelete
  3. @David
    Thanks for the info, yes it will completely screw the layout. However if you wish to render PDF, always make sure you use table instead of divs and output panel.

    ReplyDelete
  4. forcing page breaks doesn't work, unfortunately.

    ReplyDelete
  5. Try the following to force a page break after the the div:

    < div style="page-break-after: always;" >
    your content here
    < /div>

    ReplyDelete
  6. Has anyone found a way to add todays date to the bottom of the PDF?

    I've tried:

    @bottom-right {
    content: "Date " (TODAY()};

    and several other variations on the same syntax, to no avail.

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. ok so I am clearly missing something whenever I add this code to the page it just appears as text at the top before the apex page block

    ReplyDelete
    Replies
    1. @Robin,
      are you sure you are putting it inside page{}

      Delete
    2. Could it be because I am using the account standard controller?

      If I add it to a style section then nothing happens at all.

      Delete
    3. It should be inside the style and then inside @page {
      @bottom-right{
      // Write code here
      }
      }

      observe it should be inside the @page.

      Delete
  9. Hi all..
    Can any one help to render the visualforce chart on pdf?

    ReplyDelete
    Replies
    1. Why don't you post it on the boards? I am sure someone will help you out there

      Delete
  10. If your stylesheet will not render add this setting to your apex:page applyhtmltag="false"

    ReplyDelete

Post a Comment

Popular Post

The unofficial guide to become a Certified Salesforce Administrator (ADM 201)

In my attempt at maximum certifications in 60 days, I completed Salesforce Certified Administrator exam on February 11th 2013 So you have decided to ramp up your career and take certifications in your hand. Good choice. It is also likely that this is the first time you have heard of Salesforce, certification and since your company has a vision of you completing the certification you have decided to do it. At this stage it is likely that, You have done extensive googling. You have received countless brain-dumps. And you have received plenty of advise from different types of users which ranges from Admin certification is easier than making coffee to Admin certification is tougher than building a rocket-ship to fly off to the moon. The purpose of this guide is to give you a clear understanding of what to expect when you are expected to become Certified Salesforce Administrator. To bring sense to all the things you have seen so far and to clearly explain what to do and what

The Basics of writing a Apex Trigger

One of the most important and common asked question on Forums and everywhere is how do I write a trigger. Coding in Apex Trigger is like going to a dentist for a root canal, you keep dreading the moment until you realize it is actually not going to hurt you. If you plan to write an Apex Trigger this quick guide will help you doing so. The first and foremost rule in writing a trigger is to remember the oldest suggestion given to the most comprehensive Hitchhikers Guide to Galaxy, ' Don't Panic. ' Writing a trigger is not a rocket science, in-fact we should thank the team at Salesforce and ForceDotCom for making everything so simple, that anyone can do it. Enough of talk, lets code. So you want to write a trigger. Let us have a glimpse of what we are going to build. The problem statement is as follows Problem:  When the User is entering the Opportunity, check for the Opportunity Amount. If the Opportunity Amount is greater than 50,000. Mark the Parent Account as