我需要向浏览器提供PDF,并以二进制方式从API返回。
我使用的是python 2.7,Django 1.5和requests
我按照Django docs中的建议操作并安装了ReportLab。我也得到了以下示例:
response = HttpResponse(content_type="application/pdf")
response["Content-Disposition"] = "inline; filename=a_test_document.pdf"
p = canvas.Canvas(response)
p.drawString(100, 500, "Hello world")
p.showPage()
p.save()
return response
但是,这只允许我使用自己的PDF。有什么方法可以将二进制转换为PDF吗?我查看了reportlab文档以及其他一些解决方案,但没有看到任何确定的内容。
答案 0 :(得分:0)
要生成PDF,您可以使用xhtml2pdf库。
该函数返回响应对象,您只需传递模板名称,上下文数据和pdfname。
def fetch_resources(uri, rel):
"""
Callback to allow xhtml2pdf/reportlab to retrieve Images,Stylesheets, etc.
`uri` is the href attribute from the html link element.
`rel` gives a relative path, but it's not used here.
"""
if uri.startswith(settings.MEDIA_URL):
path = os.path.join(settings.MEDIA_ROOT,
uri.replace(settings.MEDIA_URL, ""))
elif uri.startswith(settings.STATIC_URL):
path = os.path.join(settings.STATIC_ROOT,
uri.replace(settings.STATIC_URL, ""))
else:
path = os.path.join(settings.STATIC_ROOT,
uri.replace(settings.STATIC_URL, ""))
if not os.path.isfile(path):
path = os.path.join(settings.MEDIA_ROOT,
uri.replace(settings.MEDIA_URL, ""))
if not os.path.isfile(path):
raise UnsupportedMediaPathException(
'media urls must start with %s or %s' % (
settings.MEDIA_ROOT, settings.STATIC_ROOT))
return path
def render_to_pdf_response(template_name, context=None, pdfname='test.pdf'):
file_object = HttpResponse(mimetype='application/pdf')
file_object['Content-Disposition'] = 'attachment; filename=%s' % pdfname
template = get_template(template_name)
html = template.render(Context(context))
pisa.CreatePDF(html.encode("UTF-8"), file_object , encoding='UTF-8',
link_callback=fetch_resources)
return file_object
答案 1 :(得分:0)
您似乎正在尝试更新现有PDF而不是简单地创建新PDF。在这种情况下,this answer可能就是您正在寻找的内容。总结他的解决方案:
- 使用PdfFileReader()读取您的PDF,我们将调用此输入
- 使用ReportLab创建一个包含要添加的文本的新pdf,将其另存为字符串对象
- 使用PdfFileReader()读取字符串对象,我们将调用此文本
- 使用PdfFileWriter()创建一个新的PDF对象,我们将调用此输出
- 遍历输入并为要添加文本的每个页面应用.mergePage( text .getPage(0)),然后使用 output .addPage()将修改后的页面添加到新文档
另一方面,如果您不确定收到的二进制文件的文件类型(不太可能是您的示例,但值得一提),您可以使用名为python-magic
的内容。这是一个未经测试的潜在例子:
In [2]: import magic
In [3]: m = magic.Magic(mime=True)
In [4]: m.from_file('/home/culebron/Documents/chapter2.pdf')
Out[4]: 'pdf'
根据最终输出,您可以确定: