簡體   English   中英

無法將生成的jsPdf發送到服務器

[英]Trouble sending an generated jsPdf to server

我已經使用jsPdfHtml2Canvas生成了PDF。 它工作得很好,並且可以下載。

我現在的目標是將生成的.pdf保存到我的服務器,因此可以通過phpmailer發送出去。 這就是我的解決方法。

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)
                }
            });
        });

    });
}

和我的upload.php

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

我的問題是,為什么我最終在服務器上沒有文件。 我覺得必須對此有一個簡單的解決方案,但我無法查明。

最新的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);
});

            });


 }

最新的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();

這就是我的方法。 看看是否可以使其適應您的代碼。 這是ajax將文件發送到的uploadFiles.php。

 <?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); } ?> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM