我正在使用 jsPDF 在客户端创建 pdf,使用 pdf.save('document.pdf');
效果很好。现在,我尝试通过在后端使用 AJAX 和 Zend_Mail
函数发送电子邮件和 PDF 作为附件,但它不起作用!
嗯,我试过 pdf.output();
, pdf.output('blob');
也试过传递 pdf.output ('datauristring');
但面临不同的问题。
首先,我使用 domtoimage
将 HTML 转换为 png,其代码如下:
domtoimage.toPng(node)
.then(function (dataUrl) {
/* This function is used to generate or send output of the pdf to the back end */
createCalendarPdf(dataUrl);
})
.catch(function (error) {
alert('Oops, something went wrong! ' + error);
}
);
为了在客户端下载 PDF,我简单地使用了它 作品
pdf.save('document.pdf');
对于发送 blob
或 base64 string
我遇到了困难。
第一种方法
下面的代码将 PDF 输出更改为 blob
object url
BLOB 对象 URL
为 PDF 创建一个 blob 对象 url
var binaryImg = pdf.output();
var length = binaryImg.length;
var arrayBuffer = new ArrayBuffer(length);
var uintArray = new Uint8Array(arrayBuffer);
for (var i = 0; i < length; i++) {
uintArray[i] = binaryImg.charCodeAt(i);
}
var currentBlob = new Blob([uintArray], {type: 'application/pdf'});
var createdBlobUrl = URL.createObjectURL(currentBlob);
# Output looks like this
blob:http://example.com/ff471a1c-702j-453d-01g0-d6a26l901bvz
AJAX 请求
使用FormData
var fd = new FormData();
fd.append('file', createdBlobUrl);
$.ajax({
type: 'POST',
url: functionUrl,
data: fd,
processData: false,
contentType: false,
async: false
}).done(function(data) {
console.log(data);
});
所以,我不确定如何从 Zend 或简单的 PHP 中的 blob
对象 URL 获取内容。所以我尝试了 file_get_contents($this->_params['file'])
但它没有用!
第二种方法
试图通过 pdf.output();
或 pdf.output('datauristring');
但后端无法收到结果,因为字符串的长度。正如我之前所说,我首先将 HTML 模板更改为 PNG,然后从中创建 PDF。所以,这就是为什么网络检查员卡在进程之间然后必须完全终止页面的原因。
已经增加了 max_post_size
和 php.ini
中已有的其他内容
Zend_Mail 函数
$mail = new Zend_Mail();
$attachment = $mail->createAttachment($pdfFile);
$attachment->type = 'application/pdf';
$attachment->disposition = Zend_Mime::DISPOSITION_ATTACHMENT;
$attachment->encoding = Zend_Mime::ENCODING_BASE64;
$attachment->filename = 'document.pdf';
$mail->setBodyHtml($html);
$mail->setFrom($emailFrom, 'Adeel');
$mail->addTo($emailTo, 'ABC');
$mail->setHeaderEncoding(Zend_Mime::ENCODING_BASE64);
$mail->setSubject($subject);
// send email
$mail->send();
所以,简而言之,我只想知道如何从 blob
对象 url 中获取内容,如果有任何可能的方法或如何将 base64
字符串传递给后端并在 PHP 中解析它。
我已经花了几天时间寻找这个问题的解决方案,但找不到,或者我在这里做错了什么。
最佳答案
我使用了结合了 canvas 和 jspdf 的 html2pdf 库,所以我确信工作仍然相同或大致相似。
第一步 我创建了 pdf 并将 datauristring 的输出设置为一个 blob
async function printer() {
var worker = html2pdf();
await worker.from(document.querySelector('#element'));
let myPdf = await worker.outputPdf('datauristring', 'mypdf.pdf');
//this converts the uri to blob
var preBlob = dataURItoBlob(myPdf);
var file = new File([preBlob], "namefile.pdf", {type: 'application/pdf'});
// sends the message to api
sendFile(file);
}
//下面是将uri字符串转换为blob的函数
function dataURItoBlob(dataURI) {
// convert base64/URLEncoded data component to raw binary data held in a string
var byteString;
if (dataURI.split(',')[0].indexOf('base64') >= 0)
byteString = atob(dataURI.split(',')[1]);
else
byteString = unescape(dataURI.split(',')[1]);
// separate out the mime component
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
// write the bytes of the string to a typed array
var ia = new Uint8Array(byteString.length);
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
return new Blob([ia], {type:mimeString});
}
然后我有了将电子邮件发送到 API 的实际功能。由于不熟悉 AJAX,我使用了 fetch:
async function sendFile(userFiles) {
let filePaths;
let formData = new FormData();
formData.append("file", userFiles);
let response = await fetch('mailer.php', {
method: 'POST',
body: formData
});
filePaths = await response.json();
return filePaths;
}
关于php - 如何将 PDF (jsPdf) 发送到后端 (PHP - Zend 1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49055072/