Javascript is_文件在传递pdf文件时失败
我正在写一些代码来获取一个页面,将其转换为pdf,然后通过电子邮件发送pdf。我正在使用html2canvas和jsPDF生成pdf。然后我将pdf发送到一个php脚本来处理它并将其发送出去,但这是失败的。我知道pdf生成是有效的,因为我可以在本地保存pdf。以下是我目前的代码: javascript:Javascript is_文件在传递pdf文件时失败,javascript,php,Javascript,Php,我正在写一些代码来获取一个页面,将其转换为pdf,然后通过电子邮件发送pdf。我正在使用html2canvas和jsPDF生成pdf。然后我将pdf发送到一个php脚本来处理它并将其发送出去,但这是失败的。我知道pdf生成是有效的,因为我可以在本地保存pdf。以下是我目前的代码: javascript: $(document).ready(function() { $('#download').click(function() { html2canvas($('#wrappe
$(document).ready(function() {
$('#download').click(function() {
html2canvas($('#wrapper'), {
onrendered: function(canvas) {
var imgData = canvas.toDataURL('image/png');
var doc = new jsPDF('p','mm');
doc.addImage(imgData, 'PNG', 10,10);
var pdfMail = btoa(doc.output());
$.post("../mailPdf.php",
{
data:pdfMail
},function (response,status) {
console.log(response);
});
}
});});});
mailPdf.php
function MailWithAttachment($to, $subject, $message, $senderMail, $senderName, $files){
$from = $senderName." <".$senderMail.">";
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
// preparing attachments
if(count($files) > 0){
for($i=0;$i<count($files);$i++){
if(is_file($files[$i])){
$message .= "--{$mime_boundary}\n";
$fp = @fopen($files[$i],"rb");
$data = @fread($fp,filesize($files[$i]));
@fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".basename($files[$i])."\"\n" .
"Content-Description: ".basename($files[$i])."\n" .
"Content-Disposition: attachment;\n" . " filename=\"".basename($files[$i])."\"; size=".filesize($files[$i]).";\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
}
}
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $senderMail;
//send email
$mail = mail($to, $subject, $message, $headers, $returnpath);
//function return true, if email sent, otherwise return fasle
if($mail){ return TRUE; } else { return FALSE; }
}
if(!empty($_POST['data'])){
//email variables
$to = 'recipient@email.com';
$from = 'sender@email.com';
$from_name = 'From Name';
//attachment files path array
$file = base64_decode($_POST['data']);
$subject = 'PHP Email with attachment';
$html_content = '<h1>PHP Email with attachment</h1>';
//call MailWithAttachment() function and pass the required arguments
$send_email = MailWithAttachment($to,$subject,$html_content,$from,$from_name,$file);
//print message after email sent
echo $send_email?"<h1> Mail Sent</h1>":"<h1> Mail not SEND</h1>";
} else {
echo "No Data Found";
}
函数MailWithAttachment($to、$subject、$message、$senderMail、$senderName、$files){
$from=$senderName.“;
$headers=“From:$From”;
//边界
$semi_rand=md5(time());
$mime_boundary=“==Multipart_boundary_x{$semi_rand}x”;
//附件的标题
$headers.=“\n时间版本:1.0\n”。“内容类型:多部分/混合;\n”。“边界=\”{$mime\U边界}\”;
//多部分边界
$message=“-->$mime\u boundary}\n.”内容类型:text/html;字符集=\“UTF-8\”\n。
“内容传输编码:7bit\n\n”。$message.\n\n”;
//准备附件
如果(计数($files)>0){
对于($i=0;$iphp端的问题是,在函数MailWithAttachment
中,您希望文件
参数的数据类型稍有不同
$file=$\u POST['data'];
实际上不是一个文件,而是一个base64编码的字符串(btoa(doc.output())
)。因此,您实际上想要做的是这样的事情(在MailWithAttachment
函数的签名之后)
更改此项:
//attachment files path array
$file = $_POST['data'];
对这样的事情:
//attachment file encoded
$files = array();
$file = $_POST['data'];
$filePath = '/tmp/foo.pdf';
// actually here you can do whatever you want. you just need to save file on disk, at least for request context. I think creating file in memory also should work.
file_put_contents($filePath, base64_decode($file));
$files[] = $filePath;
...
// pay attention $file is changed to $files here
MailWithAttachment(...,$files);
...
php端的问题是,在函数MailWithAttachment
中,您希望文件
参数的数据类型稍有不同
$file=$\u POST['data'];
实际上不是一个文件,而是一个base64编码的字符串(btoa(doc.output())
)。因此,您实际上想要做的是这样的事情(在MailWithAttachment
函数的签名之后)
更改此项:
//attachment files path array
$file = $_POST['data'];
对这样的事情:
//attachment file encoded
$files = array();
$file = $_POST['data'];
$filePath = '/tmp/foo.pdf';
// actually here you can do whatever you want. you just need to save file on disk, at least for request context. I think creating file in memory also should work.
file_put_contents($filePath, base64_decode($file));
$files[] = $filePath;
...
// pay attention $file is changed to $files here
MailWithAttachment(...,$files);
...
文件路径是否在$\u POST['data']
绝对路径(例如/home/username/file.pdf
)或相对路径(例如/file.pdf
)中?此外,每一行都应该用\r\n
分隔,而不是用\n
或\n\n
-请参阅以了解更多详细信息。文件路径是否在$\u POST['data'中
绝对路径(例如/home/username/file.pdf
)或相对路径(例如/file.pdf
)?此外,每一行应该用\r\n
分隔,而不是\n
或\n\n
——请参阅以了解更多详细信息。是的,我意识到我在发布问题时输入错误,我的脚本确实有$file=base64\u解码($_POST['data']))
然而,似乎在本地保存文件可以让函数工作。虽然我真的不想每次都在本地保存文件。我会继续工作,谢谢!@tsHunter,您可以检查tmpfile
函数以及php://memory
和php://temp
。如果这个答案解决了你的问题,将其标记为已接受。是的,我意识到我在发布问题时输入了错误,我的脚本确实有$file=base64\u decode($\u POST['data']))
然而,似乎在本地保存文件可以让函数工作。虽然我真的不想每次都在本地保存文件。我会继续工作,谢谢!@tsHunter,您可以检查tmpfile
函数以及php://memory
和php://temp
。如果此答案解决了您的问题,请将其标记为已接受。