简体   繁体   English

使用TCPDF即时生成PDF

[英]generating PDF on the fly using TCPDF

i'm using TCPDF library to generate a PDF (bill) on the fly and send it via email. 我正在使用TCPDF库动态生成PDF(清单)并通过电子邮件发送。 It all works but i have a weird problem. 一切正常,但我有一个奇怪的问题。 When i send the email to a gmail account everything is fine, but when i send it to my mail server i get the email with the pdf but when i open it it doesn't open and i get a message "Adobe reader could not open file.pdf because it's either not a supported format or because the file has been damaged." 当我将电子邮件发送到gmail帐户时,一切都很好,但是当我将其发送到我的邮件服务器时,我收到了带有pdf的电子邮件,但是当我打开它时,它没有打开,并且收到消息“ Adob​​e Reader无法打开file.pdf,因为它不是受支持的格式,或者因为文件已损坏。” (the pdf in the email is blank). (电子邮件中的pdf为空白)。

I save the PDF into a string like so: 我将PDF保存到这样的字符串中:

$attachment = $pdf->Output("mypdf.pdf","E");
$attachment = chunk_split($attachment);

and send it via email like so: 并通过电子邮件发送,如下所示:

 $header .= "--".$separator.$eol; 
 $header .= "Content-Type: application/pdf; name='mypdf.pdf'".$eol;
 $header .= "Content-Transfer-Encoding: base64".$eol;
 $header .= "Content-Disposition= attachment".$eol.$eol;
 $header .= $attachment;

i'm sending the email with php mail function. 我正在用php邮件功能发送电子邮件。

The funny thing is if i force the download of the pdf, like so: 有趣的是,如果我强制下载pdf,就像这样:

$attachment = $pdf->Output("mypdf.pdf","D"); $ attachment = $ pdf-> Output(“ mypdf.pdf”,“ D”);

the file is OK and opens without a problem! 该文件没有问题,可以正常打开! But if i change it back to "E" it doesn't work. 但是,如果我将其更改回“ E”,则它将不起作用。

The other weird thing is that some times i can open the pdf (that i got on my mail server) without a problem, but the next time it wont work (even if i send the exact same email). 另一个怪异的事情是,有时我可以打开pdf(我在邮件服务器上获得的)而没有问题,但是下次它将无法工作(即使我发送的电子邮件完全相同)。

Does any one have any idea what is going on? 有谁知道发生了什么吗? I would like to avoid saving the pdf on the local server. 我想避免将pdf保存在本地服务器上。

Why you avoid saving pdf file? 为什么要避免保存pdf文件? You can save it with "F" parameter. 您可以使用"F"参数保存它。 After mail has sent you can delete it with unlink function 邮件发送后,您可以使用unlink功能将其删除

I see some minor flaws: 我看到一些小缺陷:

  • Content-Disposition= should be Content-Disposition: Content-Disposition=应为Content-Disposition:
  • You should end your attachment with "--".$separator."--" 您应该以"--".$separator."--"结束"--".$separator."--"附件"--".$separator."--"

I'm not sure, if this fixes your problems. 我不确定,是否可以解决您的问题。 Anyway I think it's quite complicated to create all the headers manually. 无论如何,我认为手动创建所有标头非常复杂。 I use PEARs Mail_Mime for this task since years and you will find a lot of simple ready to use solutions. 多年来,我一直使用PEARs Mail_Mime进行此任务,您会发现很多简单易用的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM