Capture pdf button output through ajax to save it on the server

Capture pdf button output through ajax to save it on the server

davidvilallongadavidvilallonga Posts: 4Questions: 0Answers: 0
edited November 2023 in Free community support

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

  • allanallan Posts: 62,524Questions: 1Answers: 10,273 Site admin

    What error does it give please? is the error in the Javascript, from the save_pdf.php file, or something else?

    Allan

  • davidvilallongadavidvilallonga Posts: 4Questions: 0Answers: 0

    Allan thank you,
    The error is js: InternalError: too much recursion

  • davidvilallongadavidvilallonga Posts: 4Questions: 0Answers: 0

    The error is trigger when I call:
    dt.button('.buttons-pdf').trigger();

  • allanallan Posts: 62,524Questions: 1Answers: 10,273 Site admin

    The first line in captureAndSavePDF is:

    dt.button('.buttons-pdf').trigger();
    

    And the action for the button triggered is:

    captureAndSavePDF();
    

    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

  • davidvilallongadavidvilallonga Posts: 4Questions: 0Answers: 0

    Allan, do you think that this can retreive the blob contents of the pdf file:

    var pdfData = $('.buttons-pdf iframe').contents().find('body').html();

  • allanallan Posts: 62,524Questions: 1Answers: 10,273 Site admin

    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

Sign In or Register to comment.