【问题标题】:how to add images to multiple pages on pdf and download the full pdf如何将图像添加到pdf上的多个页面并下载完整的pdf
【发布时间】:2020-03-26 12:53:52
【问题描述】:

我开始使用 fpdf 与 fpdi 一起工作,并尝试将多个图像添加到多个页面,最后,我想下载一个 PDF,其中包含 PDF 页面上的图像。 问题是总是只有最后一页下载的最后一个 PDF。为什么我不能下载一个包含所有图像的文件?

foreach ($signatures as $signa) {
  $fileContent = file_get_contents('http://www.africau.edu/images/default/sample.pdf','rb');
  $pageCount = $pdf->setSourceFile(StreamReader::createByString($fileContent));
  $pdf->setSourceFile(StreamReader::createByString($fileContent));
  $tplId = $pdf->importPage($signa->page);
  $pdf->useTemplate($tplId, 10, 10, 100);
  $pdf->Image('signature.jpg', $signa->position->x, $signa->position->y, $signa->size->width, $signa->size->height);

  if($signa->page === 2) {
    $pdf->Output('D');   
  }
}

【问题讨论】:

  • 除非您将 PDF 创建为文件,然后在完成创建后下载它,否则您将无法向浏览器发送多个 PDF。
  • 好的,我怎样才能等待它完成所有然后下载完整文件? @戴夫
  • 更改您的输出语句以写入文件,然后使用适当的标题和readfile 将新创建的文件发送到浏览器。
  • 你没有例子吗? @戴夫
  • 恐怕这不是 SO 的工作方式。 FPDF 的文档显示了output 的选项以及如何将文件发送到浏览器很容易搜索(使用 Google 或在 SO 上)。这很简单,因为您似乎已经拥有了大部分所需的东西。

标签: php pdf fpdf fpdi


【解决方案1】:

我发现了这个 Solution 及其对我的工作。

我的代码的解决方案:

$pdf = new Fpdi();
foreach ($signatures as $signa) {
$pdf->AddPage();
$fileContent = file_get_contents('http://www.africau.edu/images/default/sample.pdf','rb');
$pdf->setSourceFile(StreamReader::createByString($fileContent));
$tplId = $pdf->importPage($signa->page);
$pdf->useTemplate($tplId, 10, 10, 100);
$pdf->Image('signature.png', $signa->position->x, $signa->position->y, $signa->size->width, $signa->size->height);
}
$pdf->Output('newpdf1.pdf', 'D');   

【讨论】:

  • 您应该将file_get_contents() 移出循环!并删除第二个参数。如果这是本地文件的 URI,则应使用本地路径!
猜你喜欢
  • 2020-02-19
  • 1970-01-01
  • 2014-03-21
  • 1970-01-01
  • 1970-01-01
  • 2010-11-26
  • 1970-01-01
  • 2012-01-11
相关资源
最近更新 更多