# Summary #Īs you can see, a new compressed PDF file with the size of 498KB instead of 757KB. The following is the output: PDFNet is running in demo mode. Let's test it out: $ python pdf_compressor.py bert-paper.pdf bert-paper-min.pdf We simply get the input and output files from the command-line arguments and then use our defined compress_file() function to compress the PDF file. # Parsing command line arguments entered by user Now let's define our main code: if _name_ = "_main_": It takes the PDF input_file and produces the compressed PDF output_file. This 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. "Compression Ratio": "".format(i, j) for i, j in ems())) "Output File": output_file, f"Compressed Size": get_size_format(compressed_size), "Input File": input_file, "Initial Size": get_size_format(initial_size), Ratio = 1 - (compressed_size / initial_size) # Reduce PDF size by removing redundant information and compressing data streamsĭoc.Save(output_file, SDFDoc.e_linearized)Ĭompressed_size = os.path.getsize(output_file) Initial_size = os.path.getsize(input_file) Now let's define our core function: def compress_file(input_file: str, output_file: str): Next, let's define a function that prints the file size in the appropriate format (grabbed from this tutorial): def get_size_format(b, factor=1024, suffix="B"):įor unit in : Open up a new Python file and import necessary modules: # Import Librariesįrom PDFNetPython3.PDFNetPython import PDFDoc, Optimizer, SDFDoc, PDFNet To get started, let's install the Python wrapper using pip: $ pip install PDFNetPython3=8.1.0 Read also: How to Compress Images in Python. You can check this tutorial for compressing and archiving files. Note that this tutorial only works for compressing PDF files and not any file. The goal of this tutorial is 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. We will use the free trial version of this SDK for this tutorial. It offers two types of licenses depending on whether you're developing an external/commercial product or an in-house solution. Developers use PDFTron SDK to read, write, and edit PDF documents compatible with all published versions of PDF specifications (including the latest ISO32000). With PDFTron components, you can build reliable & speedy applications that can view, create, print, edit, and annotate PDFs across various operating systems. PDFNetPython3 is a wrapper for PDFTron SDK. In this tutorial, you will learn how to compress PDF files using the PDFTron library in Python. As a result, it significantly increases effectiveness and shareability. These styli will help you create a clean signature and attractive annotations.Disclosure: This post may contain affiliate links, meaning when you click the links and make a purchase, we receive a commission.Ĭompressing PDF allows you to decrease the file size as small as possible while maintaining the quality of the media in that PDF file. If you're going to mark up a PDF, you'll want a good stylus. It requires a monthly subscription but has an extensive list of features and works well. It's the industry standard when it comes to PDFs, and allows you to view, edit mark up, convert, and share PDFs. If you need a more powerful tool, you should grab Adobe Acrobat (opens in new tab). These features put together with it being free earn it the top spot on this list. You can also use it to save digital signatures to speed up the process of signing documents. Its digital inking support makes it simple to add annotations and highlights to documents. If you just want an app to view PDFs and make light edits, Xodo (opens in new tab) is an excellent option. The PDF application that's best for you depends greatly on what you need to do with your PDFs. PDFs are a popular format for documents that require signatures as well as books, textbooks, and documents. Also, keep an eye for sales as it is often up to 80 percent off. This is an excellent app for managing PDFs but is not a PDF reader, so you'll need to use it in conjunction with another app on this list. You can also use it to add or remove passwords and insert pages into documents. This app allows you to merge, split, extract, and combine PDFs.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |