Friday, February 16, 2018

Visualforce renderAs pdf in different page format

Over the years, I found that there are so many people struggling with rendering pdf using visualforce, specially when they are required to set a custom page size, page break, margin, adding custom styling.

Today I shall give an example on how to print a contact address on an envelope using a pdf generated from a pdf upon the click of a button from the page layout.

First of all, to be able to generate a pdf from a Visualforce page, you need to use the renderAs attribute on the apex:page tag. See code below:

The above code will generate a pdf but you will notice that the page size will be in an A4 or letter document format. We want to achieve an envelope page size as mentioned early. This can be easily be achieved using some css in the code.

The following attribute are required on the apex:page tag: applyHtmlTag="false" and showHeader="false". To specify a size for the rendered pdf, the following style need to be added to the page:

@page {
    size: 6.4in 4.5in;

The size is customizable depending on the desired print layout:
  • size: A4;
  • size: A5 landscape;
  • size: 9in 5in;
When the @page is not specified, a full document format pdf is created. 
When no size specified (default to A4)

Now with a size specified.
Envelope size (6.4in x 4.5in)
Here is the full code for the above output.

Additional and helpful resources:

Enjoy ;)

Monday, February 5, 2018

Disabling Lightning Component Caching while developing in Lightning

Most of the time, developers has to refresh their page multiple times until they see the changes on their Lightning component. This is due because Salesforce enable caching of its client-side component to optimize performance on production. 

This can be bypass easily by disabling browser cache from Salesforce configuration. 
Here are the steps for disabling Browser cache:

Go to Setup > Security > Session Settings
Scroll to the section Caching
Uncheck Enable secure and persistent browser caching to improve performance

browser caching salesforce
Session Settings (Browser caching)
Note: THIS option should be enabled in production environment to avoid performance issues.

Additional resources: