简体   繁体   English

使用FPDF和FPDI编辑现有的PDF多页文件

[英]Edit Existing PDF multiple page File using FPDF & FPDI

I am usinf FPDI to edit my existing pdf file and its work perfect for single page. 我是FPDI的负责人,可以编辑我现有的pdf文件,它非常适合单页工作。 As ou can see i am editing my $tplIdx = $pdf->importPage(1); 如您所见,我正在编辑$tplIdx = $pdf->importPage(1); first page. 第一页。 I have six page pdf file and need to add 2 variable in different page. 我有六页pdf文件,需要在不同的页面中添加2个变量。

Is is possible ? 有可能吗? How? 怎么样?

<?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');
?>

Thanks In Advance ! 提前致谢 !

It's hard to try without FPDI installed. 没有安装FPDI很难尝试。 But the core idea would be following I believe: 但我相信以下核心思想是:

<?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');
?>

If this works you can get rid of the second block of the code and out this on a loop (and dynamic positioning as well). 如果这行得通,您可以摆脱代码的第二个块,然后循环(以及动态定位)。

Thanks @JA Your idea works for me 谢谢@JA你的想法对我有用

I just posted answer for other to help them 我刚刚发布了其他答案以帮助他们

<?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');
?>

You should really make use of the return value of setSourceFile to iterate over all pages: 您应该真正利用setSourceFile的返回值遍历所有页面:

Description 描述

public int FPDI::setSourceFile ( string $filename )

Depending on the PDF version of the used document the PDF version of the resulting document will be adjusted to the higher version. 根据所用文档的PDF版本,最终文档的PDF版本将被调整为更高版本。

Parameters 参量

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

Return Values 返回值

The number of pages in the document 文件中的页数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM