Add Header to PDF File

I’m working on a website which will process a lot of pdf files and publish them. Before a pdf can be published a header containing the site information and page numbers must be added to the top of the pdf file. Is it even possible to write a script to automate this task? Or does this have to be done manually? I can’t think of any way to do this off the top of my head so i appreciate any suggestions.

I’m looking back on this problem and thought I’d bump this topic back up for discussion.
I know its possible to dynamically generate pdfs, but is it possible to take an existing pdf and simply attach a header to the top and footer to the bottom. The header and footer will contain dynamic information (such as id numbers) generated by my web app.

I found this software:
http://www.pdflib.com/

However, at $1000 for a license it is well out of my price range.

I’ve never worked with pdf’s programatically, but glancing here http://www.php.net/pdf there seems to be some functions with the name page in them. Maybe you can insert a page, or make a new pdf, insert your page, and then copy all the pages of the other pdf over.

Ya I just found two pdf classes that I’m assuming take advantage of those php functions.

Anyone have any experience with either FPDF or [URL=“http://www.tecnick.com/public/code/cp_dpage.php?aiocp_dp=tcpdf”]TCPDF?

I’m assuming somewhere in these tools are the necessary functionality to do what i need to do. Its just going to be tough to figure it out because i can’t seem to find anyone else who has tried to do the same thing and posted a solution.

FPDF seems to be a good solution and it has a extension which allows you to import pages from an existing PDF document. I’ll have to mess around with it an see what I can come up with.

I’m working on using the tcpdf class along with the fpdi class to accomplish this task but i’m having quite a bit of difficult. My goal as mentioned above is to import a pdf and add a header to the pdf. I’ve set it up so that I can import the pdf and have setup a header. However, I can’t quite get it to work right.

One major problem i’ve run into is that fpdi is not able to open up most pdfs. I just get an error: “Cannot open file.pdf !”. I’m assuming that this has something to do with security features in pdf documents. I’ve tried a wide array of pdfs and the only ones that I can seem to import our pdfs that have been created with the tcpdf class. This of course is not helpful to me because I need to be able to import pdfs that are made by other people using some sort of pdf generating program that will not be tcpdf. Any suggestions on getting the pdfs to load?

Another problem is that the page i import from the pdf overlaps the header and I can’t seem to find the right place to change the margins to push the page down.

Here is the code i have so far (in addition to the standard class files):

<?php
// just require TCPDF instead of FPDF
require_once('../tcpdf.php');
require_once('../fpdi.php');

$pdf = new FPDI;

$pagecount = $pdf->setSourceFile('pages.pdf');

$w = 192;
$h = 0;

$_x = $x = 10;
$_y = $y = 10;

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Name');
$pdf->SetTitle('Title');
$pdf->SetSubject('Subject');
$pdf->SetKeywords('keyword,keyword');

// set default header data
/*$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);*/

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

//set some language-dependent strings
$pdf->setLanguageArray($l);

// ---------------------------------------------------------

// set font
$pdf->SetFont('times', 'BI', 16);


$pdf->addPage();
for ($n = 1; $n <= $pagecount; $n++) {
    $tplidx = $pdf->ImportPage($n);

    $size = $pdf->useTemplate($tplidx, $x, $y, $w);
    $pdf->Rect($x, $y, $size['w'], $size['h']);
    $h = max($h, $size['h']);
    if ($n % 2 == 0) {
        $y += $h+10;
        $x = $_x;
        $h = 0;
    } else {
        $x += $w+10;
    }

    if ($n != $pagecount) {
        $pdf->AddPage();
        $x = $_x;
        $y = $_y;
    }
}


$pdf->Output('newpdf.pdf', 'I');
?>

Thanks for you help.

FPDF is a simple way to create PDFs entirely from scratch.

To modify an existing PDF take a look at FPDI at http://www.setasign.de/products/pdf-php-solutions/fpdi/ - it is an FPDF add-on that can take an existing PDF as the starting point.

As you can see above i’m already using fpdi. Except i’m using it with tcpdf instead of fpdf. It is still handled the same way, there is just a bridge file to convert the tcpdf to use the fpdf functions if that makes sense.

Another problem to consider. I’ve discovered that when using fpdi to import pdfs the mass-majority of pdfs cannot be imported. I’m not an expert on pdfs but i’m assuming this occurs b/c the default creation settings for pdf software includes a basic level of encryption/protection.
I see that fpdi offers a pro account for about $400 U.S. dollars which allows you to import/export encrypted pdfs. But does anyone know of any other options out there? Its looking like i’m just going to have to spend the money.

Did you solve your problem? I’m interested in hearing about it… I have the same problem you had 2-3 years ago. Hopefully, time has given you wisdom. :magic:

Hi,
I got it working today :D.

My setup is:
• XAMPP for Windows - 1.7.3.0

o PHP - 5.3.1

o MySql Client API version - 5.1.41

• TCPDF - 5.9.046
• FPDI – 1.4
• FPDF_TPL - 1.2

I downloaded FPDF_TPL and put it in \xampp\php\PEAR. That was pretty much it. It works like a dream. I got this small test program from another forum (try it):

<?php
require_once('TCPDF/tcpdf.php');
require_once('FPDI/fpdi.php');

$pdf = new FPDI();

$pdf->AddPage();

$pdf->setSourceFile('existing_file.pdf');
$tplIdx = $pdf->importPage(1);
$pdf->useTemplate($tplIdx);

$pdf->SetFont('Courier', '', 10);

$pdf->SetXY(20, 10);
$pdf->Write(0, "Text to be stamped");

$pdf->Output("output_sample.pdf", "D");
?>