【问题标题】:Convert website form to PDF and email to webmaster将网站表单转换为 PDF 并通过电子邮件发送给网站管理员
【发布时间】:2019-11-03 16:10:28
【问题描述】:

我正在尝试为我的网站创建一个表单,提交后将转换为 PDF 并通过电子邮件发送给我自己(网站管理员)。

感谢我无法仅使用 JQuery/Javascript 完成操作,我需要使用 PHP 发送电子邮件。

将 HTML 转换为 PDF 的 Javascript

var doc = new jsPDF(); 
var specialElementHandlers = { 
    '#editor': function (element, renderer) { 
        return true; 
    } 
};
$('#submitformz').click(function () { 
    doc.fromHTML($('#content').html(), 15, 15, { 
        'width': 190, 
            'elementHandlers': specialElementHandlers 
    }); 
    doc.save('sample-page.pdf');  
});

我上面的问题是它转换为 PDF 然后下载到用户计算机上。 理想情况下,我想转换“doc.save”,以便它与 PHP 绑定以通过电子邮件发送文档

PHP:

if(isset($_POST['email'])) {

    $email_to = "webmaster@website.com";
    $email_subject = "Your email subject line";

    function died($error) {
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }

if(!isset($_POST['first_name']) ||
    !isset($_POST['last_name']) ||
    !isset($_POST['email']) ||
    !isset($_POST['telephone']) ||
    !isset($_POST['comments'])) {
    died('We are sorry, but there appears to be a problem with the form you submitted.');       
}


$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required

$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}

$string_exp = "/^[A-Za-z .'-]+$/";

if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
} 

if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}

if(strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
}

if(strlen($error_message) > 0) {
    died($error_message);
}

$email_message = "Form details below.\n\n";


function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
}



$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";

// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);

或者... 有什么方法可以修改 Javascript,而不是在客户计算机上下载文件,而是下载到我网站后端的媒体库中?

【问题讨论】:

  • 如果您希望通过电子邮件发送 pdf,那么您必须使用 php 在服务器上创建 pdf 并通过电子邮件附加。您不能使用客户端 (javascript) pdf 转换器,因为它只会将该区域打印为 pdf 并下载到用户的计算机上。寻找服务器端 (php) pdf 转换器/创建者。
  • 嗯,您当然可以使用 jspdf 从客户端将 html 转换为 pdf,然后通过 ajax 或类似的方式将其发送到服务器。看到这个example
  • 或者这个Q/A
  • 允许从受信任的来源向您发送任意文件是个坏主意,您绝对应该在服务器上执行此操作。尽管查看您的代码,但为什么要麻烦,只需发送文本,看起来像 html,因此将其转换或包装在 pre 标记中。您可能还想考虑不使用 mail() 并使用一个为您处理 HTML 和标头注入 shinanigans 的库。
  • @LawrenceCherone 你需要原谅我的天真。我对编码和学习这一点相对较新,所以我有点不确定在 pre 标签中包装是什么意思......还有......是否不可能通过 JS 将 html 转换为 PDF,然后发送给我通过 PDF 的附件?理想情况下,一旦这个人点击发送,这就是我想做的事

标签: php jquery email


【解决方案1】:

如果我目前了解您,您应该从客户端和前端获取数据,然后在后端使用 php 创建 PDF(您可以使用 wkhtmltopdf 将 HTML(由后端数据生成)转换为 PDF)然后通过电子邮件发送此 PDF 文件。

【讨论】:

  • 看起来是个不错的解决方案。原谅我的天真,我的意思是在问题中说我在共享主机包上使用 Wordpress,但是从我看过的文档和教程的外观来看,这需要直接安装在服务器上吗?
  • 是的。您必须在您的服务器上安装 wkhtmltopdf 软件。
  • 我无法做到这一点,因为我在与我的托管服务提供商交谈时发现,因为我在一个无法访问服务器的共享托管包上。您知道我可以在哪里创建一个 PHP 文件来实现类似结果的解决方案吗?
  • 是的,您可以使用在线pdf转换器的api,例如zamzar.com。您应该通过 curl 将您的 html、image、word、... 文件发送到该站点,它会返回一个 url 到您的回调以下载转换后的文件。
猜你喜欢
  • 1970-01-01
  • 2012-10-17
  • 2014-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-14
  • 2023-04-08
相关资源
最近更新 更多