How to save PDF file from jsPDF on a server in Javascript?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to save PDF file from jsPDF on a server in Javascript?




var doc = new jsPDF();
$('#generatereport').click(function()
doc.fromHTML($('#lppresults')[0], 15, 15,
width: 170
, function()
doc.save('sample-file.pdf');
);
);


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.4/jspdf.min.js"></script>
<div class="survey-results" id="lppresults">
<button id="generatereport">Download Report</button>
<h1 style="border: solid;">TEST CONTENT</h1><br />
<h1 style="border: solid;">TEST CONTENT1</h1>
</div>



I want to save file on the server with JavaScript, but currently I can save this file only on the user's computer.





Which server, is it node, IIS?
– Prashant Pimpale
Aug 10 at 12:28



node


IIS





IIS I Thing But Code Run In Php
– Dilip Shekhawat
Aug 10 at 12:37





So do you want to store it on IIS Server?
– Prashant Pimpale
Aug 10 at 12:38





I am using php so server is linux
– Dilip Shekhawat
Aug 10 at 12:41





Then you need to create one API which will accept the PDF file and saved on your WebServer
– Prashant Pimpale
Aug 10 at 13:06



PDF




1 Answer
1



Instead of doc.save function you have to use doc.output function with type 'blob' as a parameter.


doc.save


doc.output


'blob'



In Javascript part:


var doc = new jsPDF();
$('#generatereport').click(function()

doc.fromHTML(
$('#lppresults'), 15, 15,
width: 170,
function()

var blob = doc.output('blob');

var formData = new FormData();
formData.append('pdf', blob);

$.ajax('/upload.php',

method: 'POST',
data: formData,
processData: false,
contentType: false,
success: function(data)console.log(data),
error: function(data)console.log(data)
);

);
);



Here is the code for upload.php:


upload.php


<?php
move_uploaded_file(
$_FILES['pdf']['tmp_name'],
$_SERVER['DOCUMENT_ROOT'] . "/uploads/test.pdf"
);
?>






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard