pdfHtml5 button to send table via PHPMailer
pdfHtml5 button to send table via PHPMailer
Hi all,
I have partially succeeded with creating a send button to post filtered editor table data (using predefined search criteria) to phpmailer. The message comes thru, but the attachment file only contains "true".
Here is my code. Any help would be greatly appreciated.
Thanks,
Miklos
topStart: {
buttons: [
{
extend: 'pdfHtml5',
text: 'Send as PDF',
title: 'DataTable Export',
customize: function (doc) {
// optional: customize PDF
},
action: function (e, dt, button, config) {
let pdfDoc = window.pdfMake.createPdf(config);
pdfDoc.getBlob(function (blob) {
let formData = new FormData();
formData.append("pdf", blob, "datatable.pdf");
// send PDF to server via AJAX
fetch("send_pdf_email_koppan.php", {
method: "POST",
body: formData,
})
.then(response => response.text())
.then(result => alert("Email sent: " + result))
.catch(error => alert("Error: " + error));
});
}
},
This question has an accepted answers - jump to answer
Answers
Sounds like a problem in the
send_pdf_email_koppan.php
file rather than the client-side. Does the POST request include the generated PDF data?Allan
Dear Allan,
Honestly, I do not know.
Here is the <code>send_pdf_email_koppan.php</code> file:
I would suggest checking the network inspector in your browser to make sure that the POST request has the PDF data. After that' I'd suggest checking that the file does get moved to the
$target
directory (you aren't deleting it, so it should still be present after the PHP file has completed its execution). See what that is and that it is a valid PDF. The result of that should hopefully inform you of what your next debugging action would be (i.e. if it isn't the PDF, trace back from there, etc).Allan
Thank you Allan. I followed your instruction and checked first, wheter the file does get moved to the $target directory (since I am not deleting it, so it should still be present after the PHP file has completed its execution). Surprisingly, I have found no pdf file generated at all. I belive, the construction of the file is where the clue is. Will try a different approach, now it bothers me so much.
Next step is to make sure that it is in the request body for the Ajax request the client-side is sending. Use the browser's network tools to check that.
Allan
Will check further
This is what I get. The button shows continuously spinning circle, and the "time" tab of inspector show "waiting" status.
Summary
URL: https://penzugy.laparo.hu/DTEditorOrvDiv/send_pdf_email_koppan.php
Status: 200
Source: Network
Address: 185.33.55.22:443
Initiator:
table.orv_div.js:189
Request
:method: POST
:scheme: https
:authority: penzugy.laparo.hu
:path: /DTEditorOrvDiv/send_pdf_email_koppan.php
Accept: /
Accept-Encoding: gzip, deflate, br
Accept-Language: en-GB,en;q=0.9
Connection: keep-alive
Content-Length: 6189
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7n5FucJNBLtAKDGq
Cookie: PHPSESSID=8f6a285a4cfae916baeaf4d9e47bff48; phpbb3_49udr_k=; phpbb3_49udr_sid=a42c1a28ab83a831cd38420a6168822f; phpbb3_49udr_u=2; phpbb3_49udr_ba_2=1744195485; phpbb3_49udr_ccat=%5B%22calendar_on_index%22%2C%22fid_14%22%5D; phpbb3_49udr_ba_1=1744136787; phpbb3_fn1je_k=; phpbb3_fn1je_sid=466e721fc17ea90b6d0a50fb641dcd81; phpbb3_fn1je_u=1
Host: penzugy.laparo.hu
Origin: https://penzugy.laparo.hu
Referer: https://penzugy.laparo.hu/DTEditorOrvDiv/
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-origin
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.6 Safari/605.1.15
Response
:status: 200
Content-Type: text/html; charset=UTF-8
Date: Thu, 26 Jun 2025 19:30:52 GMT
Server: Apache
Request Data
MIME Type: multipart/form-data
Boundary: ----WebKitFormBoundary7n5FucJNBLtAKDGq
Request Data:
I believe the clue is somewhere here:
if (isset($_FILES['pdf']) && $_FILES['pdf']['error'] === UPLOAD_ERR_OK) {
$uploadDir = sys_get_temp_dir();
$uploadFile = $uploadDir . '/' . basename($_FILES['pdf']['name']);
I have not specified (nor created) an upload directory yet. May this be the reason?
I think the problem starts with this:
The
config
parameter is documented inbuttons.buttons.action
as:PDFMake won't understand this object and won't be able to make a PDF out of it. Take a look at
buttons.html5.js
and findDataTable.ext.buttons.pdfHtml5
. Here you will see how Datatables builds the document forpdfMake.createPdf()
.I'm not quite sure the best way to handle this as I've not done it before but you could take the relevant parts of that code and put in your action function or possibly add the ajax request to the end of the function in
buttons.html5.js
. If you modifybuttons.html5.js
then you will need to keep up those modifications every time you upgrade buttons.Another option might be to use
buttons.exportData()
to get teh table data, in thebuttons.buttons.action
function, to send via ajax to a server based PHP PDF library.Kevin
Hi all,
I have finally managed it with success.
Thank you for all your efforts trying to help me achieve this.
For those being in a similar scenario, I would like to share how this can be done.
Many thanks once more,
Yours sincerely,
Miklos
-- JS button config --
-- PHP file --
Hi Miklos,
Nice one! Thanks for sharing your solution with us
.
Allan