+ Start a Discussion

Create custom PDF Viewer with Save and Email buttons

I am wondering how i would approach creating something similar to the 'PDF Viewer' that is inherent in salesforce when you view a quote with the buttons on the bottom while being able to view the PDF.  I have a PDF being generated from a button on the opportunity page but i want the option for the user to save that pdf and/or email that pdf out to a customer.  I assume if i am generating a pdf I cannot use any type of command button, so I would either have to have separate buttons on the opportunity page to do this each seperately, or which I would prefer, have a controller that displays a pdf document inside of a form and allow buttons to display as well.  
Would that be the correct approach? is there a way to display a pdf document inside of an apex form easily?
Chris Gary CloudPerformerChris Gary CloudPerformer
Sure it can be done. Essentially, you would use an IFrame on your VF Page.  Here is an example of what the VF Page would like:
<apex:page standardController="BLah__c" extensions="BlahCreatePDFController" showHeader="true" tabStyle="Blah__c">
    <apex:sectionHeader title="Blah PDF" subtitle="Blah PDF"/>
    <apex:form >
        <apex:pageBlock >
            <apex:pageMessages id="Messages"></apex:pageMessages>
            <apex:iframe height="400px" width="800px" src="data:application/pdf;base64,{!pdf}></apex:iframe>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton action="{!save}"  value="Save"/>
                <apex:commandButton action="{!cancel}"  value="Cancel" />

And here is what the controller would look like:
public class BlahCreatePDFController
    public String pdfurl{get;set;}
    private PageReference pdfPage;
    private Blob pdfBlob;

    public QuoteCreatePDFController(ApexPages.standardController sc)

    public override void initialize(blahId)
        blah = [select whatever__c from blah__c where id=:blahId];      

        pdfPage = Page.Blah_Template;        
        pdfBlob = pdfPage.getcontent();
    public String pdf {
        get {
            return EncodingUtil.Base64Encode(pdfBlob);

I hope this helps you.
Rajiv Penagonda 12Rajiv Penagonda 12
//Id of Quote record.
String QuoteID = '0Q0XXXXXXXXXXX';
//Id of quote Template
String templateID = '0EHXXXXXXXXX';
//This Url create the pdf for quote
String quoteUrl = '/quote/quoteTemplateDataViewer.apexp?id=';
quoteUrl +=QuoteID;
quoteUrl +='&headerHeight=190&footerHeight=188&summlid=';
quoteUrl +=templateID ;
quoteUrl +='#toolbar=1&navpanes=0&zoom=90';
//Create pdf content
PageReference pg = new PageReference(quoteUrl) ;
//Document object of quote which hold the quote pdf
QuoteDocument quotedoc = new QuoteDocument(); 
//Get the content of Pdf.
Blob b = pg.getContentAsPDF() ;
//content assign to document
quotedoc.Document = b;
//assign quote id where pdf should attach
quotedoc.QuoteId = QuoteID ;
//insert the quotdoc
 insert quotedoc;

here (https://jonyforce.wordpress.com/tag/create-pdf-by-apex-on-quote/)is the link to the original article (kudos to the original author). Hope this helps.
Rajiv Penagonda 12Rajiv Penagonda 12
Do note that you could also use the code till line #25 above to add PDF from within your apex to a SingleEmailMessage.