• Resolved Amit

    (@w3helps)


    Hello,

    I wanted to generate a PDF file while user submit form.
    For example, I made a form and there have some fields like name, email etc. I added email action to send the details data after form submission. Now I wanted to generate a pdf where name, email will add as an attachment with the user email.

    Thanks in advance!

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Thanks for the feedback!

    You can use a PHP PDF library in order to generate a PDF on-the-fly and then push it as an e-mail attachment using the acfe/form/submit/email_args hook (See documentation).

    Here are the most popular PHP PDF libraries:

    Hope it helps!

    Have a nice day!

    Regards.

    Thread Starter Amit

    (@w3helps)

    I used this codes and it does not attachment with email. It just generate a PDF and show after form submission.
    How I attach the new generated pdf with email attachment?

    // Generate PDF using fpdf 
    require_once("invoice-attachment/fpdf.php");
    function generate_pdf_invoice_fpdf(){
    	$pdf = new FPDF();
    	$pdf->AddPage();
    	$pdf->SetFont('Arial','B',16);
    	$pdf->Cell(40,10,'€400 Paid from your credit card');
    	$pdf->Output();
    
    }
    
    add_filter('acfe/form/submit/email_args/action=to_customer', 'invoice_attachment_email_args', 10, 3);
    function invoice_attachment_email_args($args, $form, $action){
        
    
         $args = array(
    		'from'          => '[email protected]',
         'reply_to'      => '[email protected]',
              'to'            => '[email protected]',
              'cc'            => '[email protected]',
         'bcc'           => '[email protected]',
         'subject'       => 'Subject',
         'content'       => 'Content',
            'headers'       => array(
                'From: [email protected]',
                'Reply-to: [email protected]',
                'Content-Type: text/html',
                'charset=UTF-8'
            ),
              'attachments'   => array(
    			generate_pdf_invoice_fpdf()
             )
         );    
        // return
        return $args; 
    }
    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello!

    As shown in the documentation, the attachments argument require a list of files absolute paths, not the files output.

    Which means you have to generate your PDF and save it as a file somewhere, at least temporarily, and use that file path. Usage example:

    add_filter('acfe/form/submit/email_args/action=my-email', 'my_form_email_args', 10, 3);
    function my_form_email_args($args, $form, $action){
    
        // add attachments
        $args['attachments'] = array(
            '/var/www/domain.com/wp-content/uploads/file.pdf'
        );
    
        // return
        return $args;
        
    }
    

    See wp_mail() function, which is used behind the scene to send the email, for more details.

    Hope it helps!

    Have a nice day!

    Regards.

    Thread Starter Amit

    (@w3helps)

    Thank you for your reply!

    I used this code for generating pdf-

    
    function generate_pdf_invoice_fpdf()
    {
    	$toEmail = get_field('registration_email', acfe_form_get_action('post', 'ID'));
    	$reg_sender_name = get_field('registration_name', acfe_form_get_action('post', 'ID'));
    	$reg_event_title = get_field('registration_event_title', acfe_form_get_action('post', 'ID'));
    
    	$pdf = new FPDF();
    	$pdf->AddPage();
    	$pdf->SetFont('Arial', 'B', 16);
    	$pdf->Cell(40, 10, '€400 Paid from your credit card' . $toEmail . $reg_sender_name . $reg_event_title);
    	$pdf->Output('invoice.pdf');
    }
    

    Do you have any suggestion, how I will save / update a custom post field when the pdf generated. Also, if you could suggest me how to add that generated pdf with the attachment.

    I am tring this code to save or update custom field

    
    add_filter('acfe/form/submit/save-pdf', 'save_as_pdf', 10, 4);
    function save_as_pdf($form, $action)
    {
    
    	$post = acfe_form_get_action('post');
    
    	update_field('registration_invoice_pdf', '123', acfe_form_get_action('post', 'ID'));
    }
    
    • This reply was modified 2 years, 6 months ago by Amit.
    • This reply was modified 2 years, 6 months ago by Yui. Reason: formatting
    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    I would recommend to check the FPDF documentation before using the Output() method in order to understand how it works.

    As explained there:

    string Output([string dest [, string name [, boolean isUTF8]]])

    dest

    Destination where to send the document. It can be one of the following:
    I: send the file inline to the browser. The PDF viewer is used if available.
    D: send to the browser and force a file download with the name given by name.
    F: save to a local file with the name given by name (may include a path).
    S: return the document as a string.

    The default value is I.

    Which means you have to use it like this in order to save a file:

    $pdf->Output('F', '/folder/filename.pdf');
    

    You can use wp_get_upload_dir() (See documentation) in order to retrieve your current WP uploads folder information. Example:

    $upload_dir = wp_get_upload_dir();
    $upload_path = $upload_dir['basedir'];
    
    $pdf->Output('F', $upload_path . '/filename.pdf');
    

    I would recommend to only save the filename as post meta, so your metadata can be ported on an another WP install, since the file path can then be built again with wp_get_upload_dir(). If you’re using a Custom Action of ACFE Form:

    add_action('acfe/form/submit/save-pdf', 'save_as_pdf', 10, 2);
    function save_as_pdf($form, $action){
    
        // get the previously generated post id
        $post_id = acfe_form_get_action('post', 'ID');
    
        // get fields
        $email = get_field('registration_email', $post_id);
        $name = get_field('registration_name', $post_id);
        $title = get_field('registration_event_title', $post_id);
    
        // get wp upload path
        $upload_dir = wp_get_upload_dir();
        $upload_path = $upload_dir['basedir'];
        
        // generate filename & path
        $filename = 'invoice.pdf';
        $full_path = $upload_path . '/' . $filename;
    
        // $full_path = /var/domain.com/wp-content/uploads/invoice.pdf
    
        // generate pdf
        $pdf = new FPDF();
        // ...
        
        // save pdf file
        $pdf->Output('F', $full_path);
    
        // save only the filename as post meta
        update_field('registration_invoice_pdf', $filename, $post_id);
    
        // the full path with filename can then be retrieved again using
        // the same method with wp_get_upload_dir() (see above)
    
    }
    

    You can totally do all that script directly in the acfe/form/submit/email_args/action=my-email hook, in order to use $full_path as an attachment for your email, and also save the filename as post meta if needed.

    Hope it helps!

    Have a nice day!

    Regards.

    Thread Starter Amit

    (@w3helps)

    Thank you for your quick response.

    I want to add the pdf with the attachment. my code is like this

    $args = array(
    		'from'          => '[email protected]',
    		'reply_to'      => '[email protected]',
    		'to'            => $toEmail,
    		'cc'            => '[email protected]',
    		'bcc'           => '[email protected]',
    		'subject'       => 'Subject',
    		'content'       => 'Thank you ' . $reg_sender_name . ', for registering ' . $reg_event_title . $invoice_url  . ' with sharp reflections. You can download your invoice (attachment here' . $invoice_url . ')',
    		'headers'       => array(
    			'From: [email protected]',
    			'Reply-to: [email protected]',
    			'Content-Type: text/html',
    			'charset=UTF-8'
    		),
    
    		'attachments'   => array(
    			$invoice_url
    		)
    	);

    update_field(‘registration_invoice_pdf’, $filename, $post_id);

    $invoice_url = get_field(‘registration_invoice_pdf’, $post_id);

    Unfortunately, the pdf does not show or attach with the email.

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Please read again carefully my previous answers. You have to set the attachments with full path (ie: /var/domain.com/uploads/file.pdf), not just the filename, as explained here.

    If you only saved the post meta with the filename (without the path), you have to build the full path using wp_get_upload_dir() as explained here.

    I would recommend to log what is in your code/variables before using it, if you’re not sure what is actually in your $invoice_url, you can do error_log($invoice_url). Read more about WP Debug here.

    Regards.

    Thread Starter Amit

    (@w3helps)

    Thank you Konrad.
    It works excellent.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘PDF generate after form submission’ is closed to new replies.