GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing thepythoncode.com — How to Compress PDF Files in Python - The Python Code Ready to take Python coding to a new level? Explore our Python Code Generator. The perfect tool to get your code up and running in no time. Start now! Compressing PDF allows you to decrease the file size as small as possible while maintaining the quality of the media in that PDF file. As a result, it significantly increases effectiveness and shareability. In this tutorial, you will learn how to compress PDF files using the PDFTron library in Python. PDFNetPython3 is a wrapper for PDFTron SDK. With PDFTron components, you can build reliable & speedy applications that can view, create, print, edit, and annotate PDFs across various operating systems. Developers use PDFTron SDK to read, write, and edit PDF documents compatible with all published versions of PDF specifications (including the latest ISO32000). PDFTron is not freeware. It offers two licenses depending on whether you're developing an external/commercial product or an in-house solution. We will use the free trial version of this SDK for this tutorial. This tutorial aims to develop a lightweight command-line-based utility through Python-based modules without relying on external utilities outside the Python ecosystem (e.g., Ghostscript) that compress PDF files. Note that this tutorial only works for compressing PDF files and not any file. You can check this tutorial for compressing and archiving files. Read also: How to Compress Images in Python. To get started, let's install the Python wrapper using pip:
Open up a new Python file and import the necessary modules:
Next, let's define a function that prints the file size in the appropriate format (grabbed from this tutorial):
Download: Practical Python PDF Processing EBook. Now let's define our core function:
Get Our Practical Python PDF Processing EBookMaster PDF Manipulation with Python by building PDF tools from scratch. Get your copy now! Download EBookThis function compresses a PDF file by removing redundant information and compressing the data streams; it then prints a summary showing the compression ratio and the size of the file after compression. It takes the PDF Now let's define our main code:
We get the input and output files from the command-line arguments and then use our defined Let's test it out:
The following is the output:
As you can see, a new compressed PDF file with the size of
|
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |