Create a New JS file “js/bulk-pdf.js” and Add the functions in functions.php and in “pdf-template.php” add your html
//Bulk All PDF function enqueue_bulk_pdf_scripts() { wp_enqueue_script( 'bulk-all-ccpdf', plugin_dir_url(__FILE__) . 'js/bulk-pdf.js', ['jquery'], '1.0.0', true ); wp_localize_script('bulk-all-ccpdf', 'bulkPDFData', [ 'ajax_url' => admin_url('admin-ajax.php'), ]); } add_action('wp_enqueue_scripts', 'enqueue_bulk_pdf_scripts'); add_action('wp_ajax_get_bulk_certificate_html', 'get_bulk_certificate_html_callback'); function get_bulk_certificate_html_callback() { $bulk_ids = isset($_POST['bulk_ids']) ? explode(',', $_POST['bulk_ids']) : []; if (empty($bulk_ids)) { wp_send_json_error('No IDs sent'); } ob_start(); foreach ($bulk_ids as $post_id) { $get_bulk_post = get_post($post_id); include plugin_dir_path(__FILE__) . 'templates/pdf-template.php'; echo '<div style="page-break-after: always;"></div>'; } $html = ob_get_clean(); wp_send_json_success(['html' => $html]); }
IN JS file add the AJAX script
jQuery(document).ready(function ($) { $('.all-download').on('click', function (e) { e.preventDefault(); const bulkIds = $('#bulk_sid').val(); if (bulkIds.length === 0) { alert('Please select at least one record.'); return; } jQuery('a.btn.all-download').text('Downloading....'); $.ajax({ url: bulkPDFData.ajax_url, method: 'POST', data: { action: 'get_bulk_certificate_html', bulk_ids: bulkIds }, success: function (response) { if (response.success) { const tempDiv = document.createElement('div'); tempDiv.style.margin = '0'; tempDiv.style.padding = '0'; tempDiv.id = 'temp-div'; tempDiv.innerHTML = response.data.html; document.body.appendChild(tempDiv); html2pdf() .from(tempDiv) .set({ margin: 0, filename: 'bulk-certificates-all.pdf', image: { type: 'jpeg', quality: 0.98 }, html2canvas: { scale: 2 }, jsPDF: { unit: 'in', format: 'a4', orientation: 'portrait' } }) .save() .then(() => tempDiv.remove()); jQuery('a.btn.all-download').text('Download All'); } else { alert('Error: ' + response.data); jQuery('a.btn.all-download').text('Download All'); } }, error: function () { alert('AJAX failed.'); } }); }); });
Jagdish Sarma Asked question May 16, 2025