Storing generated PDF files on the server

前端 未结 5 2091
别跟我提以往
别跟我提以往 2020-12-05 05:34

Using the jsPDF plugin, I am creating a .pdf file and generating a download based on a button click. I would like to save the file onto my server i

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-05 06:21

    If your PDF contains binary data, you can get into problems if you try to transfer the PDF via string. I used the blob output format of jsPDF with success.

    var doc = new jsPDF();
    // ... generate pdf here ...
    
    // output as blob
    var pdf = doc.output('blob');
    
    var data = new FormData();
    data.append('data' , pdf);
    
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
      if (this.readyState == 4) {
        if (this.status !== 200) {
          // handle error
        }
      }
    }
    
    xhr.open('POST', 'upload.php', true);
    xhr.send(data);
    

    Your upload.php can then use the $_FILES array in PHP

    
    

提交回复
热议问题