I am trying to find out if it is possible to edit/add to an existing PDF file with PHP. I have a PDF agreement document that needs to have the client's name inserted on the blank field. The document will then be printed for their signature. I've created PDF's with PHP, but would like to just simply add the name without creating the entire document, if at all possible.

If this is possible, can someone please direct me to a tutorial, web site, book or include a simple snippet to help?

Thanks,
Pasquale

Recommended Answers

All 8 Replies

ya same like that also to me

i want to do is that i have a ready pdf file with form fields like textbox, checkbox etc...

and a submit button by which i can send data to database using php

but i also want to edit that data after if user click on edit pdf link and the pdf form should should be display that data from database which are stored before.

is it possible to display data from database in pdf form fields same as we can display in html form fields?

I have not played with it yet, I'm sure the functionality is limited, but I think you may be able to add, insert and possibly delete pages. You may be able to do more with it too, not quite sure, but check this out http://www.daniweb.com/forums/thread133021.html

pdftk is a command line tool, which is great if your host will let you put that sort of thing on his server. Other options within php are fpdi and tcpdf used with fpdf. FPDF is the baseline tool (fpdf.org) and the others build on it, including a facility to get specific pages from a pdf, treat them like an image in a pdf, and then add to that with your text using the standard fpdf functions.

I am trying to edit file using fpdf/fpdi

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

$pdf =& new FPDI();
$pdf->AddPage();

//Set the source PDF file
$pagecount = $pdf->setSourceFile("benchbook.pdf");

//Import the first page of the file
$tpl = $pdf->importPage($i);
//Use this page as template
$pdf->useTemplate($tpl);

#Print Hello World at the bottom of the page

//Go to 1.5 cm from bottom
$pdf->SetY(-15);
//Select Arial italic 8
$pdf->SetFont('Arial','I',8);
//Print centered cell with a text in it
$pdf->Cell(0, 10, "Hello World", 0, 0, 'C');

$pdf->Output("my_modified_pdf.pdf", "F");
?>

Maybe doing it with PHP isn't the easiest / best way to do it. You can create editable pdf files with the full Adobe package.
Click Here. This is supposed to work online. The (free) reader can accept input to form fields but won't save them. That may still be enough if all you want to do is immediately print it.

I recommend FPDF for this. Here is some sample code extracted from a project I did recently that filled out a complex employment application based on a web form. This won't quite run without adjustments but it should get you on the right track and includes a helper class to make printing text one method rather than two, and to allow for rotated text.

I've also attached a file that contains the lib/fpdf folder, including a functional font file (that was a bit tricky if I remember correctly).

<?php

require_once 'lib/fpdf/fpdf.php';
require_once 'lib/fpdf/fpdi.php';

class X_FPDI extends FPDI {
    var $angle=0;

    public function text($x, $y, $str) {
        $this->SetXY($x, $y);
        $this->Write(3, $str);
    }

    function RotatedText($x,$y,$txt,$angle)
    {
        //Text rotated around its origin
        $this->Rotate($angle,$x,$y);
        $this->Text($x,$y,$txt);
        $this->Rotate(0);
    }

    function Rotate($angle,$x=-1,$y=-1)
    {
        if($x==-1)
            $x=$this->x;
        if($y==-1)
            $y=$this->y;
        if($this->angle!=0)
            $this->_out('Q');
        $this->angle=$angle;
        if($angle!=0)
        {
            $angle*=M_PI/180;
            $c=cos($angle);
            $s=sin($angle);
            $cx=$x*$this->k;
            $cy=($this->h-$y)*$this->k;
            $this->_out(sprintf('q %.5F %.5F %.5F %.5F %.2F %.2F cm 1 0 0 1 %.2F %.2F cm',$c,$s,-$s,$c,$cx,$cy,-$cx,-$cy));
        }
    }

    function _endpage()
    {
        if($this->angle!=0)
        {
            $this->angle=0;
            $this->_out('Q');
        }
        parent::_endpage();
    }
}

$filename = APPPATH.'cache/entry_'.$entry->form_entry_id.'.pdf';
$source_filename = __DIR__.'/resources/employment_application_form.pdf';        // Template PDF to fill in

$pdf = new X_FPDI();
$pdf->AddPage();
$pdf->setSourceFile($source_filename); 
$pdf->AddFOnt('couriernewpsmt', '', 'couriernewpsmt.php');
$pdf->SetFont('couriernewpsmt');
$pdf->SetTextColor(0, 0, 0);
$pdf->SetLeftMargin(34);
$pdf->SetRightMargin(0);


/***** Cover *****/

$tplIdx = $pdf->importPage(1);  // Grab a page of the template
$pdf->useTemplate($tplIdx, -215, 0, 0, 0, true);      // Move the background over (two up pages in template)
// Comment out the above line and uncomment this line if the background is blank:
//$pdf->useTemplate($tplIdx, 0, 0, 0, 0, true);

// $pdf->RotatedText(180, 60, $entry->last_name.'/'.$entry->first_name.' '.$entry->middle_name,-90);

$pdf->text(30, 20, "*************** ONLINE APPLICATION ***************");
$pdf->text(30, 25, 'test');
$pdf->text(30, 30, "**************************************************");
$pdf->text(30, 35, 'Position: '.'test');
$pdf->text(30, 40, 'Date: '.'00-00-0000');

$pdf->AddPage();

// Reset for normal pages, needed after using a custom margin
$pdf->SetLeftMargin(34);
$pdf->SetRightMargin(0);

/***** Page 2*****/

$tplIdx = $pdf->importPage(2);  // Import next page of template
$pdf->useTemplate($tplIdx, 0, 0, 0, 0, true); // Use this line insetad of the one with -215 in it if the page is normal
$pdf->SetLeftMargin(25);    // To prevent text from wrapping to the left of the page

$pdf->text(25, 24, 'Field 1');
$pdf->text(75, 24, 'Field 2');
$pdf->text(120, 24, 'Field 3');

$pdf->text(25, 32, $entry->other_names_employed_other_maiden);
$pdf->text(141, 32, $entry->email);


$pdf->AddPage();    // Always call AddPage to end the previous page, before a call to importPage to start the next one

/***** Page 3 *****/

$tplIdx = $pdf->importPage(2);  // Import next page
$pdf->useTemplate($tplIdx, -220, 0, 0, 0, true);    // Move the background over (two up pages in template)

$pdf->SetLeftMargin(40);


$pdf->Output($filename,'F');
return $filename;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.