+ Start a Discussion
Robby SRobby S 

Options for Saving VF Page as PDF

Hi everyone,


I have a requirement for a custom Visualforce page to be downloaded and saved as a PDF when a user clicks a button on the page.


I'm aware of the renderAs apex:page attribute and how you can control rendering the page as a PDF by adding a parameter to the url - however, when we try this option, the PDF generated via this route is not very attractive and doesn't display the formatting on the page (in fact, it actually displays fragments of the HTML code).


We would also like to avoid one of the "link to PDF" options - where you send a link to a 3rd party website and the PDF is generated and returned.


Has anyone implemented similar requirements? Does anyone know of any options that could deliver what we're needing?


Thanks in advance for the help.



Prashant TiwariPrashant Tiwari

You can call below action method (GeneratePDF) from command button:


public pagereference GeneratePDF(){
pagereference Pg = Page.VFPagetoPDF_onBtnClick;
Blob pdf1 = pg.getcontentAsPdf();
Document d = new Document();
d.FolderId = UserInfo.getUserId();
d.Body = Pdf1;
d.Name = 'pdf_file.pdf';
d.ContentType = 'application/pdf';
d.Type = 'pdf';
insert d;
return null;


This will save the visualforce page as a PDF document in User's folder. To view , you can navigate to documents and view it.



Prashant Tiwari

Robby SRobby S

Thanks for the reply!


That's a great option - especially being able to save it in the user's documents folder. We had not thought about doing anything like that programatically.


The only issue is that the PDF created still doesn't capture any of the formatting we have. The page structure and all formatting is done with HTML tags and CSS. The CSS is actually what's being displayed (just typed up at the top of the PDF) instead of the HTML. So the PDF created is the same as if we did renderAs="PDF".


Also, if the user enters any data on the form, in any of the text boxes or radio buttons, etc., the values aren't captured in the PDF.


Do you know of any way to preserve the formatting and also capture the data entered on the form in the PDF?




Prashant TiwariPrashant Tiwari

Hi Robby,


The PDF renderer's support of CSS Is limited and some trial and error is nearly always required to get the results you want.


You can quickly go through the below link to get more info  about RenderAs Limitations:



  • Standard components that aren’t easily formatted for print, or form elements like inputs, buttons, or any component that requires JavaScript to be formatted, shouldn’t be used. This includes, but isn’t limited to, any component that requires a form element.






***mark it as solved if the solution worked as it might help others !!



Prashant Tiwari