【发布时间】:2020-04-25 22:29:05
【问题描述】:
我有两个 PHP 页面。第一个是用户输入数据的表单,当提交第二个页面时,它会创建一个生成“发票”的 div。此页面具有将该发票保存为 PDF 或 IMG 的选项。此页面还会自动发送并通过电子邮件确认。
我想在这封电子邮件中发送 PDF 或图片作为附件。
html2canvas.js .png:
function picDiv() {
html2canvas($('#printableArea')[0], {
width: 1200
}).then(function(canvas) {
var a = document.createElement('a');
a.href = canvas.toDataURL("image/png");
a.download = 'nalog.png';
a.click();
});
};
和html2pdf.js生成pdf:
function pdfDiv() {
var element = document.getElementById('printableArea');
var opt = {
filename: 'nalog.pdf'
};
html2pdf().set(opt).from(element).save();
};
我已经阅读了Send Image to server using File input type、html2canvas and jsPDF : send generated pdf as email attachment 等内容。还有这样的文章:Send HTML5 Canvas as Image to Server 和 html2pdf github post。 最后一篇 html2pdf github 帖子指出以下代码可以发送到电子邮件或服务器,但我不知道如何通过电子邮件发送。它将pdf成功转换为字符串。
html2pdf().from(element).toPdf().output('datauristring').then(function (pdfAsString) {
//send via email.
});
我的php邮件发送是:
$user = "some email";
$usersubject = "Initium - Vaš online radni nalog";
$userheaders = "From: some email";
$usermessage = "
//some content with php variables and text.
";
mail($user,$usersubject,$usermessage,$userheaders);
如果有人能解释如何通过我已有的代码发送 PDF 或图像,我会很高兴。 请记住,这一切都在同一页面上,并且需要在同一页面加载时完成。
我尝试了很多事情和方法,但迷失了方向,因为这对我来说是新的领域。任何帮助表示赞赏。
编辑:
完整的工作解决方案在这里:https://stackoverflow.com/a/61418366/7158959
【问题讨论】:
标签: javascript php html2canvas html2pdf