2012-05-23 204 views
1

我使用FPDI來編輯我現有的PDF文件,並且它的工作完美適用於單頁。 正如你可以看到我正在編輯我的$tplIdx = $pdf->importPage(1);首頁。 我有六頁PDF文件,需要在不同頁面添加2個變量。編輯現有PDF多頁使用FPDF和FPDI的文件

可以嗎?怎麼樣?

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

在此先感謝!

+0

@JA我已經導入6頁的PDF文件,並輸出單頁,我希望所有的頁面 –

回答

11

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

如果這個作品可以擺脫代碼的第二塊的進出該環路上(和動態定位以及)。

+0

將檢查它讓你很快知道 –

2

感謝@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'); 
?> 
0

你真的應該利用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 

返回值

的頁面文件中的數字