skip to Main Content

I have generated a PDF file through python using pdfkit library running on django server.

The size of pdf generated is 43.2 MB.

Total pages in pdf = 15.

Each page has 70 images, each image size = 623 bytes.

Tech Stack version used-

-Python = 3.8.16

-pdfkit = 1.0.0

-wkhtmltopdf = 0.12.6

-Django = 3.2.16

System OS = Ubuntu 22.04.2 LTS

Requirement is to compress this pdf file without compromising with the quality of content, images in it.

Any approach or suggestions for improvements in the things tried?

Things tried:

  1. PDFNetPython3 python package- requires license/key file.
  2. Ghost-script command from system(ubuntu) terminal to reduce file.
  3. shrinkpdf.sh – file got from github.
  4. ps2pdf command from system terminal.
  5. pdfsizeopt
  6. cpdf
  7. Making zip of original file
  8. Changing dpi value in pdfkit options.

File size after compression with quality of content maintained = 23.7 MB.
Expectation is have more reduction in file size.

2

Answers


  1. Compress images in options:

    pdfkit.from_url('http://google.com', 'out.pdf', options={"image-quality": 30, "lowquality": True})

    You can see other options you can pass here:

    https://wkhtmltopdf.org/usage/wkhtmltopdf.txt

    Good luck!

    Login or Signup to reply.
  2. I had a similar problem – one report generated with pdfkit was resulting in a 32mb file that I couldn’t deliver through discord.
    I ended up changing my code to create the report using fpdf2 and the report now is less than 2mb

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search