Crop PDF using PHP

PDF cropping programmatically using Aspose.PDF for PHP via Java Library

Crop PDF with PHP

In order to crop PDF file, 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.

How to Crop PDF using PHP


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

  1. Open a PDF document using Document object.
  2. Choose the type of box and create new Box Rectagle
  3. Save the updated document to the new path using the Save() method.

If you want to crop a PDF page, a cropping solution can help. With this powerful Aspose.PDF PHP via Java library, you can change your PDF while keeping its quality. This professional tool uses PHP to crop PDF effectively. The following example demonstrates how to modify a PDF document in PHP:

Crop PDF Files - PHP

This sample code shows how to Crop PDF Page - PHP

Input file:

File not added

Output format:

Output file:


// Open document
$document = new Document($inputFile);      

$page = $document->getPages()->get_Item(1);

$responseData = $page->getCropBox() . PHP_EOL;
$responseData = $responseData . $page->getTrimBox() . PHP_EOL;
$responseData = $responseData . $page->getArtBox() . PHP_EOL;
$responseData = $responseData . $page->getBleedBox() . PHP_EOL;
$responseData = $responseData . $page->getMediaBox() . PHP_EOL;

// Create new Box Rectangle
$newBox = new Rectangle(200, 220, 2170, 1520);

$page->setCropBox($newBox);
$page->setTrimBox($newBox);
$page->setArtBox($newBox);
$page->setBleedBox($newBox);

// Save output document
$document->save($outputFile);
$document->close();

About Aspose.PDF for PHP via Java API

Aspose.PDF for PHP via Java is a powerful API designed to assist developers in creating, manipulating, and converting PDF documents programmatically. Aspose.PDF allows developers to create PDF documents from scratch or convert other document formats (such as HTML, XML, and images) to PDF. This is useful for generating reports, invoices, or any document type. It also recommends viewing a Documentation.