麻烦发送生成jsPdf到服务器

问题描述 投票:0回答:1

我已经生成使用jsPdf和Html2Canvas的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();
javascript php jspdf
1个回答
0
投票

这就是我要做的事。看看,看看,如果你能使其适应你的代码。这是uploadFiles.php了AJAX的文件发送到。

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

?>
© www.soinside.com 2019 - 2024. All rights reserved.