Search
  • Tony Nelson

Create a "Render A Visualforce Page as PDF" Button

In this article, I would like to introduce how to download a Visualforce PDF.


Problems With Using renderAs=”pdf”

If you try to render a Visualforce page as a PDF document, usually the PDF document is displayed in the browser. Place the “Render PDF” button on the Account page. If you click the button, the PDF page appears in the browser.


・Setting a Custom Button


However, in this manner, the below problems may occur.

  1. When saving a file, you need to right-click on the page to display the save PDF every time. If you have many pages to save, it can be a hassle.

  2. The file name is “[Visualforce page name].pdf” by default. Users manually need to assign a file name.


Create a “Render a Visualforce Page as PDF” Button

By implementing the below method, a file name is automatically assigned by Apex and the PDF document can be downloaded without opening a new tab in the browser.


・PDF (Visualforce)


Specify the Apex Class as a Controller.


<apex:page standardController="Account" extensions="AccountPDFController" renderAs="pdf" applyBodyTag="false">
 <head>
 <style type="text/css">
 body { font-family: Arial Unicode MS; }
 </style>
 </head>
 <h1>Account Information</h1>
 <apex:panelGrid columns="2" border="1" cellspacing="0" cellPadding="5" width="100%">
 <apex:outputText value="{!$ObjectType.Account.fields.Name.label}" />
 <apex:outputText value="{!Account.Name}" />
 <apex:outputText value="{!$ObjectType.Account.fields.AccountNumber.label}" />
 <apex:outputText value="{!Account.AccountNumber}" />
 <apex:outputText value="{!$ObjectType.Account.fields.WebSite.label}" />
 <apex:outputText value="{!Account.WebSite}" />
 <apex:outputText value="…" />
 <apex:outputText value="…" />
 </apex:panelGrid>
</apex:page>

・PDF Controller (Apex)

Implement the below code in the constructor of the class.


Apexpages.currentPage().getHeaders().put(‘content-disposition’, ‘attachment; filename=[file name]’);


※ Only half-width characters can be used for a file name.

public class AccountPDFController {
public AccountPDFController(ApexPages.StandardController controller) {
Account acc = (Account)controller.getRecord();
String accNum = acc.AccountNumber;
//Assign "Account_[Ac].pdf" as a file name
String fileName = 'Account_' + accNum + '.pdf';
Apexpages.currentPage().getHeaders().put('content-disposition', 'attachment; filename=' + fileName);}}

・Account Page Layout


Create a custom button, “Download PDF” and add it to the page layout.


・Setting Up a Custom Button


The Javascript code is written as follows:


top.location.href='[PDFURL]';


Execution Results


By clicking the button, you can display the “Download a PDF file” dialog without switching screens.


This time we mainly focused on a custom button, but of course, this method can be applied to a custom link as well.

Please try and take advantages of the useful feature.

1 view

© 2019 TerraSky. All Rights Reserved, TerraSky, Inc.