librelist archives

« back to archive

Generate PDF files from templates, adding user-specific info

Generate PDF files from templates, adding user-specific info

From:
Marc de Verdelhan
Date:
2011-08-08 @ 09:31
Hello,

I would like to generate user-specific PDF files and send them by e-mail.
I already found how to send files with Flask-Mail and attachments.

But now I don't know how to create a PDF file from a template, adding some
user infos. For instance I would like to generate the PDF file of a contract
(terms of service), filled with infos like the user name or its address.

I heard about asciidoc to build the templates but I don't know how to
integrate it with flask. Does anyone have any idea/example/tutorial/etc. ?

Regards,

--
Marc

Re: [flask] Generate PDF files from templates, adding user-specific info

From:
Ron DuPlain
Date:
2011-08-08 @ 14:47
Hi,

On Mon, Aug 8, 2011 at 5:31 AM, Marc de Verdelhan
<marc.deverdelhan@yahoo.com> wrote:
> But now I don't know how to create a PDF file from a template, adding some
> user infos. For instance I would like to generate the PDF file of a contract
> (terms of service), filled with infos like the user name or its address.
>
> I heard about asciidoc to build the templates but I don't know how to
> integrate it with flask. Does anyone have any idea/example/tutorial/etc. ?

I ended up using xhtml2pdf:
http://pypi.python.org/pypi/xhtml2pdf

It takes HTML and simple CSS and using ReportLab creates a PDF.  This
let me reuse a lot of template includes to build nice looking PDFs.  I
found the project packaging to be a bit confusing, given its a fork
from an earlier project called pisa.  I also found the markup & CSS
support to be spotty, especially with CSS selectors.

That said, once I got into a good workflow (starting from the most
simple PDF possible), I was able to produce exactly what I needed in
short order.  The response times were even low enough to serve
dynamically in a web request.

For examples, the project builds its own PDF doc using itself:
https://github.com/chrisglass/xhtml2pdf/tree/master/doc

-Ron

Re: [flask] Generate PDF files from templates, adding user-specific info

From:
Marc de Verdelhan
Date:
2011-08-10 @ 15:47
Hi,

Thank you for all your answers. :]

@Baiju,
wkhtmltopdf requires flashplugin-nonfree. I prefer to avoid such
dependencies.

@Daniele,
I was looking for a Python library, but ReportLab seemed to be a bit tricky.
Moreover, if I'm not mistaken, templates are only available in the Pro
version.

I finally got it using xhtml2pdf. The result can be found here:
http://paste.pocoo.org/show/456022/
@Ron,
I agree with your comments about project packaging or examples. It's not
very well documented but starting from a simple template I got exactly what
I was looking for.

Thank you again.

Regards,

--
Marc

On Mon, Aug 8, 2011 at 4:47 PM, Ron DuPlain <ron.duplain@gmail.com> wrote:

> Hi,
>
> On Mon, Aug 8, 2011 at 5:31 AM, Marc de Verdelhan
> <marc.deverdelhan@yahoo.com> wrote:
> > But now I don't know how to create a PDF file from a template, adding
> some
> > user infos. For instance I would like to generate the PDF file of a
> contract
> > (terms of service), filled with infos like the user name or its address.
> >
> > I heard about asciidoc to build the templates but I don't know how to
> > integrate it with flask. Does anyone have any idea/example/tutorial/etc.
> ?
>
> I ended up using xhtml2pdf:
> http://pypi.python.org/pypi/xhtml2pdf
>
> It takes HTML and simple CSS and using ReportLab creates a PDF.  This
> let me reuse a lot of template includes to build nice looking PDFs.  I
> found the project packaging to be a bit confusing, given its a fork
> from an earlier project called pisa.  I also found the markup & CSS
> support to be spotty, especially with CSS selectors.
>
> That said, once I got into a good workflow (starting from the most
> simple PDF possible), I was able to produce exactly what I needed in
> short order.  The response times were even low enough to serve
> dynamically in a web request.
>
> For examples, the project builds its own PDF doc using itself:
> https://github.com/chrisglass/xhtml2pdf/tree/master/doc
>
> -Ron
>

Re: [flask] Generate PDF files from templates, adding user-specific info

From:
Marc de Verdelhan
Date:
2011-08-10 @ 15:54
I forgot to mention that I used Eventlet to perform the PDF generation and
the mail sending. Celery seems a bit much for so little. Any opinion about
that? Do you think Eventlet is good enough?

--
Marc

On Wed, Aug 10, 2011 at 5:47 PM, Marc de Verdelhan <
marc.deverdelhan@yahoo.com> wrote:

> Hi,
>
> Thank you for all your answers. :]
>
> @Baiju,
> wkhtmltopdf requires flashplugin-nonfree. I prefer to avoid such
> dependencies.
>
> @Daniele,
> I was looking for a Python library, but ReportLab seemed to be a bit
> tricky. Moreover, if I'm not mistaken, templates are only available in the
> Pro version.
>
> I finally got it using xhtml2pdf. The result can be found here:
> http://paste.pocoo.org/show/456022/
> @Ron,
> I agree with your comments about project packaging or examples. It's not
> very well documented but starting from a simple template I got exactly what
> I was looking for.
>
> Thank you again.
>
> Regards,
>
> --
> Marc
>
>
> On Mon, Aug 8, 2011 at 4:47 PM, Ron DuPlain <ron.duplain@gmail.com> wrote:
>
>> Hi,
>>
>> On Mon, Aug 8, 2011 at 5:31 AM, Marc de Verdelhan
>> <marc.deverdelhan@yahoo.com> wrote:
>> > But now I don't know how to create a PDF file from a template, adding
>> some
>> > user infos. For instance I would like to generate the PDF file of a
>> contract
>> > (terms of service), filled with infos like the user name or its address.
>> >
>> > I heard about asciidoc to build the templates but I don't know how to
>> > integrate it with flask. Does anyone have any idea/example/tutorial/etc.
>> ?
>>
>> I ended up using xhtml2pdf:
>> http://pypi.python.org/pypi/xhtml2pdf
>>
>> It takes HTML and simple CSS and using ReportLab creates a PDF.  This
>> let me reuse a lot of template includes to build nice looking PDFs.  I
>> found the project packaging to be a bit confusing, given its a fork
>> from an earlier project called pisa.  I also found the markup & CSS
>> support to be spotty, especially with CSS selectors.
>>
>> That said, once I got into a good workflow (starting from the most
>> simple PDF possible), I was able to produce exactly what I needed in
>> short order.  The response times were even low enough to serve
>> dynamically in a web request.
>>
>> For examples, the project builds its own PDF doc using itself:
>> https://github.com/chrisglass/xhtml2pdf/tree/master/doc
>>
>> -Ron
>>
>
>

Re: [flask] Generate PDF files from templates, adding user-specific info

From:
Daniele Nicolodi
Date:
2011-08-08 @ 10:12
On 08/08/11 11:31, Marc de Verdelhan wrote:
> I would like to generate user-specific PDF files and send them by e-mail.
> I already found how to send files with Flask-Mail and attachments.
> 
> I heard about asciidoc to build the templates but I don't know how to
> integrate it with flask. Does anyone have any idea/example/tutorial/etc. ?

Hello,

to generate high quality PDF I recommend to use pdflatex or reportlab.

The first must be invoked as a separate process, and the processing of a
complex source file file may take some time, so it may be a good idea to
use something like celery to have the PDF production been handled
asynchronously. With the hyperef package you can generate also PDF forms.

Reportlab is a python library, and I have very little experience with
it. The processing times can be large also in this case, however.

Cheers,
-- 
Daniele

Re: [flask] Generate PDF files from templates, adding user-specific info

From:
Baiju M
Date:
2011-08-08 @ 09:46
On Mon, Aug 8, 2011 at 3:01 PM, Marc de Verdelhan
<marc.deverdelhan@yahoo.com> wrote:
> Hello,
>
> I would like to generate user-specific PDF files and send them by e-mail.
> I already found how to send files with Flask-Mail and attachments.
>
> But now I don't know how to create a PDF file from a template, adding some
> user infos. For instance I would like to generate the PDF file of a contract
> (terms of service), filled with infos like the user name or its address.
>
> I heard about asciidoc to build the templates but I don't know how to
> integrate it with flask. Does anyone have any idea/example/tutorial/etc. ?

You can evaluate wkhtmltopdf (You can use it as a command-line tool):
http://code.google.com/p/wkhtmltopdf/

Also there is a Python wrapper:
http://pypi.python.org/pypi/wkhtmltopdf/0.2

Regards,
Baiju M