JSPDF + HTML2CANVAS + Wordpress +尝试将PDF保存到服务器上的文件夹。

时间:2018-09-21 23:30:25

标签: php ajax wordpress jspdf html2canvas

我一直在使用JSPDF和HTML2CANVAS创建一个PDF文件,该文件在将文件本地保存到我的机器上方面一直很有效,但是,我一直很难将文件保存到服务器上的文件夹中,而不是保存在服务器上在本地下载。

$(".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);
    })

    }

upload.php的内容:

路径为: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();
?>

我得到的错误是:

POST http://example.com/wp-content/themes/theme-child/uploadPDF.php 404 (Not Found)

尽管我知道路径是正确的...

感谢任何帮助,我很沮丧。

0 个答案:

没有答案