BarCode and Identity Card Generator using PHP | Picqer BarCode | Mpdf
- Posted on January 11, 2025
- Technology
- By MmantraTech
- 89 Views
Hey welcome to this blog. Let me explain how to generate ID cards in PHP using PHP libraries like mPDF and Picqer Barcode Generator, you can automate this process and generate professional-looking ID cards in PDF format with embedded barcodes.
-wZ2INOK4dx.png)
After walking through this blog you will get to know how to create student ID cards with a custom background, personal details, and a barcode using PHP. Let's start
Prerequisites
To follow along with this tutorial, ensure you have the following installed:
1. PHP (version 7.4 or higher)
2. Composer (for managing dependencies)
3. Required libraries:
o mpdf/mpdf
o picqer/php-barcode-generator
You can install the required libraries by running:
composer require mpdf/mpdf
composer require picqer/php-barcode-generator
The Code: Generating ID Cards
Below is the PHP code to generate ID cards for a list of students:
Composer.json
{
"require": {
"picqer/php-barcode-generator": "^2.4",
"mpdf/mpdf": "^8.2"
}
}
generator.php
require 'vendor/autoload.php';
use Picqer\Barcode\BarcodeGeneratorPNG;
use Mpdf\Mpdf;
// Sample student data
$students = [
[
'name' => "kamal",
'roll_no' => 12345,
'mobile' => 898989898,
'school_name' => "kvr",
'address' => "delhi"
],
[
'name' => "tech",
'roll_no' => 4555,
'mobile' => 90909090,
'school_name' => "kvr3",
'address' => "delhi"
],
[
'name' => "sumit",
'roll_no' => 454545,
'mobile' => 33333,
'school_name' => "123kvr",
'address' => "delhi56"
],
[
'name' => "javin",
'roll_no' => 56565,
'mobile' => 898989898,
'school_name' => "6kvr",
'address' => "7delhi"
]
];
$backgroundImage = 'background.jpg'; // Path to the background image
// Initialize barcode generator
$generator = new BarcodeGeneratorPNG();
// Start creating the PDF
$mpdf = new Mpdf(['format' => [100.6, 74]]); // Standard ID card size in mm (credit card size)
$mpdf->SetDefaultBodyCSS('background', "url('$backgroundImage')");
$mpdf->SetDefaultBodyCSS('background-image-resize', 6); // Resize the background image
$mpdf->AddPageByArray([
'margin-left' => 20,
'margin-right' => 1,
'margin-top' => 10,
'margin-bottom' => 2,
]);
// Generate an identity card for each student
foreach ($students as $student) {
$barcode = $generator->getBarcode($student['roll_no'], $generator::TYPE_CODE_128);
// Card content
$html = '
<div style="position: relative; width: 85.6mm; height: 54mm; font-family: Arial, sans-serif; color: #000; padding: 10px;">
<table style="width: 100%; height: 100%; border-collapse: collapse; text-align: left;">
<tr>
<td style="font-size: 14px; font-weight: bold;">Name:</td>
<td style="font-size: 14px;">' . htmlspecialchars($student['name']) . '</td>
</tr>
<tr>
<td style="font-size: 14px; font-weight: bold;">Roll No:</td>
<td style="font-size: 14px;">' . htmlspecialchars($student['roll_no']) . '</td>
</tr>
<tr>
<td style="font-size: 14px; font-weight: bold;">Mobile:</td>
<td style="font-size: 14px;">' . htmlspecialchars($student['mobile']) . '</td>
</tr>
<tr>
<td style="font-size: 14px; font-weight: bold;">School:</td>
<td style="font-size: 14px;">' . htmlspecialchars($student['school_name']) . '</td>
</tr>
<tr>
<td style="font-size: 14px; font-weight: bold;">Address:</td>
<td style="font-size: 14px;">' . htmlspecialchars($student['address']) . '</td>
</tr>
<tr>
<td colspan="2" style="text-align: center; padding-top: 10px;">
<img src="data:image/png;base64,' . base64_encode($barcode) . '" alt="Barcode" style="width: 100%; max-width: 200px;" />
</td>
</tr>
</table>
</div>
';
$mpdf->WriteHTML($html);
$mpdf->AddPage(); // Add a new page for the next ID card
}
// Output the PDF
$mpdf->Output('IdentityCards.pdf', \Mpdf\Output\Destination::INLINE);
Now let me explain above code step by step:
1. Sample Student Data: The $students array holds sample student information, including name, roll number, mobile number, school name, and address. you can fetech these data from any dataset
2. Background Image (optional ): We use a custom background image for the ID cards by setting it using SetDefaultBodyCSS.
3. Barcode Generation: The barcode is generated using the Picqer\Barcode\BarcodeGeneratorPNG library. Each student's roll number is converted into a barcode of type CODE_128.
4. HTML Structure (take your own structure): The card's content is dynamically created using HTML, with placeholders for student details and the barcode image.
5. PDF Creation: The mPDF library is used to create a PDF with a specific page size, and a new page is added for each ID card.
6. Output: Finally, the PDF is output directly to the browser.
Moreover you can follow below tips:
- Page Size: The page size is set to standard ID card dimensions (85.6mm x 54mm). You can modify this as needed.
- Design: Feel free to enhance the card's design by adding logos, colors, or other styles.
- Data Source: Replace the hard-coded $students array with data fetched from a database for real-world applications.
Conclusion
So this is just an demo example but same concept you can apply in module of php code. By combining mPDF and Picqer Barcode Generator, you can easily create personalized ID cards with barcodes in PHP. This approach saves time and ensures consistency, making it ideal for schools, colleges, and organizations.
Write a Response