2012-05-23 203 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 

返回值

的页面文件中的数字