Capture pdf button output through ajax to save it on the server
Capture pdf button output through ajax to save it on the server
davidvilallonga
Posts: 4Questions: 0Answers: 0
I have a pdf button:
{ extend: 'pdf',
text: '<i class="fa-solid fa-file-pdf"></i> PDF',
className: 'btn btn-sm',
action: function (e, dt, button, config) {
// Custom logic to capture PDF data and send it to the server
captureAndSavePDF();
}
}
and this finction call ajax, but it give error:
function captureAndSavePDF(dt) {
// Trigger the PDF export
dt.button('.buttons-pdf').trigger();
// Wait for the PDF generation to complete
setTimeout(function() {
// Capture the PDF data from the generated iframe
var pdfData = $('.buttons-pdf iframe').contents().find('body').html();
// Send PDF data to the server using Ajax
$.ajax({
url: 'save_pdf.php',
type: 'POST',
data: { pdfData: pdfData },
success: function (response) {
console.log('PDF saved on the server:', response);
},
error: function (xhr, status, error) {
console.error('Error saving PDF on the server:', error);
}
});
}, 2000);
}
Does anyone done that before ?
Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.
Replies
What error does it give please? is the error in the Javascript, from the
save_pdf.php
file, or something else?Allan
Allan thank you,
The error is js: InternalError: too much recursion
The error is trigger when I call:
dt.button('.buttons-pdf').trigger();
The first line in
captureAndSavePDF
is:And the action for the button triggered is:
so yes, that will cause an infinite recursion.
I'd suggest removing
dt.button('.buttons-pdf').trigger();
. I don't know what the intent with it was, but the user has already pressed the button when you get to that point.Allan
Allan, do you think that this can retreive the blob contents of the pdf file:
var pdfData = $('.buttons-pdf iframe').contents().find('body').html();
No. Because the PDF is not an HTML document.
This is the part of the code that created the PDF. If you need that data in Javascript for whatever reason, you could modify that part. pdfmake probably has an option to return a Blob if you check their API docs.
Allan