首页 > 解决方案 > 无法将生成的 jsPdf 发送到服务器

问题描述

我使用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);
});

            });


 }

最新上传.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();

标签: javascriptphpjspdf

解决方案


我就是这样做的。看一下,看看您是否可以将其调整为您的代码。这是 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);
   
}

?>


推荐阅读