Compress PDF documents using PHP programming language
PDF Compression became more important than ever in this digital document processing age. More and more digital documents are produced everyday which results in a bulky and space consuming PDF storage. ConvertAPI's PDF Compression tool allows you to shrink the PDF's size using 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 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 password-protected documents. Without further ado, let's jump straight into some code examples!
How to use PHP programming language to compress a PDF?
- Install the ConvertAPI library for PHP into your project
- Sign up for a free account and receive your API secret
- Set up the conversion using our interactive UI
- 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:
Please note that the library uses
json dependencies. Using Composer it will be handled automatically.
If installing manually, please make sure that these extensions are installed.
2. Retrieve your API Secret
You need to get the API Secret key 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 Secret Key in your account dashboard.
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 PHP programming language will look something like this:
ConvertApi::setApiSecret('your-api-secret'); $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');
Using ConvertAPI you can compress your PDFs by passing a path to local file, the URL of a hosted file, or a file stream. Lets 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::setApiSecret('your-api-secret'); $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:
An example of a code snippet would look like this:
ConvertApi::setApiSecret('your-api-secret'); $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');
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 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. If you still have questions, do not hesitate to contact our friendly customer support!