function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
dntfeedthemikeydntfeedthemikey 

Email Template PDF Attachment Issue

We have several visualforce email templates where there is an attachment that is a visualforce component. Most of the time the emails go out and the attachment is rendered correctly. Sometimes the attachment is not rendering correctly and it appears that the style sheet is not being considered. 

 

Example email template attachment tag:
<messaging:attachment renderAs="PDF" filename="{!RelatedTo.name}.pdf">
<c:component invId="{!RelatedTo.id}" />
</messaging:attachment>


Example Component:

<apex:component access="global" controller="ControllerClass">
<apex:attribute name="Id" type="string" required="false" assignTo="{!ObjId}" description="The id of the item being rendered."/>
<apex:stylesheet value="{!URLFOR($Resource.StyleSheetExtension)}" />
<apex:stylesheet value="{!URLFOR($Resource.PDFStyleSheet)}" />
<apex:componentBody >

html code here

</apex:componentBody>

 

FYI - All resource files are cache control public. 

 

Thanks

dntfeedthemikeydntfeedthemikey

After doing some more research on the issue, I've find another clue. Let me see if I can example this better.

 

Have an Email Template that includes an attachment (messaging:attachment), which in turn refernces a component. This component has two style sheet tags, that point to the resource files.  Now if the hit the button "Send Test And Verify Merge Fields" inside the Email Template detail (Visualforce Email Template), the email I get from doing that looks great. The style were added to the PDF and everything looks as design. But if you send an email threw an apex api using Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });, the styles are not a apply to the PDF. 

 

Anyone have a clue on what I'm doing wrong? Thanks 

 

Code:

 

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

EmailTemplate tEmail = [select id, Name, Subject, body from EmailTemplate where name = 'EmailTemplateName'];

 

mail.setToAddresses('Test@test.com');
mail.setReplyTo('Reply@test.com');
mail.setSenderDisplayName('Name');
mail.setUseSignature(false);

mail.setTemplateId(tEmail.id);

mail.setTargetObjectId(object.Id);
mail.setSaveAsActivity(false);
mail.setWhatId(object.id);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });