从 TCPDF 中的 base64 字符串中删除"E"输出标头



我使用使用TCPDF

$base64String = $pdf->Output('file.pdf', 'E');

所以我可以通过AJAX 发送数据

唯一的问题是,除了Base64字符串之外,它还附带了标头信息

Content-Type: application/pdf;
name="FILE-31154d59f28c63efae86e4f3d6a00e13.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="FILE-31154d59f28c63efae86e4f3d6a00e13.pdf"

因此,如果我将创建的字符串转换为base64_decode((,或者在我的情况下与phpMailer一起使用,它就会出错。是否可以删除标题,使我只有base64字符串?

(错误是打开pdf时任何pdf阅读器都无法读取pdf(

我以为我能找到解决这个问题的方法,但我什么都没找到!!

更新

这就是我为解决问题而采取的措施

$base64String = preg_replace('/Content-[sS]+?;/', '', $base64String);
$base64String = preg_replace('/name=[sS]+?pdf"/', '', $base64String);
$base64String = preg_replace('/filename=[sS]+?"/', '', $base64String);

但是它不是很优雅!因此,如果有人有更好的解决方案,请在下面发布:(

TCPDF文档庞大但不可用——直接读取源代码更容易。它有这些额外的头,因为您使用E输出模式来请求它们,该模式用于生成电子邮件。

要将PDF数据作为PHPMailer附件发送,您需要将直接的二进制PDF数据作为字符串,如S输出模式所提供的,您可以将其直接传递到addStringAttachment(),PHPMailler将为您处理所有编码。你所要做的就是:

$mail->addStringAttachment($pdf->Output('file.pdf', 'S'), 'file.pdf');

要将PDF二进制文件转换为base64,例如在JSON字符串中,只需将其通过base64_encode:

$base64String = base64_encode($pdf->Output('file.pdf', 'S'));

最新更新