Compress PDF PHP

Reduce PDF file size up to 90% by optimizing images, fonts, and removing unused objects while preserving visual quality.

Optimize Tools

Compress PDF PHP Overview

The Compress PDF PHP SDK by ConvertAPI is a tool that allows for the compression and reduction of PDF file sizes by up to 90%. This PHP SDK optimizes PDF quality by compressing text, graphics, images, subsetting fonts, and optimizing document structure. It offers various parameters for customization, including compression presets, color and grayscale image compression, and options to remove elements like bookmarks, annotations, forms, and embedded files from the PDF. The PHP SDK also provides options for optimizing the PDF for web viewing and preserving the PDF/A standard.

Lightning-Fast Compression

Shrink your PDFs in just a few seconds without compromising quality.

Advanced Image Recompression

Force image recompression to ensure even pre‑compressed images get smaller.

Compression without losing quality

Maintain visual quality of the original file while dramatically reducing file size.

Preserve Fonts & Vectors

Compression keeps text, fonts, and vector graphics sharp.

Multiple Compression Levels

Choose from predefined compression presets or set up your own settings.

Secure by Design

File compression is encrypted and compliant with ISO, GDPR, and HIPAA.

Customizable Parameters

Fine-tune your automation with these powerful conversion options

File

File Supported formats: .pdf

File to be converted. Value can be URL or file content.

Password

String

Sets the password to open protected PDF.

Preset

Collection Default: none

Choose a compression level from the available presets. If a preset is selected, all other compression options will be ignored. These presets are optimized for various use cases, such as balancing file size and quality.

Values:   none lossless text archive web ebook printer

ImageResolution

Integer Default: 150

Set the maximum image resolution in DPI.

Range:   10 .. 800

ImageQuality

Integer Default: 80

Set image quality in percentage.

Range:   10 .. 100

UnembedBaseFonts

Bool Default: False

Specifies whether to remove the base fonts from the PDF file.

SubsetEmbeddedFonts

Bool Default: False

Output PDF should only contain font characters utilized in the original document, and any unused glyphs from all fonts in the document should be removed.

Optimize

Bool Default: True

Optimize page content streams in the PDF file.

Linearize

Bool Default: False

Linearize PDF file and optimize for fast Web View.

PreservePdfa

Bool Default: False

Preserve the PDF/A standard in the PDF file.

RemoveForms

Bool Default: False

Remove PDF forms from the PDF file.

RemoveDuplicates

Bool Default: True

Remove duplicate fonts and color profiles from the PDF file.

RemovePieceInformation

Bool Default: True

Remove piece information dictionaries like Adobe Illustrator or Photoshop private data.

RemoveEmbeddedFiles

Bool Default: True

Remove embedded/attachments files from the PDF file.

RemoveStructureInformation

Bool Default: True

Remove all structure information from the PDF document.

RemoveMetadata

Bool Default: True

Detach the XMP metadata embedded in the document from its catalog, clear all properties in all schemas, and remove metadata from marked content.

RemoveUnusedResources

Bool Default: True

Remove references to resources such as fonts, images, patterns.

StoreFile

Bool Default: False

When the StoreFile parameter is set to True, your converted file is written to ConvertAPI’s encrypted, temporary storage and made available via a time-limited secure download URL, valid for up to 3 hours. After this period, the file is permanently deleted.

When StoreFile is set to False, conversion happens entirely in-memory. The raw file bytes are streamed back in the API response without touching disk or external storage, ensuring maximum security and zero persistence so that only you can access the content.

Integrate using PHP

Ready-to-run code samples for quick conversion and automation.

A detailed guide to compressing PDFs using PHP

PDF Compression became more important than ever in this digital document processing age. More and more digital documents are produced every day, which results in a bulky and space-consuming PDF storage. ConvertAPI's PDF Compression tool allows you to shrink the PDF's size using the PHP programming language up to 90% of the original without losing the visual quality of the document. In this article, we will explain how to use the PHP programming language to compress your PDF documents for sharing or long-term storage.

Best PDF Compression library for PHP

ConvertAPI provides a large suite of PDF tools using a single library for PHP. Using this library, you can convert, rotate, merge, split, watermark, and encrypt/decrypt your PDFs easily. The compression feature allows you to customize the compression levels by compressing and re-encoding images, removing duplicate objects, restructuring the PDF, subsetting fonts, and applying other techniques that we will discuss later in this article. In addition to that, you can safely compress the PDF/A-standardized documents without worrying about losing the PDF/A standard. You can also compress a password-protected document. Without further ado, let's jump straight into some code examples!

How to use the PHP programming language to compress a PDF?

  1. Install the ConvertAPI library for PHP into your project
  2. Sign up for a free account and receive your API Token
  3. Set up the conversion using our interactive UI
  4. Copy-paste the generated code snippet into your project

1. SDK library installation

You can install the ConvertAPI library manually or using the composer (preferred method). If you have the Composer installed, simply use this command:

composer require convertapi/convertapi-php

Alternatively, if you wish to install it manually, you must require the ConvertAPI autoloader:

require_once('/path/to/convertapi-php/src/ConvertApi/autoload.php');

Please note that the library uses curl and json dependencies. Using Composer it will be handled automatically. If installing manually, please make sure that these extensions are installed.

2. Retrieve your API Token

You need to get the API Token to start using the library. In order to do so, please sign up for a free account. Once signed up, you will find the API Token in your account dashboard.

API Token

3. Set up the compression parameters

Once you have your library installed, let's get to the fun part. To compress a PDF file using PHP, you don't need to code anything. Simply visit our interactive demo tool and set up the compression parameters using our friendly UI.

4. Copy-paste the code snippet into your project

Once you are happy with the compression result on our website, you will find an auto-generated code snippet at the bottom! Your PDF compression in the PHP programming language will look something like this:

ConvertApi::setApiCredentials('api_token');
$result = ConvertApi::convert('compress', [
        'File' => '/path/to/my_file.pdf',
        'ColorImageCompression' => 'jpx',
        'ColorImageQuality' => '70',
        'RemoveBookmarks' => 'true',
        'RemoveAnnotations' => 'true',
        'RemoveForms' => 'true',
        'RemovePageLabels' => 'true',
        'RemoveLayers' => 'true',
        'RemoveArticleThreads' => 'true',
        'UnembedBaseFonts' => 'true',
        'SubsetEmbeddedFonts' => 'true',
    ], 'pdf'
);
$result->saveFiles('/path/to/result/dir');

Advanced techniques

Using ConvertAPI, you can compress your PDFs by passing a path to a local file, the URL of a hosted file, or a file stream. Let's see some examples of these different approaches.

Convert a local file

The simplest and most straightforward way to pass a file to our converter is by specifying a path to your local file. It would look something like this:

ConvertApi::setApiCredentials('api_token');
$result = ConvertApi::convert('compress', [
        'File' => '/path/to/my_file.pdf',
    ], 'pdf'
);
$result->saveFiles('/path/to/result/dir');

Convert a remote file accessible via URL

Another useful feature is converting a remote file hosted on a server accessible via URL. An important thing to notice here is that the file must be publicly available and have appropriate headers set in the response: Content-Type: application/pdf. An example of a code snippet would look like this:

ConvertApi::setApiCredentials('api_token');
$result = ConvertApi::convert('compress', [
        'File' => 'https://cdn.convertapi.com/cara/testfiles/document-large.pdf',
    ], 'pdf'
);
$result->saveFiles('/path/to/result/dir');

Convert a file stream

Converting a file stream provides the best performance benefits. In order to convert a file stream, please use the following example:

$upload = new \ConvertApi\FileUpload($stream, 'test.txt');

$result = ConvertApi::convert('pdf', ['File' => $upload]);
$savedFiles = $result->saveFiles('/path/to/result/dir');

Conclusion

The number of digital documents has grown over the years. It is important to compress these documents properly for sharing and long-term storage. You can compress PDF documents using the PHP programming language with no effort at all! Simply follow the steps described in this article and check out our code examples in the ConvertAPI PHP library on GitHub.

Integrate within minutes

Easy Compress PDF automation using our simple PHP SDK

Try the Compress PDF conversion online

Try it Free

Compatible With all PHP Frameworks & Tools

Compatible with PHP Available on Composer Compatible with Symphony Compatible with Laravel Works well with Guzzle Compatible with PHP Unit Compatible with PHP Storm

Businesses trust us

Highest rated File Conversion API on major B2B software listing platforms: Capterra, G2, and Trustpilot.

"ConvertAPI has been a game-changer for our document automation workflows. Their conversion accuracy and API reliability are unmatched in the industry for over 7 years."

"ConvertAPI is a reliable, cost-effective solution with a proven track record of stability. It has grown significantly in maturity, adopting enterprise-grade practices over the years."

"We've integrated ConvertAPI across our entire document processing platform. The performance is exceptional and the support team is always responsive. Highly recommended!"

Enterprise-Grade Security

We ensure that all document processing is handled securely in the cloud, adhering to industry-leading standards like ISO 27001, GDPR, and HIPAA. To enhance security even further, we can ensure that no files or data are stored on our servers and never leave your country.

Learn more about security

Ready to Streamline Your File Conversions?