4

I have an external image that I'm trying to render in a PDF. It works fine on the associated Visualforce page, but the PDF is a no go. I have authorized the external site that stores the photo in Remote Site Settings, and I have made sure the Field Level Security for the Object is set properly.

Here is the page with the working image - https://events.spacesift.com/apex/booking?id=a0041000007WTzM

Here is the rendered PDF without the working image - https://events.spacesift.com/apex/bookingpdf?id=a0041000007WTzMAAW

  • 1
    You might want to change some personal details on those records if you're sharing it publicly. Also it'd be nice to update the question with snippets from the source code related to the image part. – Boris Bachovski Mar 3 '17 at 0:33
  • 2
    If I open the image in new window, the base URL changes from https://www.dropbox.com to https://dl.dropboxusercontent.com. Try adding it to remote site as well. – Raul Mar 3 '17 at 0:35
  • 1
    @RahulSharma I'd be inclined to post that as the answer. The 302 response from dropbox.com/s/nj2j1nidz7zna1w/umbel-logo.png?raw=1 to the new URL at dl.dropboxusercontent.com/content_link/… will need to be handled by the remote site settings. I suspect the PDF rendering engine might not follow the redirect at all, so you would need to use the final URL. – Daniel Ballinger Mar 3 '17 at 0:49
  • I've added dl.dropboxusercontent.com and same issue occurs – csakon Mar 3 '17 at 1:00
  • @csakon Can you host the file elsewhere to see if it something specific to the dropbox redirect causing the issue? – Daniel Ballinger Mar 3 '17 at 1:02
1

I believe the issue is not related to Salesforce, issue is from Dropbox, Dropbox images can't be rendered on PDF content. They have updated some of the policies regarding content distribution and sharing, and after that I believe they are only providing Preview for the hosted images content, not the direct image content like for hosting purpose. see below thread for more info on that:
Pictures in a pdf file are not showing up

you can reach out to Dropbox for further inquiries.

1

I had experience the same issue and for me image was currupted and it was showing if page is render normally. Can you try with a different image on the same URL.

  • If you want to critique or ask something from the OP, kindly post comments. This post of yours doesn't present a solution, it is more of a comment. Use add comment link right at the end of the question to comment. – Mahmood Mar 3 '17 at 5:47
0

use Data URI Schema problem will resolve

  • 1
    This is not an answer to the question. Kindly update your answer or remove it. – Mahmood Mar 3 '17 at 5:46
0

As an alternate solution you can try to capture the logo and save it to aatchment/Static Resource as png file. Second in the Visulforce page we refer this image to include in the VF.

Dropbox do a 302 re-direct the image url from dropbox.com to dl.dropboxusercontent.com. So when you get this status code you need to make a second request using the value you find in the location header of the first response. In this way you can avoid the hard coding of exact URL.

I tried to implement the above steps with image as an attachment and able to get the image in pdf.

Controller

public class PDFWithImage {
    public Attachment logo {get;set;}
    public PDFWithImage(){
    }
    public PageReference printPDF(){
        HttpRequest req = new HttpRequest();
        string logoImageURL = 'https://www.dropbox.com/s/nj2j1nidz7zna1w/umbel-logo.png?raw=1';
        req.setEndpoint(logoImageURL);
        req.setMethod('GET');
        req.setHeader('Content-Type', 'image/png');
        req.setCompressed(true);
        req.setTimeout(60000);
        HttpResponse res = new Http().send(req);
        while (res.getStatusCode() == 302) {
            req.setEndpoint(res.getHeader('Location'));
            res = new Http().send(req);
        } 
        String responseValue = res.getBody(); 
        System.debug('Response Status code for File: '+res.getStatusCode());
        system.debug('Response Body for File: ' + responseValue);         
        Blob image = res.getBodyAsBlob();        
        logo = new Attachment();
        logo.Name = 'mylogo.png';
        logo.Body = image;
        logo.contentType = 'image/png';
        logo.ParentId = '001i000000gDY4X';
        insert logo;        
        PageReference pdfPage = new PageReference('/apex/pdfwithimage');
        return pdfPage;
    }
}

VF Page

<apex:page controller="PDFWithImage" renderAs="pdf" >
    <apex:image url="{!URLFOR($Action.Attachment.Download, logo.id)}"/>
</apex:page>

Screenshot

enter image description here

  • Would be better to store it as a static resource if it isn't dynamic. – Raul Mar 3 '17 at 6:39
  • @RahulSharma Nice Idea!! mentioned in the answer as well. – Saroj Bera Mar 3 '17 at 6:48

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.