Why Your Salesforce Blob.toPDF() is Not Showing Images: Troubleshooting Guide
Generating PDFs via Apex using the Blob.toPDF() method is a powerful feature, but it often comes with a common frustration: the dreaded missing image. Whether you are an Admin configuring a document generator or a Developer building a custom solution, seeing a broken image icon in your final output is a roadblock. This guide breaks down exactly why this happens and how to fix it.
1. Verify Image File Size
One of the most common reasons images fail to render in Salesforce PDFs is their file size. The rendering engine used by Blob.toPDF() is highly sensitive to memory limits.
- The Rule of Thumb: Check if your image size is less than 15 KB to 20 KB.
- Why? Large images often fail to load within the timeout period of the rendering engine or exceed the heap limits allocated for the conversion process. If your image is high-resolution, try compressing it or using a lower-fidelity version specifically for the PDF.
2. Debug via HTML Rendering
If the image isn’t showing up, you need to determine if the issue is with the source HTML or the conversion process.
A great troubleshooting technique is to use System.debug() to print the raw HTML content right before it is passed to the Blob.toPDF() method.
Pro Tip: Take that debugged HTML output and save it locally as an
.htmlfile. Open it in your browser. If the image doesn’t show up in a standard browser, the issue isn’t Salesforce—it’s your HTML structure or the image path.
3. Validate the Image URL
The rendering engine requires a direct, accessible path to the image.
- Public Accessibility: Does the URL require a login session? If the image is stored in Salesforce Files or Documents, ensure the rendering engine has the permissions to access it.
- Absolute vs. Relative: Ensure you are using a fully qualified URL (e.g.,
https://yourdomain.file.force.com/image.png) rather than a relative path (e.g.,/servlet/servlet.FileDownload). - Protocol: Check if the URL is using
HTTPS. Inconsistent protocols can sometimes lead to mixed-content blocks during the PDF generation.
Summary Checklist
| Checkpoint | Action Item |
| Size | Ensure the image is < 20kb. |
| Integrity | Debug HTML content and test in a browser. |
| Source | Confirm the Image URL is valid and publicly reachable. |
Code Snippet: Best Practices & Recommendations
While implementing your solution, keep these considerations in mind based on the standard Salesforce PDF generation patterns:
Recommendations for Implementation
- Static Resources: Whenever possible, store images in Static Resources. They are cached and generally more reliable for PDF rendering than external URLs.
- Width/Height Attributes: Always define the
widthandheightin your<img>tags. This helps the rendering engine allocate space for the image before it even loads. - Avoid Base64: While Base64 encoding seems like a good workaround, it can significantly increase the size of your HTML string, potentially hitting Apex heap limits during the
Blob.toPDF()call. - Use System.debug() Wisely: When debugging, remember that very large HTML strings might be truncated in the Salesforce Debug Logs. Use a utility method to log in chunks if necessary.