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
Ryan DRyan D 

render as pdf insert attachment

Hi there.  I have been working with the VisualForce "renderAs="pdf"" with my page.  The client has some requirements I need to meet.  I need to add the pdf to the Opportunity record in the notes and attachments related list.

I have found the following code:

 

Apexpages.currentPage().getHeaders().put('content-disposition', 'attachment; filename='+pdfName);

 This code is great because as soon as the user clicks on the link to my pdf rendered page it just opens the save dialog.  However, in addition to this I would like to insert the pdf as an attachment to the record it is related to. 

I found some code and got some help from the forum already about inserting the page as a new attachment but when the file attaches to the record I get an error from Adobe reader when I try to open it: 

ERROR: Adobe Reader could not open 'FILENAME.pdf' because it is either not a supported file type or because the file has been damaged (for, example, it was sent as an email attachment and wasn't correctly decoded).

My question is this: does the line of code above create an attachment, and if so, can't I just insert that into my related record instead of making a new attachment and using getContent() etc.?

 

Thanks!

 

Best Answer chosen by Admin (Salesforce Developers) 
ForceMantis (Amit Jain)ForceMantis (Amit Jain)

Best way is to create two pages, once that render as pdf and another that use getcontentaspdf on previous page and attach it into attachment.