0

I have a VF page within a Lightning component. When a user saves the form I need to generate a PDF of the new data. I created another VF page to render the data as a PDF and a Controller. What is the best method to invoke the PDF VF page and make sure it is after the new record is saved since I would like to attach the PDF to the record.
P

0

You could write a trigger (before insert and update) which calls a future method, which then creates the pdf from your Visualforce page and attaches it to the record. The trigger would allow you to automatically save the pdf. The apex future method is necessary because the PageReference.getContentAsPDF() method isn't allowed in a trigger.

I hope that helps. If not, try to make your question a bit more specific on where you're stuck.

| improve this answer | |
  • Thank you for the reply. I ill try to be more specific. I have a LC in a VF page where a user will input information. When they submit their information the data is saved within Salesforce. This part works well. I need to generate a PDF of the newly created data and attach it to the new record. So what I read was to create another VF page with the required formatting and an Apex Class to retrieve the data. I have both of these but was not sure how to call invoke the PDF VF page. Can I call the VP page from the LC controller? – Phuc Nov 8 '19 at 20:08
  • Just need to understand best approach to generate a pdf from the newly created record. P – Phuc Nov 8 '19 at 20:08
  • @Phuc You can call the apex class to create the pdf from a lightning component. If you only what to create the pdf when a user updates the record, then that is what I would do. If you also want the pdf to be created when a different process creates or updates the record (import of data, trigger, workflow rule, etc) then you'd want to use a trigger. A trigger will go off on any insert or update event. – Kasper Nov 8 '19 at 20:12
  • Just need to generate and attach the PDF when a record is initially submitted, will not generate another pdf if record is updated. So am I calling the Apex Class or the VF page? Will call be done from the component helper? Is this document anywhere or do you have a link to an example that I can review? Thanks – Phuc Nov 8 '19 at 20:22
  • You would call an apex class from your lightning component, and that apex class would create a PageReference of your visualforce page, and get it's content as a pdf. This pdf Blob, can then be used as the VersionData of a file (content version) This example is quite thorough, but unfortunately based on Attachments rather then files. blog.jeffdouglas.com/2010/07/14/… . Creating a file is very similar as creating an attachment, but instead of setting the ParentId to your original record, you set the FirstPublishLocationId. – Kasper Nov 8 '19 at 20:39

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