• Resolved omarsekkkk

    (@omarsekkkk)


    Hello

    im trying to generate a pdf after form submit, here s the code and the form json code : https://pastebin.com/F67jzBYr

    when i submit the form the pdf is generated successfully and download is started immediatly, the pdf open automatically in a new window, but when i return to the form i got the waiting message “soumission en cours….” it keeps rolling.

    how can i fix that please, the doc is a bit confusing. im using : forminator_form_after_handle_submit

    i would also like to add a download link to the success message, and also add the pdf to the mail sent after submission, iv tried several ways to do that (download link, and send email attachment) but doesnt seem to work, can you help please put me in the right path.

    thanks a lot.

    PS: you can try with ID 230 on the step 3 page in the field “link to the page you need help with” click search then submit the form.

    The page I need help with: [log in to see the link]

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @omarsekkkk

    I hope you’re well today!

    1. The first thing is how you are calling this hook.

    In your code you’re using form ID that is supposed to be passed via the hook to your custom code. The form ID is the first of the parameters passed from that hook to your callback function so since you already have the function defined like this

    function generate_pdf_on_form_submit($form_id)

    you also need to make the hook pass that parameter to it; change this line

    add_action('forminator_form_after_handle_submit', 'generate_pdf_on_form_submit');

    to this

    add_action('forminator_form_after_handle_submit', 'generate_pdf_on_form_submit', 11, 1);

    The 11 is a priority value (default is 10 so setting it to 11 makes the code to be executed a bit “later” in order in case there was any other code using the same hook) and 1 is the number of values (according to defined in hook definition in plugin) to be passed over to your function.

    2. The second thing is this line

    // Kill
    exit(0);

    What’s the reason of using it? When you call it you are essentially “killing” execution of code and you do this inside an action hook so you just stop execution of entire script of which this hook is a part of. It should not be needed there actually.

    —-

    But aside from that, why not use built-in integration with e2pdf plugin instead? It doesn’t require any coding and can also work (though that depends on the PDF features you need) with the free version of e2pdf plugin, allowing you to decide what fields to use, nicely format them, attach them to e-mail notifications if needed and so on.

    Take a look here, please:

    https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#e2pdf

    Best regards,
    Adam

    Thread Starter omarsekkkk

    (@omarsekkkk)

    Hi

    thanks for your response, my client want a custom code, he dosnt want to add another plugin so its not a choice.

    i made the corrections you proposed i have added 11,1 to the action and i removed the exit(0); but still the same problem, the waiting message “soumission en cours….” keeps rolling. can you propose another hook to use and leave the pdf generation at last ?

    thanks

    Hi @omarsekkkk,

    Hope this message finds you well and thanks for the update.

    Here are Forminator Hooks you can try to generate the PDF:

    /**
     * Action called before setting fields to database
     *
     * @since 1.0.2
     *
     * @param Forminator_Form_Entry_Model $entry - the entry model.
     * @param int $form_id - the form id.
     * @param array $field_data_array - the entry data.
     */
    do_action( 'forminator_custom_form_submit_before_set_fields', 'generate_pdf_on_form_submit');
    
    function generate_pdf_on_form_submit($entry, $form_id, $data) {
        // Check if the form ID matches
        if ($form_id === REPLACEWITHYOURFORMID) {
           //Add your custom code here;
        }
    }

    Kind regards,
    Laura

    Thread Starter omarsekkkk

    (@omarsekkkk)

    many thanks for your response, what about success or error how to handle ?

    Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @omarsekkkk

    I hope you are doing well.

    By success and error do you mean in the Form submission or in generating the PDF?

    The forminator_custom_form_submit_before_set_fields is fired only when saving the fields to the database so at this point it would have a valid submission and no need to validate, but you can use if($entry->entry_id) just to confirm it returned the entry_id

    Best Regards
    Patrick Freitas

    Thread Starter omarsekkkk

    (@omarsekkkk)

    this action appears to have only one parameter, but you gave me three, can you make sure please because when i check it needs only one parameter : entry

    also error and success i mean form submission

    many thanks

    Hi @omarsekkkk,

    Thanks for the update.

    As Patrick explained in the previous reply this hook is actioned after the submissions have been saved. I did modify the code to validate the form submission:

    add_action( 'forminator_custom_form_submit_before_set_fields', 'generate_pdf_on_form_submit', 10, 3 );
    function generate_pdf_on_form_submit( $entry, $form_id, $field_data_array ) {
       //Replace with your own form ID
       if($form_id == 26 ){
           if ($entry->entry_id){
              //add custom code here
           }
           else{
               //Error
           }
        }
    }

    I double check on my own lab and it is working as expected.

    Kind regards,
    Laura

    Plugin Support Kris – WPMU DEV Support

    (@wpmudevsupport13)

    Hi @omarsekkkk

    We haven’t heard from you in a while, I’ll go and mark this thread as resolved. If you have any additional questions or require further help, please let us know!

    Kind Regards,
    Kris

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Generate PDF after From submit’ is closed to new replies.