views:

248

answers:

2

I need to code a PHP script that would let me generate a pdf file which displays a member ID card (something like a credit card used to identify oneself) at a certain resolution.

Let me explain:
I do have the basic blueprint of the card in png file format. The script needs to drop in a member's name and birthday along with a serial. So far, no problem - there are plenty of good working PHP libraries out there.

My problem is to ensure that the resulting pdf (the generated image of the card, to be precise) meets a certain resolution (preferably 300dpi), so that printing it would look right.

Any ideas?

EDIT
I solved it using the TCPDF library which lets you scale images at a certain resolution.
Get it here: http://www.tecnick.com/public/code/cp_dpage.php?aiocp_dp=tcpdf

EDIT @Don

require_once(dirname(__FILE__).'/tcpdf/config/lang/eng.php');
require_once(dirname(__FILE__).'/tcpdf/tcpdf.php');

// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT,
                 true, 'UTF-8', false); 

// set document information
$pdf->SetCreator('SO-youth');
// remove default header/footer
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);

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

//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);

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

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

// set font
$pdf->SetFont('courier', '', 10);

// add a page
$pdf->AddPage();

// set JPEG quality
$pdf->setJPEGQuality(100);

$cred = <<<EOT
    <p>
        <b>{$userdata->first_name} {$userdata->last_name}</b><br/>
        <span style="font-size: 80%;">{$userdata->geburtstag}</span>
    </p>
EOT;
$id = <<<EOT
    <span style="font-size: 60%;">{$userdata->club_id}</span>
EOT;

$pdf->Image(dirname(__FILE__).'/img/clubcard.jpg',
            10, 10, 85.6, 53.98, null, null, null, false, 300);

$pdf->writeHTMLCell(60, 15, 50.5, 20.5, $cred);
$pdf->writeHTMLCell(50, 20, 77, 50.5, $id);

//Close and output PDF document
$pdf->Output($userdata->filename, 'F');
+1  A: 

I would use Imagemagick for this purpose, along with Imagick to be able to access it directly from php.

You will then be able to take the original image, add some text to it, and output (or store) as pdf, with something like this:

    $image = new Imagick($filename);
    $draw = new ImagickDraw();
    $draw->setFont($font);
    $draw->setFontSize($fontSize);
    $image->annotateImage($draw, $xpos, $ypos, $rotation, $text);
    // Changes the dpi
    $image->setImageResolution(200,200);

I can't find the correct code for outputting/storing it as pdf quickly, but that should be documented somewhere on the Imagemagick site.

Jasper De Bruijn
This would certainly do the trick, to bad the lib is not and can't be installed (low cost hoster :/).
aefxx
A: 

Your script sounds great, can you post it so that others can use it too?

Don
@Don I've updated my post for you. Hope it helps.
aefxx