如何将HTML文件转换为PDF并使用django将其保存在项目媒体文件夹中

时间:2016-12-10 11:53:33

标签: python django pdf

我必须在这个pdf中从html文件创建动态pdf我传递了一些关键字以及条形码也生成了,基本上这个pdf是事件的票据。

所以我必须将这个pdf保存在项目的媒体foleder中,以便用户可以从他/她的帐户下载

我已经从html文件成功创建了pdf,我已经在stackoverflow上使用了代码并且工作正常,但问题是它在tab中打开,我必须将它保存在目录中。

这是我的代码

def testing_pdf(request):

image = treepoem.generate_barcode(
    barcode_type='azteccode',
    data='00100111001000000101001101111000010100111100101000000110',
    options={'layers':5} 
)
image.save(settings.MEDIA_ROOT+'/barcodes/barcode.png')
template = get_template('home/pdf-template/index.html')
context_dict = { 'name':'Testing','event': 'testing', 'barcode':image}
context = Context(context_dict)
html  = template.render(context)

result = StringIO.StringIO()
pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("utf-8")), result)
if not pdf.err:
    response = HttpResponse(result.getvalue(), content_type='application/pdf')
    return response
return HttpResponse('We had some errors<pre>%s</pre>' % escape(html))

我应该在此代码中执行哪些操作,以便将其保存到项目的媒体目录中。

0 个答案:

没有答案