如何提供从Python/Django中的第三方API获取的PDF?



我正在从第三方API获取PDF,并且我正在从API获取字符串形式的文件内容。我正在尝试在浏览器中提供它,但由于某种原因该文件是空白的。不应该。下面是我的代码:

# This returns the pdf bytes as string
document = get_document()
response = HttpResponse(document, content_type='application/pdf')
response['Content-Disposition'] = 'inline;filename=some_file.pdf'
return response

我做错了什么?

谢谢!

应将内容处置标头设置为附件。

响应标头应包含:

Content-Disposition: attachment; filename="some_file.pdf"

蟒蛇等效:

response['Content-Disposition'] = 'attachment; filename="some_file.pdf"'

参考:

  • https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition
  • https://docs.djangoproject.com/en/1.11/ref/request-response/#telling-the-browser-to-treat-the-response-as-a-file-attachment

最新更新