简体   繁体   中英

Trouble sending an generated jsPdf to server

I have generated an PDF using jsPdf and Html2Canvas . It works very well, and is downloadable.

I'm now aiming to get the generated .pdf saved to my server, so I can send it out via phpmailer. This is how I approached this.

function print() {
    document.getElementById("out").textContent = document.getElementById("fader").value;
    const filename = 'DHC_Herren_Front.pdf';

    html2canvas(document.querySelector('#pdf')).then(canvas => {
        let pdf = new jsPDF('l', 'mm', 'a4');
        pdf.addImage(canvas.toDataURL('image/png'), 'PNG', 0, 0, 298, 211, function () {
            var blob = doc.output('blob');

            var formData = new FormData();
            formData.append('pdf', blob);

            $.ajax('/st/tda/dhc/men_front/upload.php', {
                method: 'POST',
                data: formData,
                processData: false,
                contentType: false,
                success: function (data) {
                    console.log(data)
                },
                error: function (data) {
                    console.log(data)
                }
            });
        });

    });
}

and my upload.php

<?php move_uploaded_file(
    $_FILES['pdf']['tmp_name'],
    $_SERVER['DOCUMENT_ROOT'] . "/st/tda/dhc/men_front/test.pdf");
?>

My Question would be, why I end up without an File on the Server. I feel like there must be an simple solution to this, but I just can't pinpoint it.

Newest HTML

       function ma() {
       document.getElementById("out").textContent = document.getElementById("fader").value;


            html2canvas(document.querySelector('#pdf')).then(canvas => {
                    var pdf = btoa(doc.output());
                    pdf.addImage(canvas.toDataURL('image/png'), 'PNG', 0, 0, 298, 211,);


$.ajax({
  method: "POST",
  url: "/st/tda/dhc/men_front/upload.php",
  data: {data: pdf},
}).done(function(data){
   console.log(data);
});

            });


 }

Newest upload.php

   <?php
   if(!empty($_POST['data'])){
    $data = base64_decode($_POST['data']);
    // print_r($data);
   file_put_contents( "test.pdf", $data );
   } else {
   echo "No Data Sent";
   }
   exit();

This is how I do it. Take a look and see if you can adapt it to your code. This is the uploadFiles.php that the ajax sends the file to.

 <?php $ds = DIRECTORY_SEPARATOR; // a directory separator $cid = $_POST["cid"]; // directory name passed from the form as a variable $rid = $_POST["rid"]; // directory name passed from the form as a variable $storeFolder = "../recordFiles/".$cid."/".$rid; // the place where i want to save stuff // run only if there are files sent from ajax. if (!empty($_FILES)) { // check if the directory exists and if not then create it. if (!file_exists('../recordFiles/'.$cid."/".$rid)) { mkdir('../recordFiles/'.$cid."/".$rid, 0777, true); } // get the temp file name $tempFile = $_FILES['file']['tmp_name']; // remove all whitespace in the file name $cleanedFileName = $_FILES['file']['name']; $cleanedFileName = preg_replace('/\\s+/', '', $cleanedFileName); // set the target path $targetPath = dirname( __FILE__ ).$ds.$storeFolder.$ds; // set the target file name and path $targetFile = $targetPath.$cleanedFileName; // move the files move_uploaded_file($tempFile,$targetFile); } ?> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM