PNG JPG BMP TIFF PDF
Aspose.PDF  for PHP

Compress PDF in PHP SDK

Optimize PDF Document using Cloud PHP SDK

Get Started

How to crompress PDF via Cloud PHP SDK

To crompress PDF, we’ll use Aspose.PDF Cloud PHP SDK This Cloud SDK assists PHP programmers in developing cloud-based PDF creator, annotator, editor, and converter apps using PHP programming language via Aspose.PDF REST API. Simply create an account at Aspose for Cloud and get your application information. Once you have the App SID & key, you are ready to give the Aspose.PDF Cloud PHP SDK. This method leverages the Aspose.PDF Cloud SDK for PHP to optimize PDF documents efficiently. By automating the upload, optimization, and download processes, it simplifies the workflow for developers. The use of various optimization options ensures that the resulting PDF is smaller in size without compromising quality.

Package Manager Console Command

     
    composer install

Steps to compress PDF via Cloud PHP

Aspose.PDF Cloud developers can easily load & optimize PDF in just a few lines of code.

  1. Upload a PDF Document
  2. Compress the PDF Document
  3. Download the Optimized PDF Document
 

Compress PDF using PHP


    require __DIR__.'vendor\autoload.php';

    use Aspose\PDF\Configuration;
    use Aspose\PDF\Api\PdfApi;

    $credentials = json_decode(file_get_contents(__DIR__ . 'credentials.json'), true);

    $configParams = [
        "LOCAL_FOLDER" => "C:\\Samples\\",
        "PDF_DOCUMENT_NAME" => "sample.pdf",
        "TEMP_FOLDER" => "TempPdfCloud",
        "LOCAL_RESULT_DOCUMENT_NAME" => "output_sample.pdf",
    ];

    class PdfCompress
    {
        private $pdfApi;
        private $configParams;

        private function _create_rest_api() {
            $credentials = json_decode(file_get_contents("credentials.json"), true);

            $configAuth = new Configuration();
            $configAuth->setAppKey($credentials['key']);
            $configAuth->setAppSid($credentials['id']);

            $this->pdfApi = new PdfApi(null, $configAuth);
        }

        public function __construct($config) {
            $this->configParams = $config;
            $this->_create_rest_api();
        }

        public function uploadDocument()
        {
            $filePath = $this->configParams["LOCAL_FOLDER"] . DIRECTORY_SEPARATOR . $this->configParams["PDF_DOCUMENT_NAME"];
            $fileData = file_get_contents($filePath);

            $storagePath = $this->configParams["TEMP_FOLDER"] . DIRECTORY_SEPARATOR . $this->configParams["PDF_DOCUMENT_NAME"];

            $this->pdfApi->uploadFile($storagePath, $fileData);
            echo "File: '{$this->configParams["PDF_DOCUMENT_NAME"]}' successfully uploaded." . PHP_EOL;
        }

        public function downloadResult()
        {
            $fileName = $this->configParams["TEMP_FOLDER"] . DIRECTORY_SEPARATOR . $this->configParams["PDF_DOCUMENT_NAME"];
            $downloaded = $this->pdfApi->downloadFile($fileName);

            $filePath = $this->configParams["LOCAL_FOLDER"] . DIRECTORY_SEPARATOR . $this->configParams["LOCAL_RESULT_DOCUMENT_NAME"];
            file_put_contents($filePath, $downloaded);
            echo "Downloaded: {$filePath}" . PHP_EOL;
        }

        public function compressPdfDocument()
        {
            $options = new \Aspose\PDF\Model\OptimizeOptions();
            $options->setAllowReusePageContent(true);
            $options->setCompressImages(true);
            $options->setImageQuality(100);
            $options->setLinkDuplcateStreams(true);
            $options->setRemoveUnusedObjects(true);
            $options->setRemoveUnusedStreams(true);
            $options->setUnembedFonts(true);

            $response = $this->pdfApi->postOptimizeDocument(
                $this->configParams["PDF_DOCUMENT_NAME"],
                $options,
                null,
                $this->configParams["TEMP_FOLDER"]
            );

            if ($response->getCode() != 200) {
                echo "compressPdfDocument(): Failed to compress the PDF document!" . PHP_EOL;
            } else {
                echo "compressPdfDocument(): Successfully compressed the PDF document '{$this->configParams["PDF_DOCUMENT_NAME"]}' !" . PHP_EOL;
            }
        }
    }

    try {
        $compressor = new PdfCompress($pdfApi, $configParams);
        $compressor->uploadDocument();
        $compressor->compressPdfDocument();
        $compressor->downloadResult();
    } catch (Exception $e) {
        echo "Error: " . $e->getMessage() . PHP_EOL;
    }
 

With our PHP library you can:

Compress PDF documents with Aspose.PDF Cloud PHP SDK.

  • Add PDF document’s header & footer in text or image format.
  • Add tables & stamps (text or image) to PDF documents.
  • Append multiple PDF documents to an existing file.
  • Work with PDF attachments, annotations, & form fields.
  • Apply encryption or decryption to PDF documents & set a password.
  • Delete all stamps & tables from a page or entire PDF document.
  • Delete a specific stamp or table from the PDF document by its ID.
  • Replace single or multiple instances of text on a PDF page or from the entire document.
  • Extensive support for converting PDF documents to various other file formats.
  • Extract various elements of PDF files & make PDF documents optimized.
  • You can try out our free App to test the functionality online.