Pdf 'download:' option | How to suppress downloading/opening

Pdf 'download:' option | How to suppress downloading/opening

drupalista_com_brdrupalista_com_br Posts: 10Questions: 1Answers: 0
{
    extend: 'pdf',
    download: 'open', // My question is on this configuration option
    customize: function(doc) {
        pdfMake.createPdf(doc).getBlob(function(blob) {
            var formData = new FormData();
            // My fetch logic here for sending the pdf
            // somewhere else.
        });
    },
},

I'd like to suppress the downloading/opening of the pdf file.

I couldn't find a list of options for download:. I've tried 'none' but it does not prevent the downloading process.

I need help figuring out how to manipulate the download process in order to suppress it.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 22,378Questions: 26Answers: 5,139

    There is not a download option for the export buttons. The options are documented here. There isn't a built-in option to perform other actions instead of downloading.

    I'm not sure what you want to do instead of downloading but I think you will need to use buttons.buttons.action for this. See if this thread points you in the right direction.

    If you still need help then please provide details of what you want the PDF export button to do.

    Kevin

  • drupalista_com_brdrupalista_com_br Posts: 10Questions: 1Answers: 0
    edited December 2025

    Yes, there is a download: option. See here.

    I have tested it, without download: 'open' it prompts to the download window. Setting download: 'open' opens the pdf in a new browser tab.

    I'm not sure what you want to do instead of downloading I dont want it to perform no action at all. I just wanna grab a hold to the var formData, which is the pdf content, at the customize: option.

  • drupalista_com_brdrupalista_com_br Posts: 10Questions: 1Answers: 0

    This kinda works:

    action: function(e, dt, button, config) {
        var original = pdfMake.createPdf;
        pdfMake.createPdf = function(docDefinition) {
            var pdf = original.call(this, docDefinition);
            pdf.download = function() {
                pdf.getBlob(function(blob) {
                    var formData = new FormData();
                    // My logic here
                });
            };
            return pdf;
        };
        $.fn.dataTable.ext.buttons.pdfHtml5.action.call(this, e, dt, button, config);
    }
    

    What works
    1. var formData contains the pdf data I want;
    2. It suppresses the button action (no downloading window prompt and no opening in a new tab).

    What does NOT work
    1. It throws an exception
    2. The button circular animation does not quit. It hangs rotating.

    What am I missing?

  • allanallan Posts: 65,452Questions: 1Answers: 10,870 Site admin

    Buttons currently only has options to download or open the file (and even that is actually a bit limited by the browser's settings - i.e. it might download and open it).

    If you just want the contents of the pdf in a buffer, you'd need to [modify the html5pdf button at this point here. Once the PDF has been created, you'll be able to use their API to get the buffer.

    Allan

  • kthorngrenkthorngren Posts: 22,378Questions: 26Answers: 5,139

    Sorry, yes you are right. The docs are here.

    AFAIK there is not a way to stop the Datatables PDF export button from downloading the file. I modified the example in the thread I linked to. It generates the PDF doc into a variable but the PDF button just spins meaning something isn't working correctly with using buttons.buttons.action. I might be missing something. @allan can let us know if using buttons.buttons.action will stop the download from occurring.

    Maybe we can offer other suggestions if tell us what you want to do with the generated PDF file. What are your requirements?

    Kevin

  • drupalista_com_brdrupalista_com_br Posts: 10Questions: 1Answers: 0
    edited December 2025

    `Maybe we can offer other suggestions if tell us what you want to do with the generated PDF file. What are your requirements?

    Kevin`

    My logic sends the pdf content to my server. My server uploads it to google drive.

  • drupalista_com_brdrupalista_com_br Posts: 10Questions: 1Answers: 0
    edited December 2025

    The code I posted just above seems to prevent downloading/new tab opening in my brave browser. The issue is just the spinning button.

  • drupalista_com_brdrupalista_com_br Posts: 10Questions: 1Answers: 0

    Regarding the exception thrown, see image I posted above. The exception is thrown even If I do nothing, like:

    action: function(e, dt, button, config) {
        $.fn.dataTable.ext.buttons.pdfHtml5.action.call(this, e, dt, button, config);
    }
    
  • drupalista_com_brdrupalista_com_br Posts: 10Questions: 1Answers: 0

    I've figured it out. The code below works, no exception thrown, no downloading prompt and no opening in a new tab.

    action: function(e, dt, button, config, cb) {
        var original = pdfMake.createPdf;
        pdfMake.createPdf = function(docDefinition) {
            var pdf = original.call(this, docDefinition);
            pdf.download = function() {
                pdf.getBlob(function(blob) {
                    var formData = new FormData();
                    // My logic here
                });
            };
            return pdf;
        };
        DataTable.ext.buttons.pdfHtml5.action.call(this, e, dt, button, config, cb);
    }
    

    The issue is the cb arg that was missing.

    Thank you fellas.

  • kthorngrenkthorngren Posts: 22,378Questions: 26Answers: 5,139
    edited December 2025

    I just remembered seeing that error before. The buttons.buttons.action has new parameter that was added to buttons 3. It is a required parameter. I added a call to the callback in this updated test case and it solves the error:
    https://live.datatables.net/bowuguwe/1/edit

    Also this doesn't seem to perform the download.

    action: function(e, dt, button, config) {
    $.fn.dataTable.ext.buttons.pdfHtml5.action.call(this, e, dt, button, config);
    }

    The parameter needs to be added to the function and call parameters, for example:
    https://live.datatables.net/hobepiye/1/edit

    Kevin

  • drupalista_com_brdrupalista_com_br Posts: 10Questions: 1Answers: 0
    edited December 2025

    @kthorngren Yeap!!! that is it. The missing cb was causing the exception and the spinning button to never quit spinning. Problem solved. Thank you very much. I owe you a cold beer.

  • kthorngrenkthorngren Posts: 22,378Questions: 26Answers: 5,139
    edited December 2025 Answer ✓

    I think we cross posted as I was editing the above post. Make sure to look at this test case:
    https://live.datatables.net/bowuguwe/1/edit

    It might do what you want to stop the download.

    Kevin

  • drupalista_com_brdrupalista_com_br Posts: 10Questions: 1Answers: 0

    @kthorngren

    https://live.datatables.net/bowuguwe/1/edit

    // Step 5: Send PDF via AJAX
    var pdfDoc = pdfMake.createPdf(docDefinition);
    

    What method do I call on pdfDoc in order to get the pdf blob? You know, the actual pdf content?

  • drupalista_com_brdrupalista_com_br Posts: 10Questions: 1Answers: 0
    edited December 2025

    @kthorngren

    Sorry mate. Forget about my last question. The answer is pdfDoc.getBlob(), I gotta stop and get a coffee.

    Yes, your suggestion also solves my problem. Thank you again.

Sign In or Register to comment.