2

i have following problem.

I use the pdf-lib libary for js to create a pdf. As last step i create an uint8array:

const pdfBytes = await pdfDoc.save()

Now i want to send this pdfbytes with the help of ajax to a php.file on my web server. This php-file should create an pdf out of the pdf byte array and attach this to an email and send it to an specific email-address.

I coded the ajax call like this:

 function sendWithAjax(pdfBytes, email) {
        if (window.XMLHttpRequest) {
            // AJAX nutzen mit IE7+, Chrome, Firefox, Safari, Opera
                xmlhttp=new XMLHttpRequest();
            }
        else {
            // AJAX mit IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }

        xmlhttp.open("POST", "sendContractEmail.php", true);
        const data = {bytes: pdfBytes, mail: email};
        xmlhttp.send(data);

    }

But how can i code the php.file to generate the pdf and send it per email?

My php Code:

<?php

if($_SERVER['REQUEST_METHOD'] == 'PUT') {
    echo "this is a put request\";
    parse_str(file_get_contents("php://input"),$post_vars);

    echo $post_vars['pdfBytes']." is pdfBytes\";

?>

Thans for your help!

Fl4mer
  • 407
  • 1
  • 3
  • 8
  • I recommend using php to write the pdf to a file, then add the attachment using phpMailer https://stackoverflow.com/a/12302354/10456519 – Cat Mar 13 '20 at 19:13
  • For one you have an escape-backslash in your echo, which therefore does not terminate the string. You should remove that. Apart from that, what precise problem are you having with it on PHP side? – ArSeN Mar 13 '20 at 21:16
  • @Fl4mer hi , I have tha same problem: send pdfBytes to php, trasform Uint8Array in pdf and send as attachment. do you have news? thank you – sundsx May 24 '20 at 17:13

1 Answers1

1

I fought for the same problem for a week. In the end I got the result.

The first problem is to convert TypedArray Buffer (pdfBytes) to Base64

var data = new Uint8Array(pdfBytes);

var base64 = bufferToBase64(data); // cal function that convert TypedArray Buffer

//console.log(base64); check output

var formData = new FormData();

   formData.append("name", name);
   formData.append("surname", surname); 
   formData.append("email", email);
   formData.append("pdffile", base64);

   var request = new XMLHttpRequest();
   request.open("POST", "test.php");
   request.send(formData);

send to php

    $first_name = $_POST['name']; // required
    $last_name = $_POST['surname']; // required
    $email_to = $_POST['email']; // required
    $binarr=$_POST['pdffile'];
    $email_from = 'sundsx@mailaddress.it';
    $extension = 'pdf';

    $FileName = str_replace("'", "_", $first_name);
    $FileSurName = str_replace("'", "_", $last_name);

    $filePath = $_SERVER['DOCUMENT_ROOT'].'/files/'.$FileName."_".$FileSurName.".".$extension;

    # Decode the Base64 string, making sure that it contains only valid characters
    $bin = base64_decode($binarr, true);

check your $bin here base64guru

    if (strpos($bin, '%PDF') !== 0) {
    throw new Exception('Missing the PDF file signature');
    }

    # Write the PDF contents to a local file
    file_put_contents($filePath, $bin); // Goal!

Enjoy the easy life

sundsx
  • 588
  • 9
  • 27
  • Reference for TypedArray Buffer (pdfBytes) to Base64 https://coolaj86.com/articles/typedarray-buffer-to-base64-in-javascript – MaunashJani Aug 25 '22 at 18:26