簡體   English   中英

使用FPDF和FPDI編輯現有的PDF多頁文件

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

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

提前致謝 !

沒有安裝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');
?>

如果這行得通,您可以擺脫代碼的第二個塊,然后循環(以及動態定位)。

謝謝@JA你的想法對我有用

我剛剛發布了其他答案以幫助他們

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

您應該真正利用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

返回值

文件中的頁數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM