-1

In my django project, i am creating pdfs in python using pdfkit library. However my pdfs with 15 pages have 16mb size.

my lib

pdfkit==1.0.0 

How can I reduce its size, considering my code above:

import pdfkit from django.template.loader import render_to_string my_html = '' for content in blocktexts: my_html += render_to_string('mypath/myhtml.html') pdfkit.from_string(my_html, False) 

I loop in a variable called blocktexts that i have html contents in it and each one of them should create a pdf page, the problem is that each single page has 1mb, with only text in it and a small image in the begining...

How can I make this pdf smaller?

4
  • How many images are in your file? Remember that EVERY image you display on your site is going to be embedded at full resolution in your PDF. Commented Aug 27, 2024 at 22:56
  • Tim Roberts, inside my html there is only 1 image, is a logo.. but it shouldnt be 1mb for page, seems weird Commented Aug 28, 2024 at 1:26
  • i think that is not the point, the problem relies on transforming, in python pdfkit, my html code into pdf file... i am going to edit my quesiton putting in it Commented Aug 28, 2024 at 1:32
  • hummmmm, nice, i am going to try later, but for now... <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <link href="fonts.googleapis.com/…" rel="stylesheet" /> <title>Simple Transactional Email</title> <style> this is my html file starting, imagine that on the loop i am adding 16 times as you said Commented Aug 28, 2024 at 2:37

1 Answer 1

-1

If pdfkit does not meet your needs, you might explore other libraries like WeasyPrint or xhtml2pdf, which could offer better compression out-of-the-box.

Sign up to request clarification or add additional context in comments.

3 Comments

Why do you think so? There are not a lot of compression options in a PDF file. I find your assertion questionable.
You're correct that PDF compression options are generally limited, and the actual size of the PDF is often more influenced by the content (e.g., images, fonts, and layout complexity) rather than the library used. However, you are right that the differences are not always significant, and the overall file size is more related to the content and how it's structured in the HTML/CSS rather than the library's intrinsic capabilities. My initial suggestion was based on the idea that these libraries might have different default behaviors that could result in smaller PDFs for some use cases.
hey @MARO , that was the best choice.. pdfkit is deprecated and i could do the same thing with it, rendering html and transforming in pdf with smaller pdfs!!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.