InterServer Web Hosting and VPS
Best Way to Reduce Pdf File Size Using Python

Best Way to Reduce Pdf File Size Using Python

PDF is a powerful file that most often contains advanced functionality and graphic elements, which makes it larger in size than most other files. When you print contents directly from your computer, or send PDF file via email, sometimes the document is too large and needs to be compressed before being sent.

The main purpose in compressing a PDF document is to make it easier to share your pdf file with others over the internet. Obviously we want the pdf file size to be as small as possible for faster downloads. The first method which I am going to show you is using ghostscript library. it’s very fast and efficient but the problem with this is that it is available only on Linux(later I will post another method which can be used on Windows). You can install it by running the following command from your terminal:sudo apt-get install ghostscript

A pdf is a digital file format which can be used to share and view documents. Size of an pdf document is not just an important factor, it measures the space a document takes on a memory device. If you want to send a big pdf file to your colleague then make sure you compress that file.

Pdf file is popular format in the IT industry. The pdf file size is large, so if you want to send the documents to your friend or colleague by email or upload them to an FTP server, it becomes a problem. In this article, I will introduce you to how to reduce PDF file size using Python and show you step-by-step how to compress PDF using Python.

Below, we shall put you through on:

  1. How to use Python to compress PDF file
  2. How to use to compress pdf file
  3. How to compress a pdf file
  4. How to reduce size of a pdf file using online compressors
  5. How to compress a pdf file in Windows
  6. How to compress a pdf in Python

How to use python to compress pdf file?

There are many different ways to compress a pdf file using python. The simplest way is to use the zip() function. This will create a file that is compressed using the zlib library. You can also use the bz2() and gzip() functions to compress files. You can also use the pigpio library to compress pdf files. This library provides a number of ways to compress a pdf file, including using LZW compression algorithms.

We can use, You can download it in here.

This python script use python to call ghostscript to compress pdf file.

How to use to compress pdf file?

You can use compress() function to compress.

power can be:

  1. quality = {
  2. 0: ‘/default’,
  3. 1: ‘/prepress’,
  4. 2: ‘/printer’,
  5. 3: ‘/ebook’,
  6. 4: ‘/screen’
  7. }


/prepress: 300dpi
/printer: 300dpi
/ebook: 150dpi
/screen: 72dpi

For example:

  1. compress(“source.pdf”, ‘compressed.pdf’)

PDF is a great file format to send read-only documents that just look stunning and beautiful with images and good graphic design. It is a common format used by professionals and non-professionals to digitize e-books and documents.

When a PDF has many high-quality images, the file size would be high. If you’re sharing it with a friend or making it available for download on your WordPress website.

It is always a good idea to experiment with multiple information provision formats like ebooks, video, audio or infographics etc.

Whatever your reason may be, we will teach you how to reduce the size of a pdf file by compressing the file using various methods and application.

What will you learn from this tutorial?

You will learn the following from this tutorial:

  1. How to compress a pdf file?
  2. How to reduce the pdf file size using online compressors?
  3. How to compress the pdf file using free windows compressors?
  4. How to compress the pdf file using python?
  5. How to delete a page from a pdf file?

How to compress a pdf file?

PDF files are easy to work with. They can be easily created and compressed using various free methods. Some of them are free online tools, some are third-party windows applications and few for compressing using code.

Use any one of the following methods:

Click on any of above links, read about how to use each one of them.

How to reduce size of a pdf file using online compressors?

Internet had made life easier. You don’t need to download or install any application to reduce the size of a PDF file. You can use the online compressors to compress the file remotely and download it.

Since most people have an internet connection these days, I recommend you to follow this method. Since it is easy and free to implement.

Follow these simple steps to compress a pdf file using online compressors:

Step 1. Go to

Smallpdf is a free collection of pdf manipulation online tools that can compress, edit, crack, and convert pdf files. There is an alternative which you can use:

Or using another alternative free online compressor:

The step to reduce PDF file size is similar to the Smallpdf, simply upload a file and download the compressed file. What’s more, CleverPDF offers a few compressing options, so you choose low, medium, or maximum compression, or even set the custom image to make the PDF smaller without affecting its quality too much.

how to reduce pdf file size using online compressors

Step 2. Choose a pdf file for compression

Either upload a pdf file from the computer or simply drag and drop the pdf file to start compression. As soon as you upload it or drop it in the drag section, the compression starts. After the compression is over, you can select to edit it, lock it with a password, convert it to jpg, and more operations can be performed.

Step 3. Click on the download button

Click on the Download File button to download the compressed pdf file. Smallpdf compressor will also tell you to what extent the pdf file is compressed.

how to reduce pdf file size using online compressors

After you download, the compression of the pdf file is successfully been accomplished.

Read more: How to Remove Password from Adobe PDF Files – Two Simple Methods

How to compress a pdf file in Windows?

Compressing a pdf file in Windows can save you a lot of space. You can use the PDF compression tool to reduce the size of a pdf file by up to 80%. The process is simple: open the pdf file in Windows and click on the “Compress” button. The program will then start compressing the file. After it’s done, you’ll get a smaller pdf file that you can save or share.

The first method where we compress pdf files using online compressors can be used in all operating systems with an internet connection. If you don’t have an internet connection or don’t want to use any online tools, because you’re too cautious about a computer virus, you can use this method instead.

We will use windows applications to compress a pdf file in windows. If you can afford a premium tool, try Acrobat Pro PDF Optimizer. It is a great tool for optimizing PDF files. However, it is not cheap. If you can’t afford it or choose not to buy it, don’t worry. There are several free compressors available for free on the internet.

We recommend you use PDFcompressor for windows. IT is a free and well-made pdf compression tool.

Step 1. Download and install PDF compressor.

Download the PDF compressor software and install it. After you install the application, open it from the all programs menu.

Step 2. Add a file or a folder to select the input pdf file[s].

The second step is to add the pdf files as the input to the windows application. Or alternatively, add an entire folder to compress all the pdf files in the folder.

Step 3. Press the start button to begin the compression.

Then press the start button to begin the compression. It is as simple as that.

how to compress a pdf file using windows applications

Pdf compressor is a free windows application that can compress pdf files very well. However, there are other options available for you as well.

How to compress a pdf in Python?

Python is a popular programming language that has several applications written in it. If you are trying to code an application or an online tool, this is very useful. You can use python applications in the terminal to compress pdf files.

This is an easy to use and the most effective method here since you have more control on the output file.

Here is how you do it:

Step 1. Download pdfsizeopt from GitHub.

Step 2. Use the following command to optimize the pdf files.

~/pdfsizeopt/pdfsizeopt ~/Documents/input.pdf ~/Documents/output.pdf

Step 3. Press enter to start the compression

how to compress a pdf file using python

Things to keep in mind:

  • The output and input path must include the file directories.
  • The input and output files both are in the Documents folder in the commands we used.
  • You can change the input and output paths to whatever you like.
  • If you want to compress pdf files with images, you need to also download image optimizers.
  • This is a great tool if you’re creating python or python based web applications.
  • You also need to add jbig2 and pngout to your pdfsizeopt folder.
  • Download them using the git clone command and change the folder names to jbig2 and pngout instead of pdfsizeopt-jbig2 and pngout-bin

How to delete pages from pdf?

Sometimes, you just want to delete a blank page or an unnecessary page from the pdf file. In order to do that, you don’t need Adobe’s premium tools. You can just use online tools. They are simple to use.

Follow these simple steps to delete a page in pdf file:

Step 1. Go to delete a page tool.

how to delete a page in pdf

Step 2. Upload the pdf file you want to modify

how to delete a page in pdf

Step 3. Delete the page by clicking on the trash bin button.

how to delete a page in pdf

After you delete the page, it disappears from the preview. Select the page you want to delete and press the trash bin to delete the page.

how to delete a page in pdf

Step 4. Click apply changes after you deleted the unrequired pages.

how to delete a page in pdf

The online web application will delete the page and process the file accordingly and then take you to a different page where you can download the pdf file or make further changes like editing and compressing.

Step 5. Click Download File to download the output product.

how to delete a page in pdf

Once they process the pdf file and make the necessary changes you wanted, the file will be ready to be downloaded. Download it unless you want to make further changes or compress it.

Another great alternative is available now. Try StarzSoft’s PDF WIZ that let you convert to and from PDF, and you can even edit the PDF. There’s no limit on file size either!

Pdfc — PDF Compressor

Simple python script to compress PDF.


  • Install dependency Ghostscript. On MacOSX: brew install ghostscript On Windows: install binaries via [official website] (
  • Create a symbolic link if you want to run it everywhere in bash ln -s pdfc
  • Add in PATH environment variable On MacOSX: echo 'export PATH="/absolute/path/of/the/folder/script/:$PATH"' >> ~/.bash_profile


pdfc [-o output_file_path] [-c number] input_file_path

Ex: pdfc -o out.pdf in.pdf


Compress PDF...
Compression by 65%.
Final file size is 1.4MB


  • -c or --compress specifies 5 levels of compression, similar to standard pdf generator level:
    • 0: default – almost identical to /screen
    • 1: prepress – high quality, color preserving, 300 dpi imgs
    • 2: printer – high quality, 300 dpi images
    • 3: ebook – low quality, 150 dpi images
    • 4: screen – screen-view-only quality, 72 dpi images
  • -oor --out specifies the output file path. If not specified, input file will be erased.
  • -bor --backup creates a backup of the original file in case no output is specified to avoid erasing the original file.


Reduce PDF file size with Python, To reduce pdf file size, we can apply some Python script. This simple code will reduce PDF file size. So now we need space to store our pdf files because it has only a small weight and no need of storage space. We know that one pdf document will have large storage space which is difficult for our server and web host.

In the modern era of digital data and files, PDF is one of the most common file formats we are using. PDF’s portability and compatibility over a large array of devices make it attractive, but at the same time, these two things also do not allow to compress it. There are some way to reduce PDF size and optimize for all devices, so it’s easier for you to be able to save pdf.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *