Skip to content

A Python tool to compress PDF files by downscaling and compressing embedded images. Uses PyMuPDF and Pillow to optimize file size, making PDFs easier to store and share while preserving layout and quality.

License

Notifications You must be signed in to change notification settings

towfique-elahe/pdf-compressor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

PDF Compression Tool with Image Optimization

Description:

A Python tool designed to reduce PDF file size by compressing and downscaling images within the PDF. This program uses PyMuPDF to access and manipulate PDF images and Pillow to resize and compress image quality. Ideal for reducing large PDF files containing high-resolution images, making them more manageable for storage, sharing, or web uploads.

Features:

  • Image Downscaling and Compression: Reduces the resolution of images embedded within PDF pages based on a user-defined DPI (default 72 DPI), which results in smaller file sizes.
  • JPEG Quality Adjustment: Enables control over JPEG quality (0-100), allowing users to balance between file size and image quality.
  • Flexible Output: Saves the compressed file as a new PDF, preserving the layout and structure while optimizing for size.
  • Easy Customization: Parameters for DPI and JPEG quality allow users to tailor the compression level according to their needs.

Technical Details:

Dependencies:

  • PyMuPDF (fitz): For accessing and manipulating PDF file structure, extracting and replacing images on each page.
  • Pillow (PIL): For image manipulation, resizing, and quality adjustment.

Key Parameters:

  • dpi: Controls the resolution for image downscaling (default is 72 DPI; higher values result in less compression).
  • quality: JPEG compression quality (default is 50, with 0 being lowest quality and 100 being maximum).

Usage Example:

  1. Install dependencies with pip install pymupdf pillow.
  2. Run the compress_pdf function, specifying the input and output PDF paths, desired DPI, and quality.
  3. The output file will be significantly reduced in size, especially for image-heavy PDFs.

Usage Example

To compress a PDF, use the following code:

# Define paths and compression settings
input_pdf = "path/to/large_file.pdf"
output_pdf = "compressed_file.pdf"
desired_dpi = 72  # Lower DPI for higher compression
jpeg_quality = 50  # Lower quality for higher compression

# Compress the PDF
compress_pdf(input_pdf, output_pdf, dpi=desired_dpi, quality=jpeg_quality)

About

A Python tool to compress PDF files by downscaling and compressing embedded images. Uses PyMuPDF and Pillow to optimize file size, making PDFs easier to store and share while preserving layout and quality.

Topics

Resources

License

Stars

Watchers

Forks