1

Is it possible to save a visualforce page with renderas='pdf' to the files folder so I can attacht the files to an email in lightning? Something like

files f = new files( FolderId = UserInfo.getUserId(), Body = pdfBlob, Name = 'pdf_file.pdf', ContentType = 'application/pdf', Type = 'pdf'); insert f;
2

By using getContentAsPDF, you can get a visualforce's page content as a pdf, regardless of the pages renderAs attribute.

You won't be able to perform DML on the file object directly, since its a label for the ContentDocument & ContentVersion objects. You'll need to create one of those records instead.

Id someID = '000000000000000';

PageReference ref = new PageReference('/somepage?id=' + someID);

ContentVersion cont = new ContentVersion();

cont.Title = 'Title for this contentVersion';
cont.PathOnClient = 'file.pdf';
cont.VersionData = ref.getContentAsPdf();
cont.Origin = 'H';

insert cont;    
| improve this answer | |
  • could you show an example of someUrl how to get the visualforcepage with corresponding id? – Thomas Apr 30 '18 at 20:37
  • That's pretty basic stuff. I've updated the answer with some mock code. Actual implementation depends on the page and the parameters it expects, your mileage may vary. – battery.cord Apr 30 '18 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.