Add Images to PDF document via PHP

Insert images to PDF document programmatically using Aspose.PDF for PHP via Java

Add Image to PDF Document Using PHP Tool

In order to add Image in PDF, we’ll use Aspose.PDF for PHP via Java API which is a feature-rich, powerful and easy-to-use document manipulation tool in php-java. Install Tomcat 9.0 version on any location, add Aspose.PDF.war, for more details check the GitHub page.

Add Image to PDF using PHP


You need Aspose.PDF for PHP via Java to try the code in your environment.

  1. Create a Document object and open the input PDF document.
  2. Get the page you want to add an image.
  3. Add the image into the page’s Resources collection.
  4. Use the GSave operator to save the current graphical state.
  5. Use ConcatenateMatrix operator to specify where the image is to be placed.
  6. Use the Do operator to draw the image on the page.
  7. Use GRestore operator to save the updated graphical state.
  8. Save the PDF file.

Add Image to PDF document - PHP

This sample code shows how to add Images into PDF page - PHP


    // Open document
    $document = new Document($inputFile);
    $page = $document->getPages()->add();
    $page->setPageSize(1190.7, 841.995);
    $page->addImage($imageFileName, $page->getCropBox());
    $document->save($outputFile);
    $document->close();