Site icon Tutorials Website: Upgrade Your Web Development & Coding Skills

How I Built an Auto Filling Pan Card Form PDF System Using PHP

Auto Filling PAN CARD FORM PDF

When I got this requirement for the first time, I honestly thought it would be very easy.

I was like — “Okay, just take data from database, put it into a PDF, and done.”

But when I actually started working on it, things were completely different.

The main problem was that these forms (like PAN forms) are not simple PDFs. You can’t just open them and insert text wherever you want. Everything is fixed — every box, every line, every position.

That’s when I realized I needed a different approach.

After trying a few methods (and wasting a lot of time 😅), I finally found a way that works perfectly.

Are you want to get implementation help, or modify or extend the functionality?

A Tutorialswebsite Expert can do it for you.

The Approach That Actually Worked

Instead of editing a PDF directly, I did something simple:

At first, it sounded like a workaround… but it turned out to be the most reliable solution.

Let me explain how I built it step by step.

Auto Filling PAN CARD FORM PDF Generation in PHP

1. Getting Data from Database

All the form data comes from the database. As you can see in my demo page.

Things like:

Nothing fancy here, just normal database fetching.

2. Loading the Form Image

Next, I load the blank form image.

$image = imagecreatefromjpeg($imgpath);

Think of this like a blank sheet where you are going to write everything.

3. Writing Data on the Form

This part took most of my time.

imagettftext($image, 20, 0, 380, 445, $color, $font, $name);

Here, those numbers (380, 445) are very important.

You have to manually adjust them until the text fits exactly in the right place.

Honestly, I didn’t get this right in one go. I had to try again and again until everything aligned properly.

4. Handling Names (This Was Tricky)

Users don’t enter names in a fixed format.

Some people write:

So I split the name like this:

$cname = explode(' ', $name);

Then I handled different cases.

It’s a bit messy, but it works. Without this, the form looks broken.

5. Gender-Based Text

Forms require things like:

So I added simple conditions:

if($gender == 1){
    $text = 'S/O '.$fatherName;
}

Nothing complex, but very important for real forms.

6. Address Handling

Address is not a single line.

So I printed it in multiple lines:

City and state I fetched from database using IDs.

7. Saving the Filled Form

Once everything is written:

imagejpeg($image, "application_form_data/form.jpg");

Now I have a fully filled form — but it’s still an image.

8. Converting Image to PDF

This is where FPDF comes in.

$pdf = new FPDF();
$pdf->AddPage();
$pdf->Image("form.jpg",0,0,210,296);
$pdf->Output();

And that’s it — now it becomes a proper PDF.

9. Multi-Page Forms

Some forms have more than one page.

So I created separate images for each page and added them:

$pdf->AddPage();
$pdf->Image("page2.jpg");

10. Adding Tick Marks

This was actually interesting.

For checkboxes, I used a small tick image:

$pdf->Image("blue-tick.jpg",28,68);

Then I placed it conditionally.

It’s simple, but gives a very realistic output.

11. Character Spacing (Most Useful Trick)

Normal text doesn’t fit well in boxes like PAN or Aadhaar.

So I created a custom function that prints one letter at a time.

That solved the alignment issue completely.

12. Cleaning Temporary Files

After generating the PDF, I delete images:

unlink($file);

Otherwise, your server will slowly fill up with useless files.

Are you want to get implementation help, or modify or extend the functionality?

A Tutorialswebsite Expert can do it for you.

Problems I Faced

This was not smooth at all 😅

Some real issues I faced:

Most of the time went into fixing alignment.

Things I Learned

If you are planning to build something like this, keep these in mind:

Where You Can Use This

This kind of system is actually very useful.

You can use it for:

Final Thoughts

For me, this project was not just about coding. It was more about solving a real-world problem.

Once it started working properly, it saved a lot of manual work. And the best part — this can easily scale. You can generate hundreds or thousands of PDFs without extra effort.

If you are a developer, I would definitely suggest trying something like this. It’s simple in concept, but very powerful in real use.

And yes… be ready for a lot of trial and error 😄

Looking for a Website Developer in Delhi NCR?

Get a professionally designed and developed website tailored to your needs.
As an experienced website developer based in Delhi NCR, I offer customized solutions to build responsive, SEO-friendly, and user-friendly websites. Whether it’s for a personal blog, business site, or e-commerce store, I ensure your online presence stands out.

Exit mobile version