10

How can I render a VisualForce Page as PDF and have a button on the page that saves this PDF as an attachment to a record?

I want to have a button on my object detail page, 'Generate PDF' which will open a VisualForce Page for preview. On the Preview page I need a button to Save or Save and Email this PDF.

Edit - I use the professional edition + API enabled, so I cannot write a controller but maybe replace that with ajax toolkit?

Is that doable?

9

From the phrasing of your question, it sounds as though you may have a bit of a misunderstanding of how PDF "previews" work, either that or the terminology you're using is in reference to visualforce page you're starting the preview/render process from rather than the actual page that's being previewed/rendered as a PDF.

PDF's are typically rendered from a VisualForce page template of some kind using merge fields along with HTML markup. I've created these for clients who use the Professional Edition as you do. In the header of the page, will be something like the following:

<apex:page standardController="Opportunity" showHeader="false" renderas="pdf">

Because of the renderas="pdf", this page will only display as a PDF "preview". You might have another version of the page that could display as VisualForce, but most clients I've worked with don't. Instead, if they don't use a custom controller, they create a Button of some kind that populates the page and the fields within it, after which it render as a PDF.

You can't have any active content on your PDF page from which to save it or perform some other action. You'll need to do that with your button code or in some other manner that might be new to me. So, you may want to have a button for "preview" and another button for "Save PDF" if you don't want to save it manually plus a third button "Send PDF" to send it as an email attachment if you're trying to automate the steps into a single button press behaviors. I rarely do anything with button code, so can't help you out with that part.

I strongly suspect there may be a way to do this using Publisher Actions without the need of a button. But, I don't know if Actions have been fully integrated into Professional Edition or not. Hope this has been helpful to you though in better understanding the task you're attempting to accomplish and how to approach it.

Edit

Here's a couple of references for you that you might find useful. First is a nifty technique from @MastOr How do I convert a visualforce page to a PDF? that allows you to render a PDF and save it at the same time using a specific file name.

You can call the action method below (GeneratePDF) from a command button to save to a User's folder:

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;
}

I don't know if this will serve your purposes or not with the PE version, but here's a link to a post titled Generate PDF Attachment on User Editable Email?. It discusses generating PDF email attachments that users can send from an object by embedding the Visualforce code for the PDF within the tag for a Visualforce Email Template. The solution involved replacing the tag with a tag.

Finally, if you want to use a button, here's some code used for a Quote Button. I'd think you could readily modify it to suit your purposes.

<input value="Create PDF"  class="btn" title="Create PDF" name="createpdf" onclick="null
var pdfOverlay = QuotePDFPreview.quotePDFObjs['quotePDFOverlay'];
pdfOverlay.dialog.buttonContents = '&lt;
input value=\&quot;
Save to Quote\&quot;  
class=\&quot;btn\&quot; 
name=\&quot;save\&quot;
onclick=\&quot;QuotePDFPreview.getQuotePDFObject(\'quotePDFOverlay\').savePDF(\'0\',\'0\');\&quot;  
title=\&quot;Save to Quote\&quot; 
type=\&quot;button\&quot; /&gt;&lt;
input value=\&quot;Save and Email Quote\&quot;  
class=\&quot;btn\&quot; 
name=\&quot;saveAndEmail\&quot;
onclick=\&quot;QuotePDFPreview.getQuotePDFObject(\'quotePDFOverlay\').savePDF(\'1\');\&quot; 
title=\&quot;Save and Email Quote\&quot; 
type=\&quot;button\&quot; /&gt;&lt;
input value=\&quot;Cancel\&quot;  
class=\&quot;btn\&quot; 
name=\&quot;cancel\&quot; onclick=\&quot;QuotePDFPreview.getQuotePDFObject(\'quotePDFOverlay\').close();\&quot;   
title=\&quot;Cancel\&quot; 
type=\&quot;button\&quot; /&gt;';
pdfOverlay.summlid = '0EHE0000000PCKI';
pdfOverlay.setSavable(true);
pdfOverlay.setContents('/quote/quoteTemplateDataViewer.apexp?id=0Q0E0000000TNLo','/quote/quoteTemplateHeaderData.apexp?id=0Q0E0000000TNLo');
pdfOverlay.display();
" type="button" />

I'm fairly confident that with what I've provided, you should be able to come up with something that will work in a PE org.

| improve this answer | |
  • Then if I want a save PDF button which will save the PDF as an attachment who do I do it? – Dedo Apr 5 '14 at 7:46
  • 1
    See my edited reply. – crmprogdev Apr 5 '14 at 16:49
  • Thanks so much. Since I cannot use APEX code, I will not be able to use the GeneratePDF method, but I will lean the code for the button and try to do something with it. – Dedo Apr 5 '14 at 21:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.