javascript - 无法将生成的 jsPdf 发送到服务器
问题描述
我使用jsPdf和Html2Canvas生成了一个 PDF 。它工作得很好,并且可以下载。
我现在的目标是将生成的.pdf
文件保存到我的服务器上,这样我就可以通过 phpmailer 将其发送出去。这就是我处理这个问题的方式。
function print() {
document.getElementById("out").textContent = document.getElementById("fader").value;
const filename = 'DHC_Herren_Front.pdf';
html2canvas(document.querySelector('#pdf')).then(canvas => {
let pdf = new jsPDF('l', 'mm', 'a4');
pdf.addImage(canvas.toDataURL('image/png'), 'PNG', 0, 0, 298, 211, function () {
var blob = doc.output('blob');
var formData = new FormData();
formData.append('pdf', blob);
$.ajax('/st/tda/dhc/men_front/upload.php', {
method: 'POST',
data: formData,
processData: false,
contentType: false,
success: function (data) {
console.log(data)
},
error: function (data) {
console.log(data)
}
});
});
});
}
和我的upload.php
<?php move_uploaded_file(
$_FILES['pdf']['tmp_name'],
$_SERVER['DOCUMENT_ROOT'] . "/st/tda/dhc/men_front/test.pdf");
?>
我的问题是,为什么我最终在服务器上没有文件。我觉得必须有一个简单的解决方案,但我无法确定它。
最新的 HTML
function ma() {
document.getElementById("out").textContent = document.getElementById("fader").value;
html2canvas(document.querySelector('#pdf')).then(canvas => {
var pdf = btoa(doc.output());
pdf.addImage(canvas.toDataURL('image/png'), 'PNG', 0, 0, 298, 211,);
$.ajax({
method: "POST",
url: "/st/tda/dhc/men_front/upload.php",
data: {data: pdf},
}).done(function(data){
console.log(data);
});
});
}
最新上传.php
<?php
if(!empty($_POST['data'])){
$data = base64_decode($_POST['data']);
// print_r($data);
file_put_contents( "test.pdf", $data );
} else {
echo "No Data Sent";
}
exit();
解决方案
我就是这样做的。看一下,看看您是否可以将其调整为您的代码。这是 ajax 将文件发送到的 uploadFiles.php。
<?php
$ds = DIRECTORY_SEPARATOR; // a directory separator
$cid = $_POST["cid"]; // directory name passed from the form as a variable
$rid = $_POST["rid"]; // directory name passed from the form as a variable
$storeFolder = "../recordFiles/".$cid."/".$rid; // the place where i want to save stuff
// run only if there are files sent from ajax.
if (!empty($_FILES)) {
// check if the directory exists and if not then create it.
if (!file_exists('../recordFiles/'.$cid."/".$rid)) {
mkdir('../recordFiles/'.$cid."/".$rid, 0777, true);
}
// get the temp file name
$tempFile = $_FILES['file']['tmp_name'];
// remove all whitespace in the file name
$cleanedFileName = $_FILES['file']['name'];
$cleanedFileName = preg_replace('/\s+/', '', $cleanedFileName);
// set the target path
$targetPath = dirname( __FILE__ ).$ds.$storeFolder.$ds;
// set the target file name and path
$targetFile = $targetPath.$cleanedFileName;
// move the files
move_uploaded_file($tempFile,$targetFile);
}
?>
推荐阅读
- java - concat Jolt 转换在演示站点上工作;在代码中不起作用
- docker - 如何将在 Docker 容器中运行的 Grafana 连接到在主机上运行的 Prometheus 数据源(在 Docker for Mac 上)?
- c++ - 接受整数输入并返回字符串的函数
- node.js - 关于如何编写一个简单的 JHipster 模块的教程
- android - 广播接收器没有收到消息
- vb.net - 发送 SMS 的应用程序的问题
- ecmascript-6 - 为什么 ES6 模块可以摇树
- python - 如何使用 BigQuery 和 Apache Beam 将 SQL 表转换为行序列列表?
- javascript - 如何在组件 vuejs 中显示 firebase 子集合
- python-3.x - python字典 - 合并两个字典并在它们匹配时附加键值