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.

  • Did you try getContentAsPDF() instead of getContent() ? – Cloud Ninja Feb 16 '14 at 23:56
  • getContentAsPDF() returns blank. – Anand Feb 17 '14 at 7:48

"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.

| improve this answer | |
  • 1. As mentioned in the question, I am correctly authentication my request to Salesforce from the C# ASP.NET app. Inside the Webservice, I am rendering the content through the VF page as a PDF. That is not rendering correctly. getContentAsPDF() returns blank and Blob.toPDF() throws an exception due to HTML content probably not containing some valid chars. – Anand Feb 17 '14 at 7:50
  • Are you able to provide an example HTTP request that the C# code is sending? I can only go off my experience with why Salesforce would redirect you to the login page. – Daniel Ballinger Feb 17 '14 at 8:48
  • I have a WebService called IntegrationService through which I generated the WSDL to use in the C# project. The WebService has a getOpportunity() method which returns the opportunity and that works successfully. There is a saveEmail() method that takes the email content and converts it to a PDF through the VisualForce page and adds that as an attachment to the Opportunity. The VF page is returning a redirect to the login inside the WebService. Otherwise the WebService call works fine. – Anand Feb 17 '14 at 12:36
  • If VisualForcePDFPage works fine when using a browser but redirects when using PageReference.getContentAsPDF() I'd suggest capturing the debug logs. Hopefully they will indicate why Salesforce is redirecting the request. – Daniel Ballinger Feb 17 '14 at 20:49
  • I have changed my code to sanitize the HTML content and perform Blob.toPDF() which is working currently. But still the getContent() or getContentAsPDF() is not working. Also, the debug logs do not contain information regarding why SF is redirecting. – Anand Feb 18 '14 at 7:27

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.