I have been using JSPDF and HTML2CANVAS to create a PDF file which has been working great to save the file locally on my machine, however, I've been having trouble getting that file to save to a folder on the server instead of downloading it locally.
$(".publish").on("click", function () {
generatePDF();
$('#myModal').modal('show');
});
var pagesRendered = 0;
//Generate PDF
function generatePDF() {
window.scrollTo(0, 0);
$(".pagesRendered").text(pagesRendered);
var pdf = new jsPDF();
$(".page0").fadeIn();
var w = 2034;
var h = 1262.22;
var div = document.querySelector('.page0');
var canvas = document.createElement('canvas');
canvas.width = w * 2;
canvas.height = h * 2;
canvas.style.width = w + 'px';
canvas.style.height = h + 'px';
var context = canvas.getContext('2d');
context.scale(2, 2);
html2canvas(div, {
canvas: canvas,
}).then(function (canvas) {
var imgData = canvas.toDataURL("image/png", 1.0);
pdf.addImage(imgData, 'PNG', -38, -17, 600, 420);
$(".page0").fadeOut();
pagesRendered++;
$(".pagesRendered").text(pagesRendered);
savePDF();
});
function savePDF() {
//jsPDF code to save file - works as expected when downloading
locally.
// pdf.save('test.pdf');
//Trying to save PDF to server
var doc = btoa(pdf.output());
$.ajax({
method: "POST",
url: "http://example.com/wp-content/themes/theme-child/uploadPDF.php",
data: {
data: doc
},
}).done(function (data) {
console.log(data);
})
}
The contents of upload.php:
the path being: http://example.com/wp-content/themes/theme-child/uploadPDF.php
<?php if(!empty($_POST['data'])){
$data = base64_decode($_POST['data']);
file_put_contents( "http://example.com/wp-content/themes/theme-child/pdf-output/test.pdf", $data );
echo "success";
} else {
echo "No Data Sent";
}
exit();
?>
The error I'm getting is:
POST http://example.com/wp-content/themes/theme-child/uploadPDF.php 404 (Not Found)
Although I know the path is correct...
Any help is appreciated, i'm stumped.