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
SFDC coderSFDC coder 

Page is not rendered in pdf format,although specified

This my code that sends an email with an attachment.The attachment is supposed to be a file in pdf format.
Error is: email is sent properly with an attachment,but the attachment is not in the pdf format
//Method that sends the pdf as attachment to the customer
    public PageReference sendEmailWithAttachment()
    {
        System.debug('**** curr id is'+currentId);
        //Getting the page to be rendered as pdf
        PageReference pdfPage=new PageReference('/apex/PropertyDetailPage?id='+currentId);
        //pdf.getParameters().put('id',);
        body=pdfPage.getContentAsPdf();
        
        //creating the email attachment
        Messaging.EmailFileAttachment attach=new Messaging.EmailFileAttachment();
        attach.setContentType('application/pdf'); 
        attach.setFileName('Property Details');
        attach.setBody(body);
        attach.setInline(false);
        
        //code to send the mail to the specified address
        Messaging.SingleEmailMessage email=new Messaging.SingleEmailMessage();
        String []toaddr=new String[]{cemail};
        email.setToAddresses(toaddr);
        email.setSubject('Acme Property Details');
        email.setHtmlBody('Here is the email you requested! Check the attachment!');
        email.setFileAttachments(new Messaging.EmailFileAttachment[] { attach });
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });
        
        return null;
    }

Waiting for reply...
Best Answer chosen by SFDC coder
pradeep naredlapradeep naredla
check this code and make sure that u have written ur code in this format.

PageReference pdf = Page.attachmentPDF;
    pdf.getParameters().put('id',(String)account.id);
    pdf.setRedirect(true);

All Answers

SFDC coderSFDC coder
this is my vf that i want to render as an pdf

<apex:page controller="DetailPageExt" >
<apex:form >
<apex:detail subject="{!recordid}" relatedList="false" inlineEdit="false" relatedListHover="false"/>
</apex:form>
</apex:page>

Is there any problem with the markups used?

Thanks in advance
pradeep naredlapradeep naredla
just add renderAs="pdf" attribute in <apex:page>...

SFDC coderSFDC coder
Hi pradeep,

Thanks for the reply....

The problem is that i cant add render as attribute as the page would then get displayed to the user in a pdf file format
pradeep naredlapradeep naredla
Hi neha,
     
    you said ur getting the mail have u got the attachment with the mail if yes in which format ur getting the attachment..

pradeep naredlapradeep naredla
hai neha,

just give ur file name in class code with a .pdf extension i.e Property details.pdf and try...

thanks,
pradeep.
SFDC coderSFDC coder
hi,

yes i am receiving the email and the format is of type windows file....Is there any problem with using the apex:define tag in my vf page?......coz i read somewhere that its an usafe component to be used while generating pdf
SFDC coderSFDC coder
yes...i tried with Property details.pdf as extension..It does work but the content is empty
pradeep naredlapradeep naredla
taht means in ur class code u didn't represented the id which has to be sent in pdf format. remove the comments for this line -----//pdf.getParameters().put('id',);-----
pradeep naredlapradeep naredla
check this code and make sure that u have written ur code in this format.

PageReference pdf = Page.attachmentPDF;
    pdf.getParameters().put('id',(String)account.id);
    pdf.setRedirect(true);
This was selected as the best answer
SFDC coderSFDC coder
hie it works now...but is there any way to avoid the buttons of the detail page to be rendered on the pdf file?
Also theres a field on my detail page wich is a formula field and it displays an image..but in the pdf the image is not displayed properly..is it that we can't display images in pdf's created from apex?
pradeep naredlapradeep naredla
hi,
 you cant restrict the data on detailpage while rendering if u want to do so just create a vf page for ur detail page and display what ever fields u want and render it to pdf. 
SFDC coderSFDC coder
hmm okk........

Thanks a lot For your help