在作为电子邮件附件发送之前验证 PDF

发布于 2024-09-24 02:42:12 字数 228 浏览 1 评论 0原文

我使用 FDPF 为客户生成发票,然后将其附加到电子邮件中并发送给客户。发票/电子邮件是批量生成的(一次数百个)。在该批次的第一次实际运行中,少数(大约 200 名中的 5 名)客户收到了损坏的 PDF。他们之间的共同点是他们的发票比平均水平要大,这让我相信生成发票所需的时间导致了竞争条件,并且可能在 PDF 有时间完成生成之前发送了电子邮件完全地。有没有办法在发送电子邮件之前验证 PDF 没有损坏?或者还有其他方法来解决我忽略的问题吗?

I'm using FDPF to generate invoices for customers, which are then attached to an e-mail and sent along to the customer. The invoices / emails are generated in a batch (several hundred at a time). In the first real world run of the batch, a handful (about 5 out of 200) of customers received corrupt PDFs. The common link between them was that they had larger invoices than average, which leads me to believe that the time it takes to generate the invoice is causing a race condition and perhaps the e-mail is being sent before the PDF has time to finish generating completely. Is there a way to validate that the PDF is not corrupt before sending the e-mail? Or is there another way to approach the problem that I'm overlooking?

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

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

发布评论

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

评论(2

淡水深流 2024-10-01 02:42:12

您可以通过在第一次发送文件时创建 pdf 的 md5 哈希来测试 pdf 是否已完全生成,然后在发送电子邮件时再次创建,最后在发送后创建。如果 md5 每次都发生变化,则在发送电子邮件时,pdf 生成器仍在创建该文件。

以下是如何使用 md5 哈希值的示例:

<?php

$file_name = 'md5_demonstration_file';


    $file_changer = 0;

    while($file_changer < 10)
    {
     file_put_contents($file_name, $file_changer);
     echo md5_file ($file_name) . '</br>';
     $file_changer++;
    }

    ?>

您会注意到 md5 哈希值在函数的每次迭代中都会发生变化,因为文件仍在写入中。如果您尝试此示例代码,您可能必须手动设置“md5_demonstration_file”的权限,以便任何人都可以对其进行写入。

如果竞争条件不是问题,您是否已阅读此建议线程: 由 FPDF 和 PHP 生成时损坏的 PDF 电子邮件附件

You can test if the pdf is fully generated by creating an md5 hash for the pdf at the time the file is first sent, and then again while the email is sending, and finally after it has been sent. If the md5 changes each time, then the file is still being created by the pdf generator while the email is sending.

Here's an example on how to use md5 hash:

<?php

$file_name = 'md5_demonstration_file';


    $file_changer = 0;

    while($file_changer < 10)
    {
     file_put_contents($file_name, $file_changer);
     echo md5_file ($file_name) . '</br>';
     $file_changer++;
    }

    ?>

You will notice that the md5 hash changes on each iteration of the function because the file is still being written. If you try this sample code, you may have to set the permissions on 'md5_demonstration_file' manually so that anyone can write to it.

If race condition isn't the problem have you read this SO suggested thread: Corrupt PDF email attachment when generated by FPDF and PHP

王权女流氓 2024-10-01 02:42:12

请注意,最新版本的 Acrobat Reader 在读取不完全符合正确 pdf 格式的文件时更加挑剔。

pdf 文档的开头由字符串 %PDF 指示。旧版本的 Acrobat 会忽略 %PDF 标记之前出现的文件内容。例如,您可能在文件中留下了一些调试输出,如下所示:

debug line 1
debug line 2
%PDF-1.4
3 0 obj
<</Type /Page
etc.

Acrobat 会正常打开该输出(与 OSX 上的预览一样)。

Acrobat 不会再接受这一点。

我被咬了,所以希望这会有所帮助!

Be aware that the latest version of the Acrobat reader is fussier about reading files that don't conform exactly to the correct pdf format.

The start of a pdf document is indicated by the string %PDF. Older versions of Acrobat would ignore file content occurring before the %PDF marker. For example, you might have left some debug output in the file, as follows:

debug line 1
debug line 2
%PDF-1.4
3 0 obj
<</Type /Page
etc.

and Acrobat would open that ok, (as will Preview on OSX).

Acrobat won't accept that anymore.

I got bitten by that, so hope this helps!

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