使用jsPDF和PHP将生成的PDF文件保存在服务器上

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

我正在尝试使用JSPDF插件将生成的PDF文件保存到服务器中,而不是保存在客户端上,处理JsPDF的javascript部分是这样的:

<script type="text/javascript">

(function(){
 var 
form = $('.form'),
cache_width = form.width(),
//cache_height = form.height(),
a4  =[ 595.28,  841.89];  // for a4 size paper width and height

$('#create_pdf').on('click',function(){
$('body').scrollTop(0);
createPDF();
 });
//create pdf
function createPDF(){
getCanvas().then(function(canvas){
    var 
    img = canvas.toDataURL("image/png"),

    doc = new jsPDF({
      unit:'px', 
      format:'a4'
    });     

 var imgWidth = 220;
 var pageHeight = 295;  
 var imgHeight = canvas.height * imgWidth / canvas.width;
 var heightLeft = imgHeight;

 var doc = new jsPDF('p', 'mm');
 var position = 0;

 doc.addImage(img, 'PNG', 0, position, imgWidth, imgHeight);
 heightLeft -= pageHeight;

 while (heightLeft >= 0) {
    position = heightLeft - imgHeight;
    doc.addPage();
    doc.addImage(img, 'PNG', 0, position, imgWidth, imgHeight);
    heightLeft -= pageHeight;
   }

 var blob = doc.output('blob');
 var formData = new FormData();
 formData.append('pdf', blob);
  $.ajax({
url: 'upload.php', 
type: 'POST',
data: formData,
dataType: 'text',
cache: false,
processData: false,
contentType: false,
success: function(response){
alert(response); 
console.log(response)
 },
   error: function(err){
         alert(err);
        console.log(err)
  }
 });

});
  }

// create canvas object
    function getCanvas(){
      form.width((a4[0]*1.33333) -80).css('max-width','none');
      return html2canvas(form,{
       imageTimeout:2000,
       removeContainer:true
       });  
     }

  }());

 </script>

upload.php文件应将生成的pdf文件或其内容移动到Uploads文件夹,但$ _FILES ['data']为空

   <?php
    if(!empty($_FILES['data'])) {
// PDF is located at $_FILES['data']['tmp_name']

$content = file_get_contents($_FILES['data']['tmp_name']);
//echo $content;
$location = "uploads/";
move_uploaded_file($_FILES['data']['tmp_name'], $location.'random-name.pdf');

  } else {
throw new Exception("no data");
  }

我从Ajax得到的响应是“通知:未定义的索引:数据”。预先感谢您的帮助。

php jquery jspdf
1个回答
1
投票
© www.soinside.com 2019 - 2024. All rights reserved.