问题是我需要使用pdf / X-1a格式转换此pdf.我不知道是否可以改变xhtml2pdf中的格式,有人知道另一种选择吗?
视图:
def book(request): # Prepare context data = {} data['today'] = datetime.date.today() data['farmer'] = 'old MacDonald' data['animals'] = [('Cow','Moo'),('Goat','Baa'),('Pig','Oink')] # Render HTML content through HTML template with context template = get_template('book_number_one.HTML') HTML = template.render(Context(data)) # Write pdf to file file = open('test.pdf',"w+b") pisaStatus = pisa.Createpdf(HTML.encode('utf-8'),dest=file,enCoding='utf-8') # Return pdf document through a Django http response file.seek(0) pdf = file.read() file.close() # Don't forget to close the file handle return httpResponse(pdf,mimetype='application/pdf')
模板:
<HTML> <head> <Meta http-equiv="Content-Type" content="text/HTML; charset=utf-8"> <Title>Background</Title> <style> @page p1 { background-image: url("nu/COVER-FRONT-ES.png"); size: 210mm 210mm; @frame text { top: 6cm; left: 4cm; right: 4cm; bottom: 4cm; } } @page p2 { background-image: url("nu/001.png"); size: 210mm 210mm; @frame text { top: 6cm; left: 4cm; right: 4cm; bottom: 4cm; border:0; } } @Font-face { Font-family: 'Gilles'; src: url(/gilles/gilles.ttf); Font-weight: normal; Font-style: normal; } </style> </head> <body> <pdf:nextpage name="p1" /> <pdf:nextpage name="p2" /> <p > Lorem ipsum dolor sit amet,consectetur adipisicing elit. <br /> Vitae,natus,possimus,placeat consequuntur iste at sAPIente perferendis <br /> delectus suscipit dolorem dignissimos quaerat quia ex fuga officia dolore <br /> asperiores sint mollitia.<br /> <br /> </p> </body></HTML>解决方法 我建议一个名为’django-easy-pdf’的库
https://pypi.python.org/pypi/django-easy-pdf
演示:here
{% extends "easy_pdf/base.HTML" %}{% block content %} <div ID="content"> <h1>Hi there!</h1> </div>{% endblock %}from easy_pdf.vIEws import pdfTemplateVIEwclass HellopdfVIEw(pdfTemplateVIEw): template_name = "hello.HTML"总结
以上是内存溢出为你收集整理的使用Python格式PDF / X-1a将HTML转换为PDF全部内容,希望文章能够帮你解决使用Python格式PDF / X-1a将HTML转换为PDF所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)