【问题标题】:Edit Existing PDF multiple page File using FPDF & FPDI使用 FPDF 和 FPDI 编辑现有的 PDF 多页文件
【发布时间】:2012-05-23 13:57:24
【问题描述】:

我正在使用 FPDI 来编辑我现有的 pdf 文件,它的工作非常适合单页。 如您所见,我正在编辑我的$tplIdx = $pdf->importPage(1); 第一页。 我有六页的 pdf 文件,需要在不同的页面中添加 2 个变量。

有可能吗?怎么样?

<?php
require_once('fpdf.php');
require_once('fpdi.php');

// initiate FPDI
$pdf = new FPDI();
// add a page
$pdf->AddPage();
// set the sourcefile
$pdf->setSourceFile('ex.pdf');

// import page 1
$tplIdx = $pdf->importPage(1);


// use the imported page and place it at point 10,10 with a width of 100 mm
$pdf->useTemplate($tplIdx, 10, 10, 200);

// now write some text above the imported page
$pdf->SetFont('Arial');
$pdf->SetTextColor(255,0,0);
$pdf->SetXY(50, 50);
$pdf->Write(0, "Ajay Patel");

$pdf->Output('newpdf1.pdf', 'D');
?>

提前致谢!

【问题讨论】:

  • @JA 我已经导入了 6 页 pdf 文件并且输出是单页,我想要所有页面

标签: php fpdf fpdi


【解决方案1】:

没有安装 FPDI 很难尝试。但我相信核心思想如下:

<?php

  require_once('fpdf.php');
  require_once('fpdi.php');

  // initiate FPDI
  $pdf = new FPDI();

  /* <Virtual loop> */
  $pdf->AddPage();
  $pdf->setSourceFile('ex.pdf');
  $tplIdx = $pdf->importPage(1);

  $pdf->useTemplate($tplIdx, 10, 10, 200);

  // now write some text above the imported page
  $pdf->SetFont('Arial');
  $pdf->SetTextColor(255,0,0);
  $pdf->SetXY(50, 50);
  $pdf->Write(0, "Ajay Patel");

  /* </Virtual loop/> */

  $pdf->AddPage();
  //$pdf->setSourceFile('ex.pdf');
  $tplIdx = $pdf->importPage(2);

  $pdf->useTemplate($tplIdx, 10, 10, 200); // dynamic parameter based on your page

  $pdf->SetFont('Arial');
  $pdf->SetTextColor(255,0,0);
  $pdf->SetXY(50, 50);
  $pdf->Write(0, "Ajay Patel2");

  $pdf->Output('newpdf1.pdf', 'D');
?>

如果这可行,您可以去掉第二个代码块,然后循环输出(以及动态定位)。

【讨论】:

  • 会尽快检查并通知您
  • @J A 它正在创建一个空白 PDF。我应该检查什么?
【解决方案2】:

谢谢@J A 你的想法对我有用

我刚刚发布了其他人的答案以帮助他们

<?php
require_once('fpdf.php');
require_once('fpdi.php');

// initiate FPDI
$pdf = new FPDI();
// add a page
$pdf->AddPage();
// set the sourcefile
$pdf->setSourceFile('newpdf.pdf');

// import page 1
$tplidx = $pdf->importPage(1);
for ($i = 1; $i < 6; $i++) { 
              $tplidx = $pdf->ImportPage($i); 


                     $pdf->useTemplate($tplidx, 10, 10, 200);
                     $pdf->AddPage();

                     $pdf->SetFont('Arial');
                     $pdf->SetTextColor(0,0,0);
                     $pdf->SetFontSize(8);

                     if ($i==3) {
                        $pdf->SetXY(50, 124);
                        $pdf->Write(1, "Ajay Patel");

                        $pdf->SetXY(50, 133);
                        $pdf->Write(1, date("d/m/Y"));
                     }

                     if ($i==4) {
                        $pdf->SetXY(50, 171);
                        $pdf->Write(1, "Ajay Patel");

                        $pdf->SetXY(50, 185);
                        $pdf->Write(1, date("d/m/Y"));
                     }

                }

$pdf->Output('newpdf1.pdf', 'D');
?>

【讨论】:

【解决方案3】:

你真的应该利用 setSourceFile 的返回值来遍历所有页面:

说明

public int FPDI::setSourceFile ( string $filename )

根据所用文档的 PDF 版本,生成的文档的 PDF 版本将调整为更高版本。

参数

$filename : string // A valid path to the PDF document from which pages should be imported from

返回值

文档的页数

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多