Lazy load pdfmake
Lazy load pdfmake
I am very grateful for datatables!
I would like to defer loading of the pdfmake and zip libraries used for export until if/when user clicks the download pdf or excel button.
I saw question https://datatables.net/forums/discussion/47810/has-anyone-looked-into-lazy-aysnc-loading-of-the-pdf-export-function which asks exactly this, and the answer to look into custom buttons. That discussion is now closed so I can't ask a follow up directly.
I wondered if the custom button would have to be written from scratch (perhaps based on the source code of the original buttons) or if there is some way to extend the original buttons. I know that buttons have an extend feature but I'm not sure if that is relevant.
Alternatively is there any way to force the buttons plugin to render the pdf and Excel buttons even if pdfmake is not loaded at the time they table is initialised? Perhaps by creating a dummy pdfmake function? The actual pdfmake library could then be set to defered download so it will be available usually within a few seconds of page load.
This question has an accepted answers - jump to answer
Answers
The
buttons.buttons.action
can be used to call one of the export options. See the last example in the docs. I think what Allan is suggesting is to load PDFMake in the action function then execute the desired export.Kevin
Thank you very much!
In case it helps anyone here is the code I used (using ESM):
module pdf-lazy
module datatables-custom-buttons
module datatables