[SalesForce] Calling VisualForce page from WebService

In trying to create a VisualForce page that renders as a PDF and call that inside of a WebSerivce, I keep getting an HTML content inside the WebService that seems to be redirecting to the login page.

The WebService call is already authenticated and is called from a C# ASP.NET website. Is there a way to call the VisualForce page from the WebService?

What I have tried so far…

PageReference ref = Page.VisualForcePDFPage;
ref.getParameters().put('id', recordId);
Blob pdf = ref.getContent();

The page prints as PDF fine when directly accessed. Any help is appreciated.

Best Answer

"I keep getting an HTML content inside the WebService that seems to be redirecting to the login page."

This would suggest that you aren't correctly authenticating your request to Salesforce.

Based on your description in the question it sounds like you are essentially screen scrapping the contents of the Visualforce page as a PDF. Are you submitting the Session ID in the request cookie?


PageReference ref = Page.VisualForcePDFPage;
ref.getParameters().put('id', recordId);
Blob pdf = ref.getContent();

This seems distinct from your ASP.NET website calling into Salesforce as it is now Apex getting the Visualforce page content.

In this case you will want to use the PageReference.getContentAsPDF() method instead of .getContent() as it will ignore the apex:page renderAs attribute.


This might be a bit of a reach, but there have been reports of issues with PageReference.getContentAsPDF() due to a Salesforce update that adds CSRF protection on GET and POST requests. See Spring 14 problem with pages that share the same controller.

Are you on Spring 14? It is possible that the request is getting redirected for this reason.