The PageReference.GetContentAsPDF method throws an internal Salesforce.com Error when there is a problem generating the page. i.e. when an exception is thrown in the controller.

This makes it very difficult to implement the error handling for this scenario.

I have included reproduction steps below.


<apex:page controller="GetContentAsPDFController">


public class GetContentAsPDFController 
    public GetContentAsPDFController() {
        throw new CustomException('Proof of concept');

    public class CustomException extends Exception {}

anonymous apex

PageReference vfPage = Page.GetContentAsPDFPOC;

This will show up as 2 transactions in the debug log.

  • The anonymous apex transaction will have an internal server error.
  • The transaction for generating the page will have the proof of concept error.

Does anyone know a workaround to this or any way we can get around the issue?

  • 1
    Hi Kieran, welcome to SFSE. Please take a moment to visit the help center, scroll through the tour, and read How to Ask. There's no question in your question. Please edit this post to actually ask something. – Adrian Larson Apr 20 '17 at 15:02
  • It's still hard to tell how exactly observed behavior differs from your expectation. Right now, you have outlined your observations, but to us those may match our expectations exactly. Also, no need to add signatures or thanks to the bottom of your posts! – Adrian Larson Apr 20 '17 at 15:11
  • See also XY Problem. What is it you are trying to accomplish at a high level? Right now you're hung up on Y but you haven't really shared any info about X. – Adrian Larson Apr 20 '17 at 15:13
  • You can trivially make the page "mostly" error-proof by surrounding all code with try-catch blocks that are guaranteed not to throw. I'd assume you are actually looking for a way for the code calling getContentAsPDF to catch the error rather than crash. – IllusiveBrian Apr 20 '17 at 15:18
  • Exactly that. We're in batch context producing documents for invoice records and having error handling from the caller would be massively cleaner and easier. The issue with error handling inside the controller is that we have a lot of methods where each would need their own try-catch block (even then there are uncatchable exceptions that could be hit). We would also need a way to communicate to the caller that the attachment returned wasn't a success so that it doesn't get inserted. – Kieran Maguire Apr 20 '17 at 15:34

If you add renderas="pdf" to the page, getContent will return a PDF of the page, and appears to still correctly throw an ExecutionException when the page throws an error (including uncatchable exceptions). If you are not able to change this attribute on the page itself, you can create a wrapper page and use apex:include to include your base page:


<apex:page renderAs="pdf">
    <apex:include pagename="GetContentAsPDFPOC" />

Developer Console:

PageReference vfPage = Page.GetContentAsPDFWrapper;
catch(Exception ex)
| improve this answer | |
  • Thank you! This worked, the page already had renderAs="pdf" so that should be fine. Good idea with the apex:include wrapper though. – Kieran Maguire Apr 20 '17 at 16:45

In my case, it was an issue with the permissions to the VisualForce page. Once the permissions were given the Internal Error was gone. Make sure your user has permissions to access the VisualForce page.

| improve this answer | |

Your Answer

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

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