Loading

Render images in PDF with Visualforce page intermittently fails

Julkaisupäivä: Oct 13, 2022
Kuvaus
Consider the following example of rendering google map as PDF via Visualforce.
  1. Create a remote site with URL: https://maps.googleapis.com . Make sure it is active.
  2. Create new VF page
  3. <apex:page renderAs="PDF">
       <apex:image value="https://maps.googleapis.com/maps/api/staticmap?size=640x440&maptype=hybrid"/>
    </apex:page>
  4. Previewing the VF page loads the google map in PDF.
Ratkaisu
  • You might be seeing an issue intermittently on few instances when rendering this image as PDF.
  • To understand further about this intermittent issue, perform a HTTP request via developer console,
HTTPRequest req=new HTTPRequest(); 
req.setEndpoint('https://maps.googleapis.com/maps/api/staticmap?size=640x440&maptype=hybrid'); 
req.setMethod('GET'); 

Http http = new Http(); 
HTTPResponse res = http.send(req); 
System.debug(res.getStatus()); 
  • Execute the code few times and on few tries you might notice System.HttpResponse[Status=Forbidden, StatusCode=403] The reason for this is, if a lot of users/organizations in a data center use google map by callout from salesforce, limit of free accesses might be exceeded. In such case, purchasing the API access for stable access is recommended.

Another important point to note here is:
Rendering the same page as HTML works but the above issue happens only when rendered as PDF.
            HTML rendering and PDF rendering are completely different. In PDF rendered page, sfdc app server downloads images which refers from HTML of Visualforce page. Then PDF which has embedded images is generated. Whereas, in HTML case, sfdc app server returns HTML of Visualforce to browser. Then, browser downloads images which refers from that HTML.

In PDF case, client's IP against google is salesforce external IP address.
In HTML case, it is client machine's IP address.

Limit of free accesses is based on client IP address. So, if a lot of users/organizations in a data center use google map, limit of free accesses might be exceeded easily.

Status Codes and Error Responses
Knowledge-artikkelin numero

000382266

 
Ladataan
Salesforce Help | Article