You can check Celery. The idea is to use asynchronous task queue/job queue based on distributed message passing using a broker like RabbitMQ or Redis
http://www.celeryproject.org/
Ideally, your background task should run on servers different from the application servers so that they can scale independently and won't affect each other.
Answer from randy on Stack Overflowdjango - Python Weasyprint - Stack Overflow
django-bootyprint: A django pdf rendering app for WeasyPrint with a CSS companion
Pdfkit vs weasyprint ?
Django convert HTML to PDF using "django-weasyprint"
Hi,
Here's a simple example using function based views.
def download_my_pdf (request, template="yourpdf.html"):
from weasyprint import HTML, CSS
from django.shortcuts import HttpResponse
pdf_html = render_to_string(template, locals())
pdf_file = HTML(string=pdf_html).write_pdf(stylesheets=[CSS(string='@page { size: letter portrait; margin: 1cm }')])
response = HttpResponse(pdf, content_type='application/pdf')
response['Content-Disposition'] = 'filename="mypdf.pdf"'
return responseJust put something in the yourpdf.html template, create a line urls.py to point to this view, for example with :
re_path(r'^download_my_pdf/$', myviews.download_my_pdf, name="download_my_pdf"),
it should download a PDF when you visit the url.
More on reddit.comWhy are my images and CSS missing in the WeasyPrint PDF?
Does WeasyPrint run JavaScript when generating a PDF?
Can WeasyPrint fill in or edit an existing PDF form?
You can check Celery. The idea is to use asynchronous task queue/job queue based on distributed message passing using a broker like RabbitMQ or Redis
http://www.celeryproject.org/
Ideally, your background task should run on servers different from the application servers so that they can scale independently and won't affect each other.
converting html to doc
from weasyprint import HTML
def render_to_doc(template_src, context_dict={}):
template = get_template(template_src)
HTML = template.render(context_dict)
timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
file_name = f'promissorynote_{timestamp}.doc'
# saving pdf in media rootfile
pdf_path = os.path.join(settings.MEDIA_ROOT, 'doc', file_name)
HTML(string=html).write_pdf(pdf_path)
return file_name
Hi,
I'd like to introduce a generic library to create PDF documents with WeasyPrint.
This django app has always the latest BootyPrint CSS framework bundled, so you can just load the css and use css classes similar to Bootstrap.
Source: https://github.com/SvenBroeckling/django-bootyprint
What My Project Does
This django app contains a low level generate_pdf function to create a WeasyPrint PDF file from HTML source. This is extended by Django mechanics like a Response class for easy returning PDF from a View as well as template tags.
Its companion is the BootyPrint CSS framework which resembles Bootstrap, but for print media created with WeasyPrint.
With the template tag {% bootyprint_css %} in the template, a lot of Bootstrap style classes are available.
Future plans
This library will be extended in the future. Planned features are:
-
Rendering of PDF previews/thumbnails as png
-
Providing more control over the render process. Rendering in memory (instead of the current temp file)
Target Audience
This is a library used in production. It is used to create roleplay rule books, character sheets and job application letters.
Comparison
Alternatives are:
-
django-weasyprint
I have a pretty heavy website and want to generate pdf tickets , can be up to 20 per user per order, I already have pdfkit installed and working nicely with a small users number. But as my website grows I ask myself what is the better option between those two? Or is there a better third option …
My main concern is scalability and performance, my setup is a docker compose with django and celery for async pdf generation, hosted on aws elastic beanstalk with their auto scaling.
Can someone share his experience with pdf generation in django ?
Thanks guys for reading
» pip install django-weasypdf
Hello everyone, so for the past 3 nights Ive been trying to figure out how to export an HTML template to a PDF. I managed to make it work with "xhtml2pdf" but it seems its funcionality regarding styling is very limited and not very practical.
I found something called "django-weasyprint" which is a " A Django class-based view generating PDF responses using WeasyPrint. "
If you go to Weasyprint website, the PDFs are very nice, thats what Im aiming for.
The problem is, I cant manage to make it work with "django-weasyprint" which is supposed to make it easier to convert from html to pdf, instead of using "weasyprint" from scratch.
The "django-weasyprint" docs:
django-weasyprint/README.rst at main · fdemmer/django-weasyprint (github.com)
In the "Usage" section, I can't understand completely what they are saying, I'm a noob in web dev, but used class based views before, django REST framework etc. Here what they say:
"Usage
Use WeasyTemplateView as class based view base class or the just the mixin WeasyTemplateResponseMixin on a TemplateView (or subclass thereof)."
and the main "weasyprint" site is:
WeasyPrint
Can someone help me with a basic example of how to use it, I dont want super fancy code just enough to understand how it works and display a pdf from an html template.
Thanks for any help, good night!