如何从Python中的HTML / CSS(包括图像)源生成PDF?

时间:2022-06-01 03:31:59

Let's say I've got an HTML / CSS page with some images in it, and I wanted to generate a PDF from that source in Python - possible?

假设我有一个带有一些图像的HTML / CSS页面,我想用Python生成一个来自该源的PDF - 可能吗?

3 个解决方案

#1


http://www.xhtml2pdf.com/

install was a little quirky for me, but otherwise it worked fine.

安装对我来说有点古怪,但除此之外它工作正常。

#2


You can do something like this using Pisa:

你可以用比萨做这样的事情:

def receipt(request, id):
    import ho.pisa as pisa
    from django.template.loader import render_to_string
    from datetime import datetime

    r = get_or_404(id, request.affiliate)    
    now = datetime.now()
    contents = render_to_string('home/reservations/receipt.html', {
        'reservation': r,
        'today': now
    })
    filename = now.strftime('%Y-%m-%d') + '.pdf'
    response = HttpResponse(mimetype='application/pdf')
    response['Content-Disposition'] = 'attachment; filename=' + filename
    pdf = pisa.CreatePDF(contents, response)
    if pdf.err:
        message(request, 'Unable to generate the receipt.')
        return HttpResponseRedirect(reverse('view_reservation', args=[r.id]))    
    else:
        return response

(This is a Django view that generates a receipt but obviously you can use Pisa in whatever setting)

(这是一个生成收据的Django视图,但显然你可以在任何设置中使用Pisa)

You're going to have to tweak the HTML to make it play as nice as possible with Pisa, though.

不过,你将不得不调整HTML以使它尽可能地与Pisa一起玩。

#3


There is wkhtmltopdf a possibly better option which I recently started using for my project. It not only supports almost complete CSS but also javascript. Try wkhtmltopdf command first to understand it's power. Then you use it's python extension.

wkhtmltopdf是我最近开始用于我的项目的更好的选择。它不仅支持几乎完整的CSS,还支持javascript。首先尝试使用wkhtmltopdf命令来了解它的强大功能。然后你使用它的python扩展。

Here are links

这是链接

It was slightly tricky to install for me. So I wrote this quick and dirty script.

安装对我来说有点棘手。所以我写了这个快速而又脏的脚本。

#1


http://www.xhtml2pdf.com/

install was a little quirky for me, but otherwise it worked fine.

安装对我来说有点古怪,但除此之外它工作正常。

#2


You can do something like this using Pisa:

你可以用比萨做这样的事情:

def receipt(request, id):
    import ho.pisa as pisa
    from django.template.loader import render_to_string
    from datetime import datetime

    r = get_or_404(id, request.affiliate)    
    now = datetime.now()
    contents = render_to_string('home/reservations/receipt.html', {
        'reservation': r,
        'today': now
    })
    filename = now.strftime('%Y-%m-%d') + '.pdf'
    response = HttpResponse(mimetype='application/pdf')
    response['Content-Disposition'] = 'attachment; filename=' + filename
    pdf = pisa.CreatePDF(contents, response)
    if pdf.err:
        message(request, 'Unable to generate the receipt.')
        return HttpResponseRedirect(reverse('view_reservation', args=[r.id]))    
    else:
        return response

(This is a Django view that generates a receipt but obviously you can use Pisa in whatever setting)

(这是一个生成收据的Django视图,但显然你可以在任何设置中使用Pisa)

You're going to have to tweak the HTML to make it play as nice as possible with Pisa, though.

不过,你将不得不调整HTML以使它尽可能地与Pisa一起玩。

#3


There is wkhtmltopdf a possibly better option which I recently started using for my project. It not only supports almost complete CSS but also javascript. Try wkhtmltopdf command first to understand it's power. Then you use it's python extension.

wkhtmltopdf是我最近开始用于我的项目的更好的选择。它不仅支持几乎完整的CSS,还支持javascript。首先尝试使用wkhtmltopdf命令来了解它的强大功能。然后你使用它的python扩展。

Here are links

这是链接

It was slightly tricky to install for me. So I wrote this quick and dirty script.

安装对我来说有点棘手。所以我写了这个快速而又脏的脚本。