Creating business cards with FPDF and PHP
By Flib
2009-05-19
Category: PHP
The Problem
As a programmer, I am fairly adept at writing programs to solve my own problems. One thing I am definately not though is a graphic designer. I spend as little time as possible with applications that the 'crayon users' use on a daily basis.
Given the above, when I need to create business cards or similar documents, I frequently head for the text editor and knock out a small app. This is one such application.
The Solution
I don't think this needs much expanation, the comments are fairly obvious
<?php
//fpdf provides the pdf creation capability
require('fpdf.php');
$pdf=new FPDF();
$pdf->AliasNbPages();
//we need at least one page to make a useful pdf
$pdf->AddPage();
//set colour to grey
$pdf->SetDrawColor(192,192,192);
//vertical crop marks
$pdf->Line(105,10,105,290);
$pdf->Line(20,10,20,290);
$pdf->Line(190,10,190,290);
//top crop mark (others done with each card)
$pdf->Line(10,285,290,285);
//loop through 10 cards (one page)
for ($i=1;$i<=10;$i++) {
//x and y are the offset for the start of the card
$x=($i % 2)*85 + 20;
$y=($i % 5)*54 + 15;
//draw a crop mark (there will be one
//on top of the other for each column
//but it doesn't make much difference)
$pdf->Line(10,$y,200,$y);
//add a logo
$pdf->Image('logo.png',$x+3,$y+3,20,30);
//set font to arial 10pt and colour to black
$pdf->SetFont('Arial','',10);
$pdf->SetTextColor(0);
//draw text
$pdf->Text(30+$x,25+$y,'Name:');
$pdf->SetFont('Arial','',10);
$pdf->Text(5+$x,35+$y,'Membership Number:');
$pdf->SetFont('Arial','',10);
$pdf->Text(5+$x,40+$y,'Membership Expires:');
$pdf->SetFont('Arial','',10);
$pdf->Text(5+$x,50+$y,'District Manager: ');
} //end of loop
//add some meta data to the page.
$pdf->SetFont('Arial','',5);
$pdf->Text(10,293,'Card Size : 85mmx54mm A4 Margins L:20mm T:15mm R:20mm B:12mm Fonts: Arial');
$pdf->Text(10,295.5,'Version 1.0 - 17/11/2008');
$pdf->Text(150,293,'PDF created by Technomonk Industries - www.technomonk.com');
$pdf->Output();
?>
This could easily be made far more dynamic; the logo, the positions of the lines, the font style and colour and the content itself could all be pulled from a database and used to generate a pdf ready for printing.
I freely give this example code (but not the article itself) to the puclic domain. You are free to use it for any purpose (although an acknowlegement would be nice).