0

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.

  • Are you sure your remote server allows absolute URLs for `file_put_contents()`? – Hans Sep 22 '18 at 00:27
  • As I'm reading more into it I believe it may have something to do with admin-ajax.php - https://wordpress.stackexchange.com/questions/307835/using-custom-php-file-for-ajax-url-inside-plugin?answertab=oldest#comment454175_307839 – terrorbyrd Sep 22 '18 at 15:52
  • @JoelMcCann did you find a solution to this? I have a similar problem where I am looking to have a form on my website completed and when submitted, converted to PDF and then saved to server but no joy as yet. I cannot use any of the PHP libraries as I am on a shared server and do not have root access. My question is https://wordpress.stackexchange.com/questions/341547/converting-plugin-to-download-data-on-server-side/341549#341549 i think its similar? – Paulmcf1987 Jul 02 '19 at 18:50
  • I have the same issues I am also using JSPDF but I don't this so JSPD support save the PDF files in the folder. – kantsverma Mar 15 '21 at 07:37

0 Answers0