Convert Images to PDF Guide

Develop your own solution to convert JPG, JPEG, PNG, TIF and TIFF to PDF by integrating with iLovePDF’s REST API.

New to the API? Start with Processing a PDF: every tool guide builds on its task flow.

Basic images to PDF conversion

You can easily convert JPG, PNG and TIFF images to PDF files like this:

Basic images to PDF conversion
// Create a new task
$myTaskConvertJPG = $ilovepdf->newTask('imagepdf');
// Add files to task for upload
$file1 = $myTaskConvertJPG->addFile('path/to/image1_name.jpg');
$file2 = $myTaskConvertJPG->addFile('path/to/image2_name.png');
// Execute the task
$myTaskConvertJPG->execute();
// Download the package files
$myTaskConvertJPG->download();
PHP library ilovepdf/ilovepdf-phpcomposer require ilovepdf/ilovepdf-php

As a result, you will get a single PDF file with one page per image. By default, all images are merged into one PDF (see merge_after below).

Advanced images to PDF conversion

You can customize some options of your image to PDF conversion with the following properties:

Change PDF orientation

Change PDF orientation
// Choose your processing tool
$myTaskConvertJPG = $ilovepdf->newTask('imagepdf');
// Set your tool options
$myTaskConvertJPG->setOrientation('landscape');
PHP library ilovepdf/ilovepdf-phpcomposer require ilovepdf/ilovepdf-php

With orientation you can change your final PDF file from portrait to landscape and vice versa. This may be useful depending on the size ratio of your converted images. Note that this function will change the orientation of your final PDF file, not the original images.

If you want to rotate your images before converting them to PDF, you need to use the rotate property for every uploaded file, as we have seen in previous guides.

Change PDF orientation individually
$file1->setRotation(270);
PHP library ilovepdf/ilovepdf-phpcomposer require ilovepdf/ilovepdf-php

There are four rotation orientations to choose from:

  • 0 degrees
  • 90 degrees
  • 180 degrees
  • 270 degrees

Set page margin

Set page margin
// Choose your processing tool
$myTaskConvertJPG = $ilovepdf->newTask('imagepdf');
// Set your tool options
$myTaskConvertJPG->setMargin(20);
PHP library ilovepdf/ilovepdf-phpcomposer require ilovepdf/ilovepdf-php

margin allows you to set a margin between the PDF page and its contained image. If you don’t set any margin, your final PDF will be as big as the original image.

Set page size

Set page size
// Choose your processing tool
$myTaskConvertJPG = $ilovepdf->newTask('imagepdf');
// Set your tool options
$myTaskConvertJPG->setPagesize('A4');
PHP library ilovepdf/ilovepdf-phpcomposer require ilovepdf/ilovepdf-php

One way to set your final PDF document’s page size is by forcing it with pagesize. You have three standard sizes to choose from:

  • A4 creates a final document with A4-standard size and the image filling it
  • letter creates a final document with Letter-standard size and the image filling it
  • fit creates a final document with the same size as the original image. This is the default option.

Merge all images in a single PDF

Merge all images in a single PDF
// Choose your processing tool
$myTaskConvertJPG = $ilovepdf->newTask('imagepdf');
// Set your tool options
$myTaskConvertJPG->setMergeAfter(true);
PHP library ilovepdf/ilovepdf-phpcomposer require ilovepdf/ilovepdf-php

Setting merge_after to true (the default) serves all converted images in a single PDF. Set it to false to get a separate PDF for every image. Note that if you combine this with pagesize, you will have different results.


That is all you need to know about Image to PDF conversion.