After nearly giving up and trying to recreate the standard template in VisualForce (ouch!), I somehow managed to find a WordPress article on how to create a PDF with the standard template (or a custom template) created through the UI. It is sort of hacky, but it beats having to recreate a template from scratch!
The article can be found here. I also came up with the pass-through idea from an Andy In The Cloud article found here.
Now one thing to note, I had to create a pass-through page to get this to work. Otherwise, I get the following:
And that's a real bad error. Apparently, you cannot create a Quote, then generate a PDF in the same method, even though it won't hit a limit (that I know of).
So I did the following:
First, I create a custom label and populated the value with the the Quote template Id. This can be obtained in the edit view of the template.
Once the label was created, I could access it via Apex, in an easier fashion.
Next, I'd create a pass-through page that can be hit, which will generate the PDF and redirect to the Quote record.
VisualForce
<apex:page controller="GenerateQuotePDFPassthroughController" showHeader="false" sidebar="false" standardStylesheets="false" action="{!GenerateQuotePDFAndRedirectToQuotePage}">
Generating Quote PDF and e-mailing to contact...
</apex:page>
Leveraging the action method for the page, I can create my PDF and redirect to my Quote, however, it'll be so fast, the user won't trully notice the process going through multiple pages.
The Controller Code
public with sharing class GenerateQuotePDFPassthroughController
{
private final Id QuoteId = (Id) ApexPages.currentPage().getParameters().get('quoteId');
private final Id QuotePDFTemplateId = (Id)Label.QuotePDFTemplate;
public PageReference GenerateQuotePDFAndRedirectToQuotePage()
{
GenerateQuoteDocument(QuoteId);
return new PageReference('/' + QuoteId);
}
private void GenerateQuoteDocument(Id quotesId)
{
String quoteURL = '/quote/quoteTemplateDataViewer.apexp?id=' + quotesId
+ '&headerHeight=190&footerHeight=188&summlid=' + QuotePDFTemplateId
+ '#toolbar=1&navpanes=0&zoom=90';
PageReference pageReferenceForPDF = new PageReference(quoteURL);
QuoteDocument newQuoteDocument = new QuoteDocument();
Blob renderedPDF = pageReferenceForPDF.getContentAsPDF();
newQuoteDocument.Document = renderedPDF;
newQuoteDocument.QuoteId = quotesId;
INSERT newQuoteDocument;
}
}
Now, to simply use this pass-through successfully, I just redirect to the pass-through after my Quote generation instead of to the record.
return new PageReference('/apex/GenerateQuotePDFPassthrough?quoteId=' + NewOrExistingQuote.Id);
It kind of bothers me that I had to resort in a pass-through page in order for this to work. If there are any other suggestions, I'd love to know.