Imagine this scenario – you have a big presentation coming up, and you’ve been working hard for days to get your PDF ready for your website design. But you’re finding out that your PDF file is too big to upload on the website. Do you need a quick and efficient way to compress it without compromising quality?
To compress a PDF file using PHP and ConvertAPI, follow these steps:
- Install the ConvertAPI PHP library by running the composer require command in your terminal:
composer require convertapi/convertapi-php
- Import the ConvertApi library into your PHP code:
use ConvertApiConvertApi;
- Set your ConvertAPI secret key using the setApiSecret() method:
ConvertApi::setApiSecret(‘Secret-Key’);
To get ‘Secret-Key’, all you have to do is create an account with ConvertAPI to get a free API Secret Key and add to code above.
- Use the convert() method to compress the PDF file, passing in the ‘compress’ conversion type and the path to the PDF file as arguments:
$result = ConvertApi::convert(‘compress’, [
‘File’ => ‘/path/to/my_file.pdf’,
], ‘pdf’
);
- Save the compressed PDF file to the desired location using the saveFiles() method:
$result->saveFiles(‘/path/to/result/dir’);
That’s it! You have successfully compressed a PDF file using PHP and ConvertAPI.
Advanced Settings
If you wish to update or remove any compression properties, simply scroll down to Advanced Properties from this page.
There are radio buttons and settings which will dynamically generate code snippets.
No coding experience is required – there are plenty of straightforward options that can be selected in order to incorporate various compression features; like removing Bookmarks, Annotations, Changing Color Image Quality, etc.
You can remove objects like bookmarks, change compression rate, subset fonts, and change the output; its respective parameters will automatically modify existing code.
Here is the complete code with a custom compression level:
ConvertApi::setApiSecret(‘<YOUR SECRET HERE>’);
$result = ConvertApi::convert(‘compress’, [
‘File’ => ‘/path/to/my_file.pdf’,
‘ColorImageCompression’ => ‘jpx’,
‘ColorImageQuality’ => ’70’,
‘ColorImageDownsample’ => ‘true’,
‘ColorImageThresholdDpi’ => ‘150’,
‘ColorImageResampleDpi’ => ’70’,
‘RemoveBookmarks’ => ‘true’,
‘RemoveAnnotations’ => ‘true’,
‘UnembedBaseFonts’ => ‘true’,
‘SubsetEmbeddedFonts’ => ‘true’,
], ‘pdf’
);
$result->saveFiles(‘/path/to/result/dir’);
Using PHP to compress PDF files can be beneficial in several situations, such as:
- Web design and development – as mentioned, large PDF files can be challenging to upload on your website, leading to slow loading times or even timeouts. You can also use other file conversions with PDF compression, such as images to PDF, HTML to PDF, and many others.
- E-commerce – large invoices or statements could take a while to send across the internet, especially in PDF format. Compressing these documents can help your business save time and money by reducing the delivery times of these documents.
- Corporate document sharing – if you’re working with a large organization, chances are you have to share documents every day. Large corporates have their own CMS or ERP that is customizable by various languages, they have to share documents for various operations, and if those documents are of considerable size, it’ll take a lot of time for the documents to be delivered.
- Academic document sharing – students need to share documents often as part of their assignments or research. PDF compression can also help them save time in delivering the documents while ensuring that the quality is still preserved.
- Online marketing – when creating digital ads or campaigns, marketers need to ensure that the documents they share are of good quality and size. Compressing PDF files can help them achieve this with ease.
- Legal documents – when filing for any legal document, data security, and the file size are essential. Compressing PDF with ConvertAPI can help lawyers make sure that the files are smaller and still retain their original quality.
- Improved user experience – Slow loading times and large file sizes can frustrate users, especially on mobile devices. Using PHP to compress PDF files can improve the user experience by making the files faster to load and easier to view.
- Archiving and preservation – PDF files can be used to preserve important documents and records for long-term storage. Using PHP to compress these files, you can reduce the space required for storage and make it easier to access and manage the documents.
How to compress a PDF file without losing quality?
You can compress a PDF file using ConvertAPI’s Compress PDF API while keeping its quality intact. This is done by compressing text, graphics, and images, subsetting fonts, and optimizing document structure.
You can set parameters such as color, grayscale, and monochrome image compression by resampling image DPI and selecting the desired quality. This way, you can achieve your desired compression rate and keep the file’s original quality.
If you want to remove unnecessary elements from the PDF file, such as bookmarks, annotations, forms, page labels, and more, ConvertAPI also offers many Objects removal from API advanced settings. With that, you can easily remove all of these elements from the document without affecting its quality or size.
What is the difference between color image compression, grayscale, and monochrome image compression?
Color image compression is used to reduce the file size while preserving the color information of an image. Grayscale image compression reduces only the brightness information of an image, while monochrome image compression reduces the brightness and color information. Color images generally have larger file sizes than grayscale or monochrome images. For this reason, choosing the proper image compression method before compressing a PDF is essential to obtain the best results.
The parameters used for each compression type differ depending on the desired outcome and can be adjusted accordingly. The higher the quality parameter, the better the quality of the compressed image will be. However, increasing the quality parameter can also increase the file size. Finding a balance between quality and file size is essential when compressing images to achieve the desired outcome.
In Conclusion, compressing a PDF file can be done quickly and easily with ConvertAPI’s Compress PDF API.
This Compress PDF PHP API enables users to compress text, graphics, images, and documents while optimizing document structure for maximal quality. ConvertAPI allows users to adjust quality parameters and provides an easy way to remove unnecessary elements from a PDF file without affecting its size or quality.