使用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

...

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

  1. IAR 下C中嵌入汇编

    最近在读<C专家编程>一书时,遇到了C中嵌入汇编的问题,刚好项目中也经常遇到这个问题,决定花时间整理一番,理清在IAR环境下的使用方法. C中嵌入汇编指令在不同的编译器下有不同的实现方式, ...

  2. html中看到php代码_如何在HTML中嵌入PHP代码

    如何在HTML中嵌入PHP代码 对于一个有经验的 PHP Web 开发者,在HTML中嵌入PHP代码是一件非常容易的事情.但是对于刚开始接触 PHP 编程语言的新手这就是一个问题.下面是小编为大家带来 ...

  3. 如何在Panel中嵌入子窗体

    文章目录 1 如何在Panel中嵌入子窗体 1.1 在Panel中嵌入子窗体的方法 1 如何在Panel中嵌入子窗体 1.1 在Panel中嵌入子窗体的方法 首先看嵌入前的效果: 点击按钮" ...

  4. 在html中如何创建空白子网页,在DreamWeaver中创建空白页

    可以创建包含预设计 CSS 布局的页面,或者先创建一个完全空白的页,然后创建自己的布局. 1.选择"文件">"新建". 2.在"新建文档&quo ...

  5. 在网页中嵌入Base64编码文件

    大家可能注意到了,网页上有些图片的src或css背景图片的url后面跟了一大串字符,比如:data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAEAAAAk ...

  6. linux强制移除pdf密码,分享|如何在 Linux 中从一个 PDF 文件中移除密码

    今天,我碰巧分享一个受密码保护的 PDF 文件给我的一个朋友.我知道这个 PDF 文件的密码,但是我不想透露密码.作为代替,我只想移除密码并发送文件给他.我开始在因特网上查找一些简单的方法来从 PDF ...

  7. 如何将PDF文件或图片或网页中的公式变为可编辑的--mathpix快速输入公式

    说在前面: PDF文件或图片或网页中的公式,有自己写文章或者做PPT需要的,如果公式很复杂,或者你就是懒.若是用latex写文章,就想直接把公式变为可编辑的latex格式,是用word写文章或者做PP ...

  8. Java 在Word中嵌入多媒体(视频、音频)文件

    Word中可将Office(Word/Excel/PowerPoint).PDF.txt等文件作为OLE对象插入到文档中,双击该对象可直接访问或编辑该文件,除了以上常见的文件格式对象,也可以插入多媒体 ...

  9. 如何Java 在Word中嵌入多媒体(视频、音频)文件

    Word中可将Office(Word/Excel/PowerPoint).PDF.txt等文件作为OLE对象插入到文档中,双击该对象可直接访问或编辑该文件,除了以上常见的文件格式对象,也可以插入多媒体 ...

最新文章

  1. Xen虚拟化之一:Xen环境组件详解
  2. python 笔记:csv 读写
  3. SpringBoot如何返回页面
  4. jibx_Jibx Jersey2集成
  5. 手写实现java中的trim_JS中字符串trim()使用示例
  6. Flash Media Server 4.5下载
  7. 四阶行列式直接展开_【Just For Fun】n 階行列式計算 宏 生成器,四阶行列式的最优展开...
  8. css中aspect,css 媒体查询 aspect-ratio less 使用方法
  9. 携程ELK日志分析平台深耕之路
  10. python程序设计第二版课后答案江红_Python核心编程第二版 第十三章课后答案
  11. MATLAB当中reshape函数以及转置的使用
  12. 神经网络学习小记录61——Tensorflow2 搭建常见分类网络平台(VGG16、MobileNet、ResNet50)
  13. 联想启天m430安装黑苹果 10500 big sur 11.6
  14. STM32中 利用PWM控制步进电机,ARR与PSC值的设定
  15. Webpack Chunk 分包规则
  16. centos 自动运行python脚本,centos配置 Python 定时任务
  17. 计算机视觉博士去向,为什么现在不看好 CV 方向了呢?
  18. 51node1006LCS
  19. 如何使用DEV-C++(超详细)
  20. HDU 3031 ToBe Or Not To Be(模拟)

热门文章

  1. 【金九银十】大专生学java好找工作
  2. 个人所得税缴纳税率是多少
  3. 一次短暂的淮安、盐城之旅
  4. 2021-04-03 java学习
  5. 《赢在蓝天碧水间》观后感
  6. 奇葩之想让我帮写代码还要白嫖
  7. php 生成带有小数的随机数
  8. 羽毛球单打和双打的有效边界区域
  9. 天使投资人徐小平:最爱理性狂热创业者
  10. ExoPlayer-三AudioRender