PDF to JPG conversion using PHP

PDF to JPG conversion using PHP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

PDF to JPG conversion using PHP

Converting a PDF to an image in PHP often feels straightforward until you start wrestling with resolution and dimensions. You might have a script that successfully reads an A4 document and outputs a JPEG, but when you print that image, it looks tiny compared to the original paper. This happens because the conversion logic is ignoring the physical units defined in the PDF or misinterpreting the DPI settings.

In the scenario where imagick reads a file with correct dimensions (210x297 mm) but outputs an 842x595 pixel image at 72 DPI, the root cause lies in how ImageMagick handles coordinate systems and default settings. The code snippet using $img->readImageFile() works well for simple cases, but it inherits the PDF's internal resolution profile. If the PDF was created with a standard screen display profile (often 72 or 96 DPI), imagick defaults to that scale unless explicitly overridden.

To ensure the resulting JPEG matches the physical size of the A4 paper when printed, you must force ImageMagick to interpret the input dimensions in millimeters and then render them at your desired output resolution.

The most reliable approach is to read the PDF page as a raw image first, then resize it based on physical units. Instead of relying on readImageFile() alone, which might lock into the document's embedded DPI, you should explicitly set the resolution before or after reading. Consider modifying the workflow to ensure the resolution parameter takes precedence:

$fp_pdf = fopen($pdf, 'rb');
$img = new Imagick();
$img->setResolution(300, 300); // Force 300 DPI for high-quality print output
$img->readImageFile($fp_pdf);

// Ensure units are set to pixels per inch so the calculation is consistent
$img->setImagingMatrix(1.0); 
$img->setImageFormat("jpg");
$img->setImageCompression(Imagick::COMPRESSION_JPEG);
$img->setImageCompressionQuality(95); // High quality for printing

$data = $img->getImageBlob();
fclose($fp_pdf);

If you are still seeing the dimensions shrink, the issue might be how ImageMagick interprets the source page size. Some PDFs define their pages in points or Twips rather than millimeters. To guarantee the output matches the physical A4 size exactly, you can read the image and then force a resize based on known physical dimensions:

$img = new Imagick();
$img->readImageFile($pdf);

// Set the target resolution (e.g., 300 DPI)
$img->setResolution(300, 300);

// If the resulting image is still not A4 sized in pixels, 
// calculate the exact pixel dimensions for A4 at 300 DPI
// Width = 210mm / 25.4 * 300 = ~2480px
// Height = 297mm / 25.4 * 300 = ~3508px
$targetWidth = round(210 / 25.4 * 300);
$targetHeight = round(297 / 25.4 * 300);

$img->resizeImage($targetWidth, $targetHeight, Imagick::FILTER_LANCZOS, true);

$data = $img->getImageBlob();

This method overrides any embedded DPI metadata and ensures the pixel count corresponds strictly to a 300 DPI print of A4 paper. The trade-off here is that you lose the original document's native resolution if it was higher than 300 DPI, but this is usually acceptable for general image conversion tasks where file size needs to be manageable.

Another pitfall to watch out for is the distinction between "virtual" resolution and actual pixel dimensions. Setting $img->setResolution(300, 300) tells ImageMagick how many pixels per inch to use when saving, but if the source image has a different aspect ratio or embedded size, you might end up with black bars or stretched content. Always verify that the source PDF page size matches your target output dimensions before resizing.

For production environments where performance matters, remember that reading large PDFs into memory can be resource-intensive. If you are converting hundreds of documents, consider processing them in a queue system rather than synchronously. Additionally, while imagick is powerful, it requires the ImageMagick binary to be installed on the server, which adds deployment complexity compared to pure PHP libraries like mepdf.

If your specific use case involves generating thumbnails or previews where exact print dimensions aren't critical, sticking with the default readImageFile() behavior and adjusting quality settings might suffice. However, for tasks requiring faithful reproduction of paper documents, explicitly defining resolution and physical dimensions is the only way to guarantee the output matches expectations on a printer.

Our network