best way to generate PDFs in django - handling concurrency

194 views
Skip to first unread message

Danny Blaker

unread,
Dec 27, 2018, 6:05:17 PM12/27/18
to Django users
we're building an app for the council where users fill in a form, then we generate a PDF (containing a page of text), and users get a download link on the homepage. 
we expect many users to submit forms concurrently.

I see 2 approaches:

1. Generate in the view as per documentation : https://docs.djangoproject.com/en/2.1/howto/outputting-pdf/
2. Use a seperate script to generate PDF and use django api https://www.django-rest-framework.org/api-guide/parsers/#fileuploadparser

However, to handle concurrency we'll also need a broker - like rabbitMQ + Celery

Is there a "best practice" way to approach this, or has anyone had experience with generating PDFs in django and can recommend an approach?

Thanks!!

resources:



Peter van der Does

unread,
Dec 27, 2018, 11:29:51 PM12/27/18
to django...@googlegroups.com

Best way is relative.

Simplified we do the following:

  • Use Django Templates to create a HTML page.
  • Save the HTML output as a file.
  • Use subprocess to call wkhtmltopdf to convert the HTML file to PDF file.
  • Return a PDF response using the saved PDF file

It works, not sure if it is the fastest way, we see about a 4-5 sec response time. As some of the generated PDF files can be reused we actually keep the PDF file on disk and reuse them for other recurring requests. This bypasses the generation of PDF file bring the response time back to 1 sec.

By using Celery you don't have an exact time of when a PDF will be generated.

There are several commercial applications available to generate PDF's which might be much faster but for us the open source application works fine.

wkhtmltopdf: https://wkhtmltopdf.org/

--
Peter van der Does
o: 
410-584-2500
ONeil Interactive, Inc 

Danny Blaker

unread,
Dec 28, 2018, 2:02:32 AM12/28/18
to Django users
Thanks!

Carsten Fuchs

unread,
Dec 28, 2018, 2:05:41 AM12/28/18
to django...@googlegroups.com
Hello,

in my project, using ReportLab exactly as documented works very well.

Best regards,
Carsten


Am 28.12.18 um 00:05 schrieb Danny Blaker:
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9f06d9aa-7d89-4700-8cd0-9ae3343b3128%40googlegroups.com <https://groups.google.com/d/msgid/django-users/9f06d9aa-7d89-4700-8cd0-9ae3343b3128%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

--
Dipl.-Inf. Carsten Fuchs
Industriegebiet 3 ℅ Rofu
55768 Hoppstädten-Weiersbach
https://www.cafu.de

mazz ahmed

unread,
Dec 28, 2018, 2:05:59 AM12/28/18
to django...@googlegroups.com
Use wkhtmltopdf package for that bro. 

On Fri, Dec 28, 2018 at 12:02 PM Danny Blaker <dzu...@gmail.com> wrote:
Thanks!


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.

Danny Blaker

unread,
Dec 28, 2018, 10:47:00 PM12/28/18
to Django users
Great. Thanks

Shashank Singh

unread,
Dec 28, 2018, 10:51:32 PM12/28/18
to django...@googlegroups.com
Celery + django + rabbitmq + template+ django-hardcopy



On Sat, 29 Dec, 2018, 9:17 AM Danny Blaker <dzu...@gmail.com wrote:
Great. Thanks


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages