使用 Django 和 PIL/Django-Photologue 动态创建和保存图像
我想生成一个包含动态内容的 html 页面,然后将结果保存为图像并向用户显示该页面。因此,用户注册参加会议并给出他们的姓名和组织。该数据与 html/css 元素相结合,以显示他们的会议 ID 徽章的外观(他们的姓名和组织位于我们的会议徽标背景之上)以供预览。获得批准后,该页面将以图像格式(PNG、PDF 或 JPG)保存在服务器上,以便管理员稍后将其打印到物理徽章上。我正在使用由 PIL 提供支持的 Django 和 django-photologue。
视图可能看起来像这样
# 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),
)
模板可能看起来很
{# 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 %}
简单,但是如何保存结果?或者有更好的方法来完成这件事吗?
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 could 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我唯一能想到的就是分两次完成:
a)使用 http://www.xhtml2pdf.com/ 将 HTML 转换为 PDF。
b) 使用类似 http://www.swftools.org/gfx_tutorial.html 进行转换将 PDF 转换为图像。
我无法想象这样做会很快...
您最好直接转换并允许他们下载 PDF(即仅使用上面的步骤 a))或尝试直接生成徽章而不需要 HTML 中间步骤。
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.