如何将生成的javascript pdf文件保存到我的服务器&x27;s文件系统?
我正在使用pdfmake创建我的pdf,虽然它允许用户直接打开pdf或将其下载到他们的计算机,但我不确定如何生成pdf并将其保存到服务器的文件系统 据我所知,有很多安全措施不允许javascript将数据保存到文件中,所以将其发送到我的php后端是唯一的选择吗?我该怎么做呢 谢谢 (未经测试)如何将生成的javascript pdf文件保存到我的服务器&x27;s文件系统?,javascript,php,pdfmake,Javascript,Php,Pdfmake,我正在使用pdfmake创建我的pdf,虽然它允许用户直接打开pdf或将其下载到他们的计算机,但我不确定如何生成pdf并将其保存到服务器的文件系统 据我所知,有很多安全措施不允许javascript将数据保存到文件中,所以将其发送到我的php后端是唯一的选择吗?我该怎么做呢 谢谢 (未经测试) PHP: 上载和接收来自的代码。实际上,您可以直接从pdfmake'pdfmake.createPdf(docDefinition).getBase64(函数(dataURL){//do somethin
PHP:
上载和接收来自的代码。实际上,您可以直接从pdfmake'pdfmake.createPdf(docDefinition).getBase64(函数(dataURL){//do something}
<?
// pull the raw binary data from the POST array
$data = substr($_POST['data'], strpos($_POST['data'], ",") + 1);
// decode it
$decodedData = base64_decode($data);
// print out the raw data,
echo ($decodedData);
$filename = "test.pdf";
// write the data out to the file
$fp = fopen($filename, 'wb');
fwrite($fp, $decodedData);
fclose($fp);
?>
var docDefinition = {
content: 'This is an sample PDF printed with pdfMake'
};
pdfMake.createPdf(docDefinition).getBuffer(function(buffer) {
var blob = new Blob([buffer]);
var reader = new FileReader();
// this function is triggered once a call to readAsDataURL returns
reader.onload = function(event) {
var fd = new FormData();
fd.append('fname', 'test.pdf');
fd.append('data', event.target.result);
$.ajax({
type: 'POST',
url: 'upload.php', // Change to PHP filename
data: fd,
processData: false,
contentType: false
}).done(function(data) {
// print the output from the upload.php script
console.log(data);
});
};
// trigger the read from the reader...
reader.readAsDataURL(blob);
});