pdf 生成和打印的长时间延迟发送电子邮件。在服务器解析脚本时放置图像?

发布于 2024-11-25 04:20:02 字数 273 浏览 2 评论 0原文

我正在制作一个表格,您可以在其中订购快递服务。它的主要思想是生成包含验证数据的 PDF 文件,然后将其附加到电子邮件中发送给客户 + 抄送给快递公司。

问题是 pdf 生成 (TCPDF) 和电子邮件发送 (Swiftmail) 需要相当长的时间。

我想防止不耐烦的用户一遍又一遍地单击“确认”。理想的解决方案是显示一些 gif“正在加载”图像等。我研究过 jquery .load() 函数,它看起来对于这个问题来说是件好事,但是对于没有 js 的用户呢?

你能指出我正确的方向吗?

I'm working on a form in which you can order courier service. The main idea for it is to generate PDF file containing validated data and then attach it to an e-mail to client + cc for courier company.

The thing is pdf generation (TCPDF) and e-mail sending (Swiftmail) takes noticeably long.

I would like to prevent impatient user from clicking 'confirm' over and over. The ideal solution would be to show some gif 'loading' image or so. I've looked into jquery .load() function and it looks like a good thing for this problem, but what about users w/o js?

Can you point me in the right direction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谈情不如逗狗 2024-12-02 04:20:02

在这种情况下我会使用ajax。但如果您真正担心缺乏 JS,那么有几种方法可以在没有 JS 的情况下做到这一点。
首先,您可以在开始使用 pdf 之前向用户显示一些内容。例如,

<?php
echo "<html><head></head><body> Please Wait..";
flush();
// process pdf here
?>

函数flush()将强制网络服务器将数据发送到浏览器,以便在加载页面的其余部分时显示数据。然而有些浏览器会在内部缓存这个东西。有一些方法可以克服这个问题(查看 php.net 中的lush() 函数注释),但它可能会变得混乱。

另一种方法是异步处理 PDF。在这种情况下,目标脚本仅显示一条消息,表明电子邮件将很快发送。并解雇真正要发送它的脚本女巫。例如:

<?php
  echo "<html><head></head><body> Email will be send out shortly </body></html>";

  $params='';
  foreach ($_POST as $name => $value) {
    $params.=$name.'='.$value.'&';
  }
  if ($params!='')
    $params=urldecode(substr($params,0,-1));
  $parts=parse_url('process_pdf.php'.'?'.$params);
  $fp = fsockopen('process_pdf.php',80, $errno, $errstr, 30);
  $out = "POST ".$parts['path']." HTTP/1.1\r\n";
  $out.= "Host: ".$parts['host']."\r\n";
  $out.= "Content-Type: application/x-www-form-urlencoded\r\n";
  $out.= "Content-Length: ".strlen($parts['query'])."\r\n";
  $out.= "Connection: Close\r\n\r\n";
  $out.= $parts['query'];
  fwrite($fp, $out);
  fclose($fp);
?>

在此示例中,“process_pdf.php”将获得与原始脚本相同的 POST 参数。但会在后台执行,不会中断原始页面。

I would use ajax in this case. but if lack of JS is a genuine concern for you, there are couple of ways to do this without JS.
First you can display something to the user before starting to work with pdf. eg

<?php
echo "<html><head></head><body> Please Wait..";
flush();
// process pdf here
?>

function flush() will force the webserver to send the data to the browser, so it will be displayed while the rest of the page is loaded. However some browsers will cache this thing internally. There are ways to overcome this (check out comments in php.net for a flush() function) but it could get messy.

Another way is to process PDF asynchronously. In this case target script just displays a message that email will be send shortly. And fires the script witch actually going to send it. eg:

<?php
  echo "<html><head></head><body> Email will be send out shortly </body></html>";

  $params='';
  foreach ($_POST as $name => $value) {
    $params.=$name.'='.$value.'&';
  }
  if ($params!='')
    $params=urldecode(substr($params,0,-1));
  $parts=parse_url('process_pdf.php'.'?'.$params);
  $fp = fsockopen('process_pdf.php',80, $errno, $errstr, 30);
  $out = "POST ".$parts['path']." HTTP/1.1\r\n";
  $out.= "Host: ".$parts['host']."\r\n";
  $out.= "Content-Type: application/x-www-form-urlencoded\r\n";
  $out.= "Content-Length: ".strlen($parts['query'])."\r\n";
  $out.= "Connection: Close\r\n\r\n";
  $out.= $parts['query'];
  fwrite($fp, $out);
  fclose($fp);
?>

in this example "process_pdf.php" will get all the same POST parameters as original script. but will be executed in the background without interrupting the original page.

仲春光 2024-12-02 04:20:02

超过98%的用户激活了javascript。剩下的 2% 是由:

  • 非常书呆子的人知道如何禁用它并想要这样做:他们可能不会点击提交按钮,就好像它已经过时了,因为他们会注意到默认浏览器装载指示器;
  • 有视力问题的人使用屏幕阅读器,无论如何也不会从图像中受益匪浅。

如今,Js 几乎被认为是理所当然的,它是一个 Web 标准。我想说的是,别担心,去做吧——没有其他方法可以做到这一点。不过,对于盲人来说,如果有一个 标签,其中包含诸如“操作可能需要几秒钟”之类的信息,那就太好了。

More than 98% of users have javascript activated. The remaining 2% is made by:

  • Utterly nerdy people who know how to disable it and want to do it: they probably won't click on the submit button as if it was going out of style, since they'll notice the default browser loading indicators;
  • People with sight problems, who use screen readers and won't benefit much from the image anyway.

Js is taken mostly for granted nowadays, and it's a web standard. I'd say don't worry and go for it - there is no other way to do it. Still, for the blind, it would be nice to have a <noscript> tag with information such as "The operation might take some seconds".

南烟 2024-12-02 04:20:02

另一种方法是在后台作业中进行处理(创建 PDF 并发送电子邮件)。因此,当您的用户单击“确认”时,该后台作业将根据表单中的输入启动,并向用户显示一条消息,表明其作业正在处理,并且他或她很快就会通过电子邮件收到确认。这样,您就可以避免完全依赖 Javascript。

你可以使用类似 Beanstalkd 的东西。

Another approach would be to do the processing (creating the PDF and sending the email) in a background job. So when your uses clicks 'Confirm', this background job is started with the input from the form and the user is shown a message that his job is being processed and he or she will receive a confirmation by email shortly. This way, you avoid relying on Javascript all together.

You could use something like Beanstalkd for this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文