如何使用 dompdf 为我的 PDF 文件生成动态文件名?

发布于 2024-12-10 12:46:58 字数 1289 浏览 0 评论 0原文

我正在使用 dompdf ,它似乎运行得很好。

我将生成的 pdf 文件作为电子邮件附件获取。我想知道如何在文件名后附加一个唯一的数字,以便每个接收 pdf 的用户都不会具有完全相同的文件名。

我目前获取文件的名称并获取已成功分配给它的任何名称,例如 myfile.pdf。

作为测试,我还可以在文件名后附加一个随机数,如下所示:

$message = Swift_Message::newInstance()
                       ->setSubject('My Subject Text') // Message subject
                       ->setTo(array('[email protected]' => 'MyName'))   // Array of people to send to
                       ->setFrom(array('[email protected]' => 'SenderName')) // From:
                       ->setBody($html_message, 'text/html') // Attach that HTML message from earlier
                       ->attach(Swift_Attachment::newInstance($pdf_content, 'myfile'.rand(10,1000).'.pdf', 'application/pdf')); // Attach the generated PDF from earlier

这对于发送带有文件名和随机数的 pdf 非常有用。

但我怎样才能用更有意义的东西来做到这一点呢?

就像从特定数字开始并为提交的每个文件自动递增一样?

或者使用基于(或来自)会话的数字?

什么是有意义的最佳实践?

我希望每个文件名都是 myfile_UNIQUENUMBER.pdf (我认为某种类型的自动增量或会话编号是最好的)

有什么想法?

I am using dompdf which seems to be going really well.

I get my generated pdf file as an email attachment. I wanted to know how to append a unique number to the file name so that every users that receives the pdf does not have the same exact file name.

I currently get name the file and get whatever I name have assigned it successfully, such as myfile.pdf.

As a test, I can also get a random number appended to the filename like this:

$message = Swift_Message::newInstance()
                       ->setSubject('My Subject Text') // Message subject
                       ->setTo(array('[email protected]' => 'MyName'))   // Array of people to send to
                       ->setFrom(array('[email protected]' => 'SenderName')) // From:
                       ->setBody($html_message, 'text/html') // Attach that HTML message from earlier
                       ->attach(Swift_Attachment::newInstance($pdf_content, 'myfile'.rand(10,1000).'.pdf', 'application/pdf')); // Attach the generated PDF from earlier

That works great for sending a pdf with the filename and random number.

But how can I do this with something that makes more sense?

Like starting with a specific number and auto-incrementing for every file that gets submitted?

Or using numbers based on (or from) the SESSION?

What is the best practice that would make sense?

I would like each file name to be myfile_UNIQUENUMBER.pdf (I think some type of auto-increment or SESSION number would be best)

Thoughts?

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

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

发布评论

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

评论(1

被翻牌 2024-12-17 12:46:58

当将日期与d/m/year和时间一起使用时,您可以使用日期作为唯一的数字,并使用您选择的名称,这样每个文件名都会有不同的数字。尝试以下代码:

  $tym = date('m-d-Y : hi a');
  $filename = ' APPLICATION '.$tym; // YOU CAN CHOOSE YOUR FILE NAME
  $message = Swift_Message::newInstance()
  ->attach(Swift_Attachment::newInstance($pdf_content,  $_POST["username"] . $filename. ".pdf", 'application/pdf')); //

You may use the date as unique number when use it with d/m/year AND TIME with a name you choose so every filename will have different number..try this code :

  $tym = date('m-d-Y : hi a');
  $filename = ' APPLICATION '.$tym; // YOU CAN CHOOSE YOUR FILE NAME
  $message = Swift_Message::newInstance()
  ->attach(Swift_Attachment::newInstance($pdf_content,  $_POST["username"] . $filename. ".pdf", 'application/pdf')); //
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文