开发者

Dynamically create and save image with Django and PIL/Django-Photologue

开发者 https://www.devze.com 2022-12-23 00:56 出处:网络
I want to generate a page of html with dynamic content and then save the result as an image as well as display the page to the user. So, user signs up to attend conference and gives their name and org

I want to generate a page of html with dynamic content and then save the result as an image as well as display the page to the user. So, user signs up to attend conference and gives their name and org. That data is combined with html/css elements to show what their id badge for the conference will look like (their name and org on top of our conference logo background) for preview. Upon approval, the page is saved on the server to an image format (PNG, PDF or JPG) to be printed onto a physical badge by an admin later. I am using Django and django-photologue powered by PIL.

The view might look like this

# app/views.py

def badgepreview(request, attendee_id):
    u = User.objects.get(id=attendee_id)
    name = u.name
    org = u.org

    return render_to_response('app/badgepreview.html',
        {'name':name,'org':org,},
        context_instance = RequestContext(request),
    )

The template c开发者_如何学JAVAould look like this

{# templates/app/badgepreview.html #}
{% extends "base.html" %}
{% block page_body %}
    <div style='background:url(/site_media/img/logo_bg.png) no-repeat;'>
        <h4>{{ name }}</h4>
        <h4>{{ org }}</h4>
    </div>
{% endblock %}

simple, but how do I save the result? Or is there a better way to get this done?


The only thing I can think is to do it in two passes:

a) Use http://www.xhtml2pdf.com/ to convert the HTML into a PDF.

b) Use something like http://www.swftools.org/gfx_tutorial.html to convert the PDF into an image.

I can't imagine that doing this would be fast...

You might be better off just converting and allowing them to download a PDF (i.e. use just step a) above) or trying to generate the badge directly without the HTML intermediate step.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号