Send Custom PDF as an Attachment

By: S F | Asked: 02/22/2024
ForumsCategory: General questionsSend Custom PDF as an Attachment
S F asked 10 months ago
Perhaps I am missing something. I understand you can click a download link to get a custom pdf based upon a view. However, is there a way to have the custom pdf automatically sent as an attachment in a response email? In the actions section, selecting the pdf attachment just sends the form entries as a pdf attachment, not the custom pdf. I can't seem to find where I would direct it to my custom pdf format and use that to send as an attachment. Any help would be much appreciated. Thanks.
2 Answers
S F answered 10 months ago

OK. I figured this out on my own in the event it's helpful to someone else... Step 1: Create a child theme. You can do this manually or use a plugin. I used: https://wordpress.org/plugins/child-theme-configurator/ Step 2: Place the following code in the functions.php file.
// Apply a Formidable Forms view for the attached PDF in email
add_filter( 'frm_pdfs_email_attachment_args', 'add_view_to_attached_pdf', 10, 2 );
function add_view_to_attached_pdf( $pdf_args, $args ) {
    if($args['form']->id ==5) { //change 5 to the ID of the form
         $pdf_args['view'] = 2; // ID of view.
       $pdf_args['id']   = $args['entry']->id; // Do this to show the detail view, otherwise, it shows the listing view.
       return $pdf_args;
   }
}
//Customize the PDF Name
add_filter( 'frm_pdfs_email_attachment_args', 'change_attached_pdf_file_name', 10, 2 );
function change_attached_pdf_file_name( $pdf_args, $args ) {
    // Use the actual numeric field ID here instead of 'field_id'
    $field_id = 1; // This is the ID of the field you want to include in the filename
    
    $field_value = ''; // Default to an empty string if the field value is not found
    if ( isset( $args['entry']->metas[$field_id] ) ) {
        $field_value = $args['entry']->metas[$field_id]; // Fetch the field value
    }
    // Append the field value to the filename
    $pdf_args['filename'] = 'Stuff-' . $field_value; // Change 'Stuff-' to anything you want
    return $pdf_args;
}  

Pretty simple actually.

Barbara Smith replied 8 months ago

Thanks for this, it's almost working for me. Not sure what I'm doing wrong. It now attaches the custom pdf to my emails but for some reason the pdf includes the information twice. It displays all the info and then at the end it displays it all again - exact duplicate. Very weird. Since you seem to be knowledgeable I wondered if you had any insights about why this might be happening?

Barbara Smith replied 8 months ago

Sorted!

S F answered 9 months ago
Nothing like forgetting how to do this..coming here to search for a possible answer...and realizing I solved this once before. LOL 
Barbara Smith replied 8 months ago

Hi - sorry to bug you but I'm hoping you can give me some guidance on how I get this code to work for multiple forms and views. I have more than one form and have built views for each of them but I don't know how to adjust the code so that they all work. I can only get it to work for one. Thanks in advance.

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right