1

I am using html2pdf to generate pdf-files. I want to save those files in the background on disk. I am using the html2pdf.outputPdf (jsPDF.output) with the argument "datauri" to make then 64-base encoded.

I then decode them in my php script and save them. When i try to open the generated file in my folder I get the message from adobe that the file is damaged, (possibly not decoded right).

why is this happening, and how can i fix it?

Javascript:

<script>

$(document).ready(function() {

$('#Save').on('click', function () {

  element = document.getElementById('root');

  var pdf;
  var pdfout;
  var Narvarande = "Johnny";
  var filnamn = "123456";
  var Lagenhetsadress = "BB1234 43";

  //html2pdf().from(element).save();
  //console.log("CLicked!");

  html2pdf().from(element).outputPdf('datauri').then(function(pdfout){

//  console.log(btoa(pdf));;

  $.ajax({
    method: "POST",
    url: "includes/skickapdf.php",
    data: {
      data: pdfout,
      besiktare: Narvarande,
      namn: filnamn,
      adress: Lagenhetsadress,
    },
  }).done(function(data){
    console.log("Return från skickapdf: ");
  console.log(data);
});

});

  });
});

</script>

PHP:

<?php
if(!empty($_POST['data']) && isset($_POST['namn']) &&isset($_POST['besiktare'])&& isset($_POST['adress']))
{

  $N = $_POST['namn'];
  $B = $_POST['besiktare'];
  $A = $_POST['adress'];

  $data = base64_decode($_POST['data']);
  echo ($N);
  // print_r($data);
  file_put_contents("{$N}.pdf", $data);
} 
else {
  echo "No Data Sent";
  echo "--------------";
  echo $_POST['data'];
  echo "--------------";
}``
?>
Erik
  • 67
  • 1
  • 10
  • See [How to save a PNG image server-side, from a base64 data string](https://stackoverflow.com/questions/11511511/how-to-save-a-png-image-server-side-from-a-base64-data-string) – guest271314 Feb 09 '19 at 23:18

0 Answers0