Forum


Replies: 1   Views: 226
How to compress pdfs and reduce file size ?
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by Coco-Mrtn  · 23-05-2024 - 07:07

Hello,

I use MultiMerge() to merge several DOCX files, then I transform them into PDFs with TransformDocAdvMSWord().

Is there a possibility to compress the final PDF or choose the quality?

For example, with a 3-page document, the DOCX from the MultiMerge weighs 28KB, and the PDF is 516KB, which is too large.

I tested compressing the final PDF with an online compressor, and the result was 55KB. However, I would love to compress it directly in my PHP if possible.

Thanks for your help.

Posted by admin  · 23-05-2024 - 07:54

Hello,

Unfortunately, MS Word doesn't include options to reduce or optimize the PDF output size. In this case, the best approach would be using an external tool installed on the server, such as GhostsScript:

https://www.digitalocean.com/community/tutorials/reduce-pdf-file-size-in-linux

https://gist.github.com/ahmed-musallam/27de7d7c5ac68ecbd1ed65b6b48416f9

GhostScript is available for Windows, Linux and macOS and you can run it from PHP using shell_exec.

Please note that in most cases, a large PDF file size is due to the images. As alternative approach to using GhostScript, you could try to optimize the DOCX file with optimizeDocx (using imageFiles and imageFilesToJpegLevel options to optimize the images) before being transformed to PDF.

Regards.