1

I am trying to insert a pdf on click of a button using the getContentasPDF method. The page which has to be rendered is the page I'm redirecting to. I'm pasting the partial code -

PageReference nextPage = new PageReference('/apex/CaseDetailPage?id='+caseDetail.Id+'&caseId='+caseDetail.id);
                string str = '/apex/CaseDetailPage?id='+caseDetail.Id+'&caseId='+caseDetail.id;
                generatePDF(str, caseDetail.id);
                nextPage.SetRedirect(true);
                nextPage.GetParameters().Put('message',sSuccessMessage );
                return nextPage;

Here is my generatePDF method -

    @future(callout = true)
       public static void generatePDF(String pageRef, ID caseID){
       PageReference nextPage = new PageReference(pageRef);
       Blob reportPdf;
                    try {
                            reportPdf = nextPage.getContentAsPDF();
                        }
                    catch (Exception e) {
                    reportPdf = Blob.valueOf(e.getMessage());
                    }

           ContentVersion conVer = new ContentVersion();
           conVer.ContentLocation = 'S'; // S specify this document is in SF, use E for external files
           conVer.PathOnClient = 'Case Submission.pdf'; // The files name, extension is very important here which will help the file in preview.
           conVer.Title = 'Case Submission'; // Display name of the files
           conVer.VersionData = reportPdf; // converting your binary string to Blob
           insert conVer;
}

When I try to view the saved files, I see that it is bit blurred and also style codes appear throughout the pdf. I tried using the attribute applyBodyTag="false" but it doesn't help much.

Top page of the pdf

Bottom page of the pdf

  • 6
    Nothing wrong with how you're calling this, far as I can tell, but odds are your page isn't written correctly. The PDF rendering engine is not nearly so forgiving as a real browser. – sfdcfox Jun 26 '18 at 17:40
  • 2
    It is also old, quirky, and has poor support for many commonplace web constructs (including some modern CSS). – David Reed Jun 26 '18 at 18:05
  • 1
    Thankfully Salesforce is working on a "Advanced PDF" render, which is built on Chromium. The Pilot was released on Summer 17, so we might not be too far away from a GA feature. - releasenotes.docs.salesforce.com/en-us/summer17/release-notes/… – Renato Oliveira Jun 26 '18 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.