I have a VisualForce page that contains an image that I want to render as a PDF. This rendering is done as part of a controller call, which will take the rendered PDF blob and attach it as a File to an Object. If I go through these pages in regular Salesforce, it works just fine rendering the PDF to include the image. If I do it as part of a community, the image does not render (though the rest of the PDF is generated), and the data is not queried for (the PDF form is empty). The image is hosted on Salesforce as a File attachment, and is available at the time of PDF rendering.
Controller snippet:
// Get PDF...
PageReference pdf = Page.CustomReport;
pdf.getParameters().put('id', Id);
pdf.getParameters().put('imageUrl', prepareUrl(Id));
Blob body = pdf.getContent();
// Attach Blob to Object...
VF Page snippet:
<apex:image id="customImage" value="{!imageUrl}" alt="Custom Image" />
I've verified that the URL is indeed populated, and indeed references the image in question, which I can access with a GET request in my browser. My understanding is that the PDF rendering engine may be run in a different context than the original controller, but it leaves me at a loss explaining why this works in regular Salesforce but not the community.
Best Answer
Well, I had sort of answered my own question, but haven't found any documentation to support this.
The PDF rendering indeed happens as its own call with its own context. In regular Salesforce, this continues to execute as the user who made the initial controller call. In the community, this doesn't happen as the authenticated user, but rather the unauthenticated
Community Guest User
. The URL in question for the image is protected, and requires an authenticated user to access it. The underlying data is also protected, hence no values populating the PDF. This is borne out both in the Debug Logs, and that I was also able to render an image from an unprotected URL (e.g.https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png
).It seems, therefore, the options are:
I would have liked to have seen some documentation in Salesforce stating the difference in behavior between regular use and Community, but so far I haven't had any luck.