【问题标题】:If Possible to Read existing PDF content position and add the some content after over the existing content in laravel?如果可以读取现有的 PDF 内容位置并在 laravel 中的现有内容之后添加一些内容?
【发布时间】:2020-11-09 06:59:44
【问题描述】:

Laravel 包: "setasign/fpdi": "^2.3", "setasign/fpdf": "^1.8"

$pdf = new \setasign\Fpdi\Fpdi('L','mm','A4');
$pageCount = $pdf->setSourceFile(public_path().'/'.$url);
$pdf->setFont('Arial', 'B', 10);
for($i = 1; $i <= $pageCount; $i++){
    $tplIdx = $pdf->importPage($i);
    $pageDimensions = $pdf->getImportedPageSize($tplIdx);
    $pdf->addPage($pageDimensions['orientation'], $pageDimensions);
    $pdf->useTemplate($tplIdx);
 }

如果可能的话,读取最后一页的内容,并得到当前位置的后页内容纵坐标。然后编写新内容而不添加新页面或空格

【问题讨论】:

  • 基本上你想附加到以前的pdf
  • 是的,我需要最后一行位置。
  • FPDI 不支持访问页面内容。
  • 但是在这段代码中pageCount工作正常,只需要最后一行的位置,然后在写入新内容后
  • “最后一行位置”是(如果我理解正确的话)通过呈现导入页面的文本来解决的信息。 FPDI 不支持。

标签: laravel pdf laravel-5 fpdf fpdi


【解决方案1】:

如果使用 DomPdf 生成 Pdf,那么您可以使用生成 HTML 文件

$pdf->getDomPDF()->outputHtml()

使用 DomPDF 生成的 html 文件添加如下元素:

&lt;div id="divId"&gt;&lt;/div&gt;

那么每当你需要编辑这个文档时,你需要逐行读取 HTML 文件并 编辑文档:

$url=public_path() .'/test.html';
$htmlFile='';
foreach(file($url) as  $key => $line){
  if(strpos($line, 'id="divId"') !== false){
    $line='<div>Hello world</div>';
  }
  $htmlFile.=$line;
}
$pdf = PDF::loadHTML($htmlFile)->setPaper('a4', 'landscape');
$pdf->output();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-29
    • 2011-03-21
    • 2015-07-15
    相关资源
    最近更新 更多