8

I have different ways in my organization to generate my invoice pdf and attach it to my records. My PDF includes a logo stored in a public StaticResource. It works perfectly everywhere except when I generate my pdf from Apex REST, it doesn't display my logo :

enter image description here

Some Code :

@RestResource(urlMapping='/invoicereq/*')
global without sharing class InvoiceTemplateController
{
    @HttpGet
    global static void generateInvoiceAttachment()
    {
             ....
             //1. Generate & insert Invoice as an attachment
            PageReference pdf = new Pagereference('/apex/InvoicePDF?id=' + header.Id);
            Blob body = pdf.getContentAsPDF();

            Attachment attachment = new Attachment();
            attachment.Body = body;
            attachment.Name = 'Invoice ' + header.Name + '.pdf';
            attachment.IsPrivate = false;
            attachment.ParentId = header.Id;
            insert attachment;
            ...
    }
}

HTML

<img src="{!URLFOR($Resource.MyLogo)}" />

However if I use my logo as a Document with the same code, it works perfectly.

Any ideas why ?

3
  • And your static resource is public? – Boris Bachovski Aug 22 '14 at 0:45
  • 1
    Correct, my static resource is public. Thank you Bachovksi. – Cloud Ninja Aug 22 '14 at 0:46
  • Honestly, not too familiar with REST(yet) but saw another post with similar behavior who suggested using <apex:image url="{!URLFOR($Resource.MyLogo)}"/> vs. <img src> tag. – Jenny B Aug 24 '14 at 0:50
2

Explicitly set renderAs to PDF on the Visualforce page. This must be hardcoded, and not come from a variable.

    <apex:page controller="myController" renderAs="PDF" >
        <img src="{!URLFOR($Resource.MyLogo)}" />
    </apex:page>

In your controller, call getContent, not getContentAsPDF:

att.body = pdf.getContent();

According to the documentation, the content will be "as displayed to a user in a Web browser," which will include your image.

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.