Compress PDF using python step by step
Compress PDF using python step by step
1. Method
Here's a step-by-step guide to compress a PDF file using Python:
Install the PyPDF2 library:
pip install PyPDF2
Import the PyPDF2 library in your Python script:
python
pip install Py pip Copy code
import PyPDF2
Open the PDF file you want to compress:
makefile
pdf_file = open("original.pdf", "rb")
pdf_reader = PyPDF2.PdfFileReader(pdf_file)
Create a new PDF file to store the compressed version:
makefile
Copy code
pdf_writer = PyPDF2.PdfFileWriter()
Loop through each page of the original PDF file and add it to the new PDF file, setting the compression level:
scss
for page_num in range(pdf_reader.numPages):
page = pdf_reader.getPage(page_num)
page.compressContentStreams()
pdf_writer.addPage(page)
Save the compressed PDF file:
lua
compressed_pdf = open("compressed.pdf", "wb")
pdf_writer.write(compressed_pdf)
compressed_pdf.close()
pdf_file.close()
That's it! You now have a compressed version of your PDF file.
Complete code
Here's the complete code for compressing a PDF file using Python and the PyPDF2 library:
python
import PyPDF2
# Open the original PDF file
pdf_file = open("original.pdf", "rb")
pdf_reader = PyPDF2.PdfFileReader(pdf_file)
# Create a new PDF file to store the
compressed version
pdf_writer = PyPDF2.PdfFileWriter()
# Loop through each page of the original PDF
# file and add it to the new PDF file
for page_num in range(pdf_reader.numPages):
page = pdf_reader.getPage(page_num)
page.compressContentStreams()
pdf_writer.addPage(page)
# Save the compressed PDF file
compressed_pdf = open("compressed.pdf", "wb")
pdf_writer.write(compressed_pdf)
# Close both PDF files
compressed_pdf.close()
pdf_file.close()
2. Method
Another way to compress a PDF file using Python is to use the pdfminer library. The pdfminer library provides the pdf2pdf script that can be used to compress a PDF file. Here's an example of how to use the pdf2pdf script to compress a PDF file:
Install the pdfminer library:
Copy code
pip install pdfminer
Run the pdf2pdf script on the command line, specifying the input and output files and the desired compression level:
pdf2pdf -dNOPAUSE -dBATCH -sDEVICE=pdfwrite
-dCompatibilityLevel=1.4
-dPDFSETTINGS=/ebook -sOutputFile=compressed.pdf
original.pdf
This will compress the original.pdf file and save the compressed version to compressed.pdf. The -dPDFSETTINGS option sets the compression level, with /ebook providing the highest compression and /prepress providing the lowest compression.
Note that this method is a command-line tool, not a Python library, and therefore requires using the command line to run the script
Complete code
As mentioned earlier, this method of compressing a PDF file using the pdfminer library is a command-line tool and not a Python library, so there is no code to write in Python. You just need to run the following command in your terminal or command prompt:
bash
pdf2pdf -dNOPAUSE -dBATCH -sDEVICE=pdfwrite
-dCompatibilityLevel=1.4
-dPDFSETTINGS=/ebook -sOutputFile=compressed.pdf
original.pdf
This command will compress the original.pdf file and save the compressed version to compressed.pdf. The -dPDFSETTINGS option sets the compression level, with /ebook providing the highest compression and /prepress providing the lowest compression
Thanks 🙏
ReplyDeleteThanks 👍
ReplyDelete