function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SHaron ToSHaron To 

Render page in Salesforce1 as PDF after button click

Hello Everyone,

Within Salesforce1, I'm creating a checklist of items for the user to complete. I would like to provide an option for them to save their checklist as a pdf and email it to themselves but I'm having trouble figuring out how to convert the page to a pdf first. Is it possible to add this type of functionality to a Salesforce1 .app using Lightning?
Best Answer chosen by SHaron To
NagendraNagendra (Salesforce Developers) 
Hi Sharon,

Please find the below explanation matching your requirement criteria.

Visual Force Page:
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>
The above code displays the below 
User-added image

Apex Controller:
mplement 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.

For more information refer to the below link:
http://salesforce.stackexchange.com/questions/31768/rendering-a-vf-page-as-pdf-and-saving-it-as-an-attachement

Kindly mark this as the best answer if it helps you.

Best Regards,
Nagendra.P


 

All Answers

Scott Haleo 4Scott Haleo 4
Hi Sharon,

You have to create custom solution using visualforce and apex controller and using rerenderas then you can convert your page to pdf in Salesforce.

And also you can add method in your apex controller to send that pdf to an email to users.

Thanks

Scott Haleo
Hytechpro
NagendraNagendra (Salesforce Developers) 
Hi Sharon,

Please find the below explanation matching your requirement criteria.

Visual Force Page:
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>
The above code displays the below 
User-added image

Apex Controller:
mplement 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.

For more information refer to the below link:
http://salesforce.stackexchange.com/questions/31768/rendering-a-vf-page-as-pdf-and-saving-it-as-an-attachement

Kindly mark this as the best answer if it helps you.

Best Regards,
Nagendra.P


 
This was selected as the best answer