PDF ID Card Handbook

The PDF-IDcard module, meaning PDF Identification Card, is designed to create a PDF with exact dimensions for the purpose of being printed out. It was created for the public access center television station, Manhattan Neighborhood Network, to provide physical Identification cards that the staff and producers carry around with them.

The module handles all of the process of creating the PDF, adding a page, matching the printable dimensions, adding a background image, as well as permissions, etc. You are responsible for writing a a bit of PHP code to print data on the Identification card (for example the User's name and/or email address). The module is designed to take a background image and overlay data on top of it.

These are the three basic steps to creating a card

Step #1

Create a template
4.7: Click on "ID Cards" in the administrative menu
5.x: Click Site building and then on "ID Cards"

*Name: Anything you'd like but avoid special characters and spaces.

*Page orientation (portrait or landscape) this will determine the layout of the PDF and is usually based on how your printer is setup

*Background Image: (optional). If a background image is uploaded it will be used as the backdrop of the PDF. The module was designed to be used with a standard 72 DPI image created as a jpeg or png file. Text is then placed on top of the image to create the final badge.

*Page Style: This will determine the overall size of the resulting PDF although it may match your paper size it does not necessarily have to.
** Custom: If custom is chosen then the Width/Height boxes will be used to size the PDF
** Image: If you have uploaded a custom image setting this to Image will cause the resulting PDF to be the same width/height of the uploaded image (72 DPI).

Step #2

This is the "tricky" part. In order to print something onto the card you will need to use PHP code. The PHP Code syntax is straight from the TCPDF library thus you can use any of the available objects/functions, etc. from TCPDF. If you are not a strong coder, or a coder at all, don't worry as the syntax can be quite simple.

Documentation is included with the TCPDF download and is linked to at the bottom of the box. This will be your coding bible for this part.

The module provide you two objects to work with. $pdf and $account.

$pdf = TCPDF Object with a page added to the PDF document
you only need to print text and don't need to worry about opening/closing pages, the module does that for you
$account = A Drupal user object for the user whose identification card is being printed
if the profile module is installed the user's profile is loaded onto the account object.

Here is some sample code that will get you started.

$pdf->SetFont('freeserif','B',14);
$pdf->SetXY(100,100);
$pdf->Cell(100,20,"Hello World",0);

Now let's break that down.

$pdf->SetFont() needs to be called at least once to tell the PDF what font you will be using, whether its bold or not and what font size to use. In this case its using a bolded freeserif 14 point font.
TCPDF ships will several fonts and you can add custom fonts; review the fonts directory in TCPDF to determine what's available (TCPDF uses its own font directory and does not use the systems fonts)

$pdf->SetXY(100,100); This set the coordinates of where the text will be printed out. Think of this like setting the head of a type writer either at the start of the page, middle of the page, etc. etc. All output will being at these coordinates until $pdf->SetXY(x,y) is called again. If you write something and then stop writing will occur at the last know location.

$pdf->Cell(100,20,"Hello World",0); This is one of many write functions that outputs data. Cell() works just like an HTML table cell in that it has a border, padding, fill color, left/center/right alignment, etc. In this case the Cell will be 100 millimeters wide by 20 millimeters tall and will say "Hello World" inside it. 0 means that there will be no border.
$pdf->Write() is another way to write data out. See the advanced example.

note
Unfortunately your PHP code must contain no syntax errors otherwise the resulting PDF will not be created. The module will not warn you if your code contains syntax errors instead PHP will yell at you with a big error that contains "eval()'d code" in the error message (this is how you know its' your code that has an error).

Step #3

Now that a template is setup and there is some PHP code to overlay data on top of it you simply need to navigate to a User account and print it out (ex. /user/1) If there is only a single template available there will be a link to click on. If multiple cards exist there will be a dropdown to choose the card.

FAQs

#1) Where are the PDFS stored?
They are not stored, they are generated dynamically and sent directly to the browser.

#2) Is it possible to put images into the resulting PDF cards?
yes. Use $pdf->Image(string $file, float $x, float $y, float $w, float $h, [string $type = ''], [mixed $link = '']) to accomplish this. Check the TCPPDF documentation for more info.

#3) I keep getting errors, this sucks!
yes.. errors do suck. If you have an error like "warning: Cannot modify header information - headers already sent by" then check the error for the words "eval()'d code".
If you have this error it means that you have an error with your PHP code. Check the syntax of your code and try again.

That's it! Please comment on this page so that it can be updated with better examples and more relevant instructions.

Advanced Example

This example is to be used with the attached sample JPEG.

Guide maintainers

jredding's picture