How to attach images to pdf generated from forminator form
-
I have built a form using forminator which contains an upload image input field, when the form is submitted at final execute button the pdf is generated, this pdf does contain the images uploaded while filling out the form
I want to have the same functionality of attaching the image to the generated pdf not on final execute/submit button but when the form is saved as a draft and the user want’s to send the draft link as an email.
For now when the save-draft link is clicked, the email-form appears to enter email and forminator generates draft link also generates/attach pdf to the email but without the image if any images was uploaded while filling out the form.
I know in official documentation of forminator they says “we dont save image uploads and signatures in drafts”
For now i did this
JQuery, when save-draft-link is clicked, i’m saving the uploaded image url to the db
$('body').on('click', '.forminator-save-draft-link', function(e) {
// e.preventDefault();
// e.stopImmediatePropagation();
// Initialize a FormData object
setTimeout(function() {
var formData = new FormData();
var files = $('input[name="upload-1"]')[0].files;
if(files.length > 0) {
formData.append('file', files[0]);
}
// Add other form data if necessary
formData.append('action', 'save_forminator_draft');
formData.append('draft_id', 'some_draft_id'); // Assuming draft ID is stored in a hidden input with ID 'draft-id'
formData.append('form_id', $('input[name="form_id"]').val()); // Assuming form ID is stored in a hidden input with ID 'form-id'
$.ajax({
url: ajaxurl, // WordPress AJAX handler
type: 'POST',
data: formData,
contentType: false,
processData: false,
success: function(response) {
// Handle the response
console.log(response);
},
error:function(data){
console.log(data);
}
});
}, 2000); // Delay in milliseconds (2000ms = 2 seconds)
});php code
add_action('wp_ajax_save_forminator_draft', 'save_forminator_draft');
add_action('wp_ajax_nopriv_save_forminator_draft', 'save_forminator_draft');
function save_forminator_draft() {
global $wpdb;
$upload_dir = wp_upload_dir();
$custom_dir = $upload_dir['basedir'] . '/forminator/182_824519259117d20a5e68e9b2b8322387/uploads/';
// Ensure the custom directory exists
if (!file_exists($custom_dir)) {
wp_mkdir_p($custom_dir);
}
// Handle file upload
$file_data = array();
if (!empty($_FILES['file']['name'])) {
$uploaded_file = $_FILES['file'];
$file_name = pathinfo($uploaded_file['name'], PATHINFO_FILENAME);
$file_extension = pathinfo($uploaded_file['name'], PATHINFO_EXTENSION);
$timestamp = time();
$new_file_name = $file_name . '-' . $timestamp . '.' . $file_extension;
$new_file_path = $custom_dir . $new_file_name;
if (move_uploaded_file($uploaded_file['tmp_name'], $new_file_path)) {
$file_url = $upload_dir['baseurl'] . '/forminator/182_824519259117d20a5e68e9b2b8322387/uploads/' . $new_file_name;
// $file_url = shorten_url($file_url);
$file_data = array(
'file' => array(
'success' => true,
'file_name' => $new_file_name,
'file_url' => $file_url,
'message' => '',
'file_path' => $new_file_path,
),
);
} else {
$file_data = array(
'success' => false,
'message' => 'Failed to move the uploaded file.'
);
}
}
// Get draft ID and form ID
$draft_id = sanitize_text_field($_POST['draft_id']);
$form_id = intval($_POST['form_id']);
$meta_value = maybe_serialize($file_data);
// Table names with prefix
$entry_table = $wpdb->prefix . 'frmt_form_entry';
$meta_table = $wpdb->prefix . 'frmt_form_entry_meta';
// Insert into crf_frmt_form_entry using SQL query
// Construct SQL query to select the last entry ID
$sql = "SELECT MAX(entry_id) AS last_entry_id FROM $entry_table";
// Execute the query
$entry_id = $wpdb->get_var($sql);
$entry_id = intval($entry_id);
// $entry = Forminator_Form_Entry_Model::get_latest_entry_by_form_id( $form_id );
// $entry_id = $entry->entry_id;
// Insert into frm_entry_meta
$wpdb->insert( // meta table
$meta_table,
array(
'entry_id' => $entry_id,
'meta_key' => 'upload-1',
'meta_value' => $meta_value,
'date_created' => current_time('mysql')
)
);
wp_send_json_success(array('entry_id' => $entry_id));
die();
}But i can’t attach it to the pdf that is attached to the email
I asked this myself in stackoverflow, but no luck
https://stackoverflow.com/questions/78559496/how-to-attach-image-in-the-pdf-generated-by-forminator-formThe page I need help with: [log in to see the link]
- You must be logged in to reply to this topic.