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.
Comments
Some help to setup TCPDF.
Some help to setup TCPDF.
if you dont know your php version create a file called info.php with the following content:
Upload that file to your webserver (e.g. your drupal root directory) and call in in your browser http://www.yourdomain.com/info.php
----------------------
forward-media.de
Help with the code
Could someone post a few examples of code that cover common uses - i've got this installed and working (the hello world example works fine) but i'm lost as to how to actually do anything useful with it.
I would like to output the users ID, username, email and a field from usernode / nodeprofile if possible....
(edit -i've got it working for the username email and user id - i'm still stuck on the stuff from nodeprofile though)
any help would be much appreciated :)
here is what i'm using currently maybe this will help someone else use this module
profile custom fields not found
In Step #2 above you wrote:
However I cannot access $account->profile_xxx (where profile_xxx is a custom profile field added through the profile module).
In fact I looked at both print_r($account) and {global $user; print_r($user);} and the custom profile fields are nowhere to be found. Can anyone explain how to retrieve the custom fields added through the profile module?
Load the profile
You can do that in your PDF creation code. I'll add this to the next stable release as it is only a few lines of code, doesn't add a dependency and would make things easier for people.
User Picture
I'm trying to add the users picture to the id card here is my code and the error I am getting.
Any help or ideas is appreciated thank you.
$pdf->Image($account->profile_picture, 50, 50, 50, 50, ' ', true, 150);
TCPDF ERROR: [Image] No such file or directory in
i found the solution
for print the profile picture use this:
$pdf->Image($account->picture, 40,40);
because profile_picture does not exist as class, picture is the correct class
escuse my english
*_*
Is it possible to use a tpl.php file and publish it as id card having all the fields/variables replaced by the respective values loaded in $user/$account?
This will make it more flexible to use any user profile modules like content profile and will reduce the overhead of handicraft the code to generate the id card.
Regards.
Regards.
🪷 Beautifulmind
Thanks
Thanks for the idea.
Any word on a Drupal 7
Any word on a Drupal 7 version of this? This is not working well on 7 (looks like it's still using 6.x hooks instead of 7s)