php中嵌入pdf文件,使用Base64在PHP中附加PDF文件(Appending PDF Files in PHP with Base64)

使用Base64在PHP中附加PDF文件(Appending PDF Files in PHP with Base64)

我有一系列我想合并在一起的base64 PDF文件。 目前我正在使用file_get_contents(),并且使用PHPMailer可以分别附加它们中的每一个。

$woFile = file_get_contents($url);

$invoiceFile = file_get_contents($invPDF64);

$tsFile = file_get_contents($tsPDF64);

...

$mail->AddStringAttachment($woFile, "1.pdf", "base64", "application/pdf");

$mail->AddStringAttachment($invoiceFile, "2.pdf", "base64", "application/pdf");

$mail->AddStringAttachment($tsFile, "3.pdf", "base64", "application/pdf");

我在网上看到的所有例子如FPDF都需要在本地下载文件,至少从我看到的内容开始。 有没有办法将每个PDF文件附加到一个文件中,然后将其附加到电子邮件中?

提前致谢!

I have a series of base64 PDF Files that I would like to merge together. Currently I am using file_get_contents() and with PHPMailer can attach each of them separately.

$woFile = file_get_contents($url);

$invoiceFile = file_get_contents($invPDF64);

$tsFile = file_get_contents($tsPDF64);

...

$mail->AddStringAttachment($woFile, "1.pdf", "base64", "application/pdf");

$mail->AddStringAttachment($invoiceFile, "2.pdf", "base64", "application/pdf");

$mail->AddStringAttachment($tsFile, "3.pdf", "base64", "application/pdf");

All the examples I've seen online such as FPDF require the file to be locally downloaded, at least from what I saw. Is there a way to append each of these PDF files into one, and then have that attached to the email?

Thanks in advance!

原文:https://stackoverflow.com/questions/46959350

更新时间:2020-01-25 05:25

最满意答案

我不确定您是否需要将PDF合并为一个PDF,或者您只需要一个文件。 以下是两者的选项:

如果要将所有PDF合并为单个PDF文件,则这是一个重复的问题 。 您提到不想拥有本地文件,但这可能是一个不合理的约束(例如,大型PDF的内存问题)。 根据需要使用临时文件并自行清理。

如果您只想要一个文件,请考虑将这些文件放入ZIP压缩文件并发送该文件。 您可能也喜欢ZipStream库用于此目的。 这是使用本机库的一些最小代码:$attachmentArchiveFilename = tempnam('tmp', 'zip');

$zip = new ZipArchve();

# omitting error checking here; don't do it in production

$zip->open($attachmentArchiveFilename, ZipArchve::OVERWRITE);

$zip->addFromString('PDFs/first.pdf', $woFile);

$zip->addFromString('PDFs/second.pdf', $invoiceFile);

$zip->addFromString('PDFs/third.pdf', $tsFile);

$zip->close();

$mail->addAttachment($attachmentArchiveFilename, 'InvoicePDFs.zip');

# be sure to unlink/delete/remove your temporary file

unlink( $attachmentArchiveFilename );

I'm not sure if you specifically need to merge the PDFs into one PDF, or if you just want one file. Here are options for both:

If you want to merge all PDFs into a single PDF file, then this is a duplicate question. You mention not wanting to have a local file, but this may be an unreasonable constraint (e.g., memory issues with large PDFs). Use temporary files as appropriate and clean up after yourself.

If you just want a single file, consider putting the files into a ZIP archive and sending that. You might also like the ZipStream library for this purpose. Here's some minimal code using the native library: $attachmentArchiveFilename = tempnam('tmp', 'zip');

$zip = new ZipArchve();

# omitting error checking here; don't do it in production

$zip->open($attachmentArchiveFilename, ZipArchve::OVERWRITE);

$zip->addFromString('PDFs/first.pdf', $woFile);

$zip->addFromString('PDFs/second.pdf', $invoiceFile);

$zip->addFromString('PDFs/third.pdf', $tsFile);

$zip->close();

$mail->addAttachment($attachmentArchiveFilename, 'InvoicePDFs.zip');

# be sure to unlink/delete/remove your temporary file

unlink( $attachmentArchiveFilename );

2017-10-26

相关问答

我了解到,Zendesk应用程序框架对请求使用jQuery AJAX包装,并且阵列缓冲区类型不受支持,因此文件已损坏。 应用程序框架团队解决了这个问题。 I learned that the Zendesk app framework uses a jQuery AJAX wrapper for requests and the arraybuffer type is unsupported, so the file was getting corrupted. The app framework

...

编码片段不正确,它以“文本”模式打开pdf文件。 根据文件大小,您只需以二进制模式打开文件并使用编码字符串方法示例: def pdf_encode(pdf_filename):

return open(pdf_filename,"rb").read().encode("base64");

或者如果文件大小很大,你可能不得不将编码分解成块,但没有查看是否有模块这样做但是它可以像下面的示例代码一样简单: def chunk_24_read(pdf_filename) :

with

...

以下是您可以尝试的内容: handleUploadFile(event) {

let selectedFile = event.target.files;

let file = null;

let fileName = "";

//Check File is not Empty

if (selectedFile.length > 0) {

// Select the very first file from list

let

...

base64_encode采用字符串输入。 所以你所做的只是编码路径。 你应该抓住文件的内容 $b64Doc = chunk_split(base64_encode(file_get_contents($this->pdfdoc)));

base64_encode takes a string input. So all you're doing is encoding the path. You should grab the contents of the file $b64Doc = ch

...

标题用于向客户端提供数据 - 您不需要这些。 只需使用file_put_contents 至于文件保存位置,您知道将保存到用户配置文件中的文件夹中吗? 如果您想要当前文件夹,请使用. 而不是~ 。 Headers are for serving data to the client - you don't need these. Just use file_put_contents As for the file save location, you know that will save int

...

这里的例子似乎很有帮助: http : //php.net/manual/en/function.readfile.php 在你的情况下: <?php

$decoded = base64_decode($base64);

$file = 'invoice.pdf';

file_put_contents($file, $decoded);

if (file_exists($file)) {

header('Content-Description: File Transfer');

...

我不确定您是否需要将PDF合并为一个PDF,或者您只需要一个文件。 以下是两者的选项: 如果要将所有PDF合并为单个PDF文件,则这是一个重复的问题 。 您提到不想拥有本地文件,但这可能是一个不合理的约束(例如,大型PDF的内存问题)。 根据需要使用临时文件并自行清理。 如果您只想要一个文件,请考虑将这些文件放入ZIP压缩文件并发送该文件。 您可能也喜欢ZipStream库用于此目的。 这是使用本机库的一些最小代码: $attachmentArchiveFilename = tempnam('tm

...

我很确定,它的发生,因为你没有退出你的功能,它仍然填满缓冲区。 您必须在流准备就绪后立即停止脚本,清理缓冲区并退出。 如果它是一个有效的Excel文件,所有你需要做的是: header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');

header('Cache-Control: must-revalidate');

header('Expires: 0');

header('Pr

...

最好不要自己构建JSON字符串,而是让json_encode完成工作,这需要处理斜杠。 那你不需要addlashes : // create the object

$obj = array(

"content" => chunk_split(base64_encode($file_contents))

);

// encode the object in JSON format

$json = json_encode($obj);

请注意,使用chunk_split插入的新行字符将在编码

...

最后,我先前解码了我的base64 pdf文件并将解码后的字符串保存到变量中,并在附件属性中设置了该变量。 顺便说一下,我使用过PHP Mailer <?php

$_SESSION["sesDataPDF"] = $_POST["hdn_UriPdf"];

$b64file = $_SESSION["sesDataPDF"];

$decodPdf;

if ($b64file){

$nomPdf = ;

$nomPdf .= ".pdf";

$nomPdf = (string

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值