DEV Community

onlin3.net
onlin3.net

Posted on

# How I Built a Free Online Tool to Convert Images to PDF (PHP + mPDF)

Converting images to PDF sounds simple, but when you try to make it work for everyone (desktop, mobile, different formats, large files), you realize it’s a real challenge.

In this post, I’ll share how I built onlin3.net — a free tool that allows anyone to upload images (JPG, PNG) and turn them into a clean PDF file.


1. Choosing the Tech Stack

For this project, I kept things simple but powerful:

  • PHP for the backend.
  • mPDF for generating PDFs.
  • Bootstrap for a clean, responsive UI.

This allowed me to focus on solving the actual problem instead of reinventing the wheel.


2. Handling Image Uploads

The first step was to handle multiple image uploads safely.


php
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['images'])) {
    $files = $_FILES['images'];
    foreach ($files['tmp_name'] as $file) {
        $image = imagecreatefromstring(file_get_contents($file));
        // process image here
    }
}
`
Key features I implemented:

Support for both JPG and PNG.

Automatic EXIF rotation fix (so mobile camera photos don’t appear sideways).

Compression before adding images to the PDF to keep file sizes small.
`require_once __DIR__ . '/vendor/autoload.php';
use Mpdf\Mpdf;

$mpdf = new Mpdf();
$mpdf->WriteHTML('<h1>Image to PDF Example</h1>');
$mpdf->Output('output.pdf', 'I');
`
3. Generating the PDF

With mPDF, turning images into PDF pages is straightforward:
`require_once __DIR__ . '/vendor/autoload.php';
use Mpdf\Mpdf;

$mpdf = new Mpdf();
$mpdf->WriteHTML('<h1>Image to PDF Example</h1>');
$mpdf->Output('output.pdf', 'I');
`
Each uploaded image becomes a new page.

I also added a “scan effect” option:

Grayscale mode

Crop white borders

Improve brightness/contrast

This makes images look like professionally scanned documents.

4. User Experience

To keep the service fair and simple:

Guests can upload 1 image at a time.

Registered users can upload up to 3 images in one go.

PDFs for guests are stored temporarily.

PDFs for logged-in users are stored permanently in their dashboard.

5. Challenges

Some of the hardest parts of the project:

Handling very large images (10MB+ from modern cameras).

Keeping the tool fast even on shared hosting.

Making sure PDFs render correctly on different devices.

6. Try It Yourself 🚀

If you want to test the tool live:
👉 Convert JPG/PNG to PDF online

Conclusion

This project taught me that “simple” tools are rarely simple under the hood.

If you’re working with PHP and need a reliable PDF solution, I highly recommend checking out mPDF.

And if you just need a quick solution, feel free to use my free tool at onlin3.net
.
Enter fullscreen mode Exit fullscreen mode

Top comments (0)