django pdf与标志图像 [英] django pdf with logo image
本文介绍了django pdf与标志图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
def generate_invoice(request,user_id = None):
personal_html =''
personal_html + ='hai'
fileread = str(settings.TEMPLATE_DIRS [0])+ str('/ invoice.html')
fr = open(fileread,r 。$(
$ b fr = fr.replace('personal_details',personal_html)
result = StringIO.StringIO()
pdf = pisa.CreatePDF(
fr,result
filewrite = str(settings.TEMPLATE_DIRS [0])+ str('/ invoice_write.html')
empty =
fw = open(filewrite,'w'
fw.write(空)
fw.write(fr)
fw.close()
PaymentPdf.objects.filter(invoicepdf = user_id).delete()
pdf_contents = render_to_pdf1('invoice_write.html',result)
file_to_be_saved = ContentFile(pdf_contents)
random_str =''.join(random.sample((string.ascii_lowercase + string.ascii_uppercase + string.digits),8))
resume_name =(str(user_id)+_+ random_str +.pdf)replace(@,'')
resume = PaymentPdf.objects .create(name = resume_name,invoicepdf_id = user_id,created_by = request.user)
resume.name.save(resume_name,file_to_be_saved)
file_path = PaymentPdf.objects.get(invoicepdf = user_id).name
pdf_file = str(file_path).split(media)[1]
return HttpResponseRedirect('/ site_media'+ pdf_file)
def render_to_pdf1(template_src,context_dict):
template = get_template(template_src)
context = Context(context_dict)
html = template.render(context)
result = StringIO.StringIO()
pdf = pisa。 pisaDocument(StringIO.StringIO(html.encode(ISO-8859-1)),结果)
return result.getvalue()
任何人都可以帮我在pdf中显示图片?
解决方案
参考比萨(XHTML - > PDF)在Django中不会显示PDF的图像,并添加行
links = lambda uri,rel: os.path.join(settings.MEDIA_ROOT,uri.replace(settings.MEDIA_URL,''))
pdf = pisa.pisaDocument(StringIO.StringIO(html.encode(UTF-8)),dest =结果,link_callback = links)
在render_to_pdf1的视图中,
和html: p>
< img src =/ site_media / img / logo.jpg/ height =55width =275>
它会工作。
I have created Invoice Pdf text template using pisa. But I want to display logo(image) in the pdf file along with the text. I dont know how to display image in pdf. My views as follows:
def generate_invoice(request, user_id = None):
personal_html = ''
personal_html += 'hai'
fileread = str(settings.TEMPLATE_DIRS[0])+str('/invoice.html')
fr = open(fileread, "r").read()
fr = fr.replace('personal_details', personal_html)
result = StringIO.StringIO()
pdf = pisa.CreatePDF(
fr,result
)
filewrite = str(settings.TEMPLATE_DIRS[0]) + str('/invoice_write.html')
empty = ""
fw = open(filewrite, 'w')
fw.write(empty)
fw.write(fr)
fw.close()
PaymentPdf.objects.filter(invoicepdf = user_id).delete()
pdf_contents = render_to_pdf1('invoice_write.html',result)
file_to_be_saved = ContentFile(pdf_contents)
random_str = ''.join(random.sample((string.ascii_lowercase + string.ascii_uppercase + string.digits), 8))
resume_name = (str(user_id) + "_" + random_str + ".pdf").replace("@", '')
resume = PaymentPdf.objects.create(name = resume_name, invoicepdf_id = user_id, created_by = request.user)
resume.name.save(resume_name ,file_to_be_saved)
file_path = PaymentPdf.objects.get(invoicepdf = user_id).name
pdf_file = str(file_path).split("media")[1]
return HttpResponseRedirect('/site_media' + pdf_file)
def render_to_pdf1(template_src, context_dict):
template = get_template(template_src)
context = Context(context_dict)
html = template.render(context)
result = StringIO.StringIO()
pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("ISO-8859-1")), result)
return result.getvalue()
Anyone help me to display image in pdf?
解决方案
Refer Pisa (XHTML -> PDF) in Django will not display images in PDF, and add the lines
links = lambda uri, rel: os.path.join(settings.MEDIA_ROOT, uri.replace(settings.MEDIA_URL, ''))
pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("UTF-8")),dest=result, link_callback=links)
in render_to_pdf1 of views, and in html:
<img src="/site_media/img/logo.jpg"/ height="55" width="275">
It will work.
这篇关于django pdf与标志图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文