如何从ajax数据发送将pdf上载到服务器(使用jsPDF) [英] how to upload pdf to server from ajax data send (using jsPDF)
问题描述
我正在使用jsPDF在客户端生成pdf.使用函数doc.save('filename.pdf'),我可以下载它. 现在,我需要将其保存在服务器上,因此我将使用.ajax()发送pdf数据,并使用PHP脚本进行接收,但是生成的pdfURL上的图像未显示(
I'm using jsPDF to generate a pdf on client-side. With the function doc.save('filename.pdf') I can download it. Now I need to save it on the server, so I'm sending the pdf data with .ajax() and receiving it with a PHP script but the images on the generated pdfURL doesn't show (http://mydomain/tmp/test.pdf); only shows the text.
你能帮我一下吗?
我的js代码:
//doc.save('test.pdf'); WORKS WELL
var pdf = doc.output();
$.ajax({
method: "POST",
url: "inc/test.php",
data: {data: pdf},
}).done(function(data){
console.log(data);
});
PHP脚本:
<?php
if(!empty($_POST['data'])){
$data = $_POST['data'];
print_r($data);
file_put_contents( "../tmp/test.pdf", $data );
} else {
echo "No Data Sent";
}
exit();
?>
这是在php脚本编写过程之后生成的pdf: http://control.edge-cdn.com.ar/tmp/test.pdf
This is the pdf generated after the php scripting proccess: http://control.edge-cdn.com.ar/tmp/test.pdf
这是使用doc.save()函数生成的: http://control.edge-cdn.com.ar/repo/all.pdf 问候!
And this is the generated with the doc.save() function: http://control.edge-cdn.com.ar/repo/all.pdf Regards!
推荐答案
解决方案 :
SOLUTION:
我试图将pdf数据作为二进制发送.我只是对字符串进行base64编码,将其发送并在php上进行解码.
I was trying to send the pdf data as binary. I just base64 encode the string, send it and them decode that on the php.
JS:
var pdf = btoa(doc.output());
$.ajax({
method: "POST",
url: "inc/test.php",
data: {data: pdf},
}).done(function(data){
console.log(data);
});
PHP:
if(!empty($_POST['data'])){
$data = base64_decode($_POST['data']);
// print_r($data);
file_put_contents( "../tmp/test.pdf", $data );
} else {
echo "No Data Sent";
}
exit();
这篇关于如何从ajax数据发送将pdf上载到服务器(使用jsPDF)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!