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
SoleesSolees 

How to use Salesforce PDF Viewer with custom PDF page ?

Hi friends, do you know the code (Apex or Javacript) to open the Salesforce PDF Viewer but with a PDF i created manually? Not the templates you can create with salesforce.

 

cheers

Best Answer chosen by Admin (Salesforce Developers) 
SoleesSolees

So after 2 days of searching here is my result,

PLEASE LET ME KNOW IF IT HELPS YOU

FEEL FREE TO POST AN UPDATED CODE

 

You can do this with ORGs that can have Apex Classes (sorry Professional i tried so much but i could not make it work, not the Save Pdf File part).

 

So follow this steps and you will get something cool.

 

 

 

1) Create your Apex VF page with the Standard Controller "Quote" and RenderAs="pdf".  Mine is called "Prueba_PDF"

 

2) Create a WebService as follow (could be a Controller too) :

 

global class AttachmentGenerator {
   webService static String AttachPDFToQuote(string Id) {
        string retRes = '';
        try
        {
         PageReference pageRef = new PageReference('/apex/Prueba_PDF?Id='+Id);
         Blob content = pageRef.getContent();
         QuoteDocument doc = new QuoteDocument(Document = content, QuoteId = Id);
         insert doc;

         retRes='SUCCESS';
         return retRes;
       } catch(exception ex) {
           System.debug('--- Error ----------'+ ex);
           retRes= ex.getMessage();
           return retRes;
       }
   }
}

 

3) Create a javascript file mine is called "QuoteCustomWS.js", here you are going to call the WebService:

 

function SaveQuotePDF(a) {
    try {
        var res = sforce.apex.execute("AttachmentGenerator","AttachPDFToQuote",{Id : a});
        if(res=='SUCCESS')
            window.location.reload();
        else
            alert('Error in attaching file ----'+ res);
    } catch(er) {
        alert(er);
    }
}

 

4) Upload this File as a Static Resource and then click view and copy the URL without the namespace.  For example "/resource/1350609029999/QuoteCustomWS" .

 

5) The last ,but not least, very important thing, create a Custom Button in Quotes that executes javascript (OnClick Javscript):

 

{!REQUIRESCRIPT("/soap/ajax/24.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/24.0/apex.js")}
{!REQUIRESCRIPT("/resource/1350609029999/QuoteCustomWS")}

var pdfOverlay = QuotePDFPreview.quotePDFObjs['quotePDFOverlay'];
pdfOverlay.dialog.buttonContents = '<input value=\"Guardar Presupuesto\"  class=\"btn\" name=\"save\" onclick=\"SaveQuotePDF(\'{!Quote.Id}\');\" title=\"Guardar en Presupuesto\" type=\"button\" /><input value=\"Cancelar\"  class=\"btn\" name=\"cancel\" onclick=\"QuotePDFPreview.getQuotePDFObject(\'quotePDFOverlay\').close();\" title=\"Cancelar\" type=\"button\" />';

pdfOverlay.setSavable(true);
pdfOverlay.setPDFContents('/apex/Prueba_PDF?id={!Quote.Id}',null,null);
pdfOverlay.display();

 

6) Add the button to your Quote Page Format.

 

 

 

 

Important things to consider:

a) The Code in the Custom Button is use to View the File in the "Salesforce PDF Viewer" but it will not be complete without the "Guardar Presupuesto" button that calls the WebService using the "QuoteCustomWS.js".  You need all the code to make this work.

b) If you want different Templates you could just add a Picklist Custom Field to your Page Format and add the names of them.  Then grab this info in the Custom Button and change a little but the "QuoteCustomWS.js" to grab the name of the Apex VF Page to save. Easy.

 

I tried so hard with Professional edition and i did not make it work, not the Save Pdf file Part thats actually on the Server side.  If someone could just read the Embed PDF with Javascript let me know, thats the part that i could not do.  If we could complete this, we could all use this great feature as we please.

 

Cheers and thanks for reading.

 

 

 

All Answers

SoleesSolees

So after 2 days of searching here is my result,

PLEASE LET ME KNOW IF IT HELPS YOU

FEEL FREE TO POST AN UPDATED CODE

 

You can do this with ORGs that can have Apex Classes (sorry Professional i tried so much but i could not make it work, not the Save Pdf File part).

 

So follow this steps and you will get something cool.

 

 

 

1) Create your Apex VF page with the Standard Controller "Quote" and RenderAs="pdf".  Mine is called "Prueba_PDF"

 

2) Create a WebService as follow (could be a Controller too) :

 

global class AttachmentGenerator {
   webService static String AttachPDFToQuote(string Id) {
        string retRes = '';
        try
        {
         PageReference pageRef = new PageReference('/apex/Prueba_PDF?Id='+Id);
         Blob content = pageRef.getContent();
         QuoteDocument doc = new QuoteDocument(Document = content, QuoteId = Id);
         insert doc;

         retRes='SUCCESS';
         return retRes;
       } catch(exception ex) {
           System.debug('--- Error ----------'+ ex);
           retRes= ex.getMessage();
           return retRes;
       }
   }
}

 

3) Create a javascript file mine is called "QuoteCustomWS.js", here you are going to call the WebService:

 

function SaveQuotePDF(a) {
    try {
        var res = sforce.apex.execute("AttachmentGenerator","AttachPDFToQuote",{Id : a});
        if(res=='SUCCESS')
            window.location.reload();
        else
            alert('Error in attaching file ----'+ res);
    } catch(er) {
        alert(er);
    }
}

 

4) Upload this File as a Static Resource and then click view and copy the URL without the namespace.  For example "/resource/1350609029999/QuoteCustomWS" .

 

5) The last ,but not least, very important thing, create a Custom Button in Quotes that executes javascript (OnClick Javscript):

 

{!REQUIRESCRIPT("/soap/ajax/24.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/24.0/apex.js")}
{!REQUIRESCRIPT("/resource/1350609029999/QuoteCustomWS")}

var pdfOverlay = QuotePDFPreview.quotePDFObjs['quotePDFOverlay'];
pdfOverlay.dialog.buttonContents = '<input value=\"Guardar Presupuesto\"  class=\"btn\" name=\"save\" onclick=\"SaveQuotePDF(\'{!Quote.Id}\');\" title=\"Guardar en Presupuesto\" type=\"button\" /><input value=\"Cancelar\"  class=\"btn\" name=\"cancel\" onclick=\"QuotePDFPreview.getQuotePDFObject(\'quotePDFOverlay\').close();\" title=\"Cancelar\" type=\"button\" />';

pdfOverlay.setSavable(true);
pdfOverlay.setPDFContents('/apex/Prueba_PDF?id={!Quote.Id}',null,null);
pdfOverlay.display();

 

6) Add the button to your Quote Page Format.

 

 

 

 

Important things to consider:

a) The Code in the Custom Button is use to View the File in the "Salesforce PDF Viewer" but it will not be complete without the "Guardar Presupuesto" button that calls the WebService using the "QuoteCustomWS.js".  You need all the code to make this work.

b) If you want different Templates you could just add a Picklist Custom Field to your Page Format and add the names of them.  Then grab this info in the Custom Button and change a little but the "QuoteCustomWS.js" to grab the name of the Apex VF Page to save. Easy.

 

I tried so hard with Professional edition and i did not make it work, not the Save Pdf file Part thats actually on the Server side.  If someone could just read the Embed PDF with Javascript let me know, thats the part that i could not do.  If we could complete this, we could all use this great feature as we please.

 

Cheers and thanks for reading.

 

 

 

This was selected as the best answer
Ade12Ade12

I have followed this example and the code is working fine however I want the toolbar to show at the top the way it does for the standard Quote PDF.

Does anyone have any idea how to do this?

 

Also the cancel button is not working for me, when I press it nothing happens.

Anshul GoyalAnshul Goyal
Can we also have the functionality of send and save Quote as in standard sf overlay does on Quote .
In standard sf we can see a button "Save and Email Quote" and i want to use same in the above custom overlay.
Anybody has the idea or implemented then please share....
Avijeet SinghAvijeet Singh

Yes! you can.

 

You might want to do some additions in the JS code shared above in step 5:-

create a Custom Button in Quotes that executes javascript (OnClick Javscript):

{!REQUIRESCRIPT("/soap/ajax/24.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/24.0/apex.js")}
{!REQUIRESCRIPT("/resource/1350609029999/QuoteCustomWS")}

var pdfOverlay = QuotePDFPreview.quotePDFObjs['quotePDFOverlay'];
pdfOverlay.dialog.buttonContents = '<input value=\"Guardar Presupuesto\"  class=\"btn\" name=\"save\" onclick=\"SaveQuotePDF(\'{!Quote.Id}\');\" title=\"Guardar en Presupuesto\" type=\"button\" /><input value=\"Cancelar\"  class=\"btn\" name=\"cancel\" onclick=\"QuotePDFPreview.getQuotePDFObject(\'quotePDFOverlay\').close();\" title=\"Cancelar\" type=\"button\" />';

pdfOverlay.setSavable(true);
pdfOverlay.setPDFContents('/apex/Prueba_PDF?id={!Quote.Id}',null,null);
pdfOverlay.display();


The code in bold shows a button used to "Cancel" and close the pdf viewer. Similarly a code can be written to perform a "Save" and "Send Email" button.

Isa Delgado 9Isa Delgado 9

Hello,

This is possible to use in custom object?

Suneel Mishra 4Suneel Mishra 4
HI Solees,

If in case i want to use standard quote templates in this, then?? 
Bailey RuddBailey Rudd
Avijeet,

Can you explain in more detail the web service callout? Why is this necessary? Any more instructions on how to implement that would be great. 
Rushank Parmar 19Rushank Parmar 19
Hi,

I have similar requirement but need to achieve in lightning.

I have an button created in Lightning component. On click of it, I need to open the attachment attached to file related list of the record. How the solution mentioned by you will work
Átila CastroÁtila Castro
Any solution for lightning experience?