【问题标题】:how to Serve Pdf data from server to client. (Django)如何将 Pdf 数据从服务器提供给客户端。 (姜戈)
【发布时间】:2020-09-28 13:45:16
【问题描述】:

我正在开发一个电子书阅读器应用程序,人们可以在该应用程序中以更适合移动阅读的方式发送 pdf 和获取 pdf 内容。无论如何要在不干扰图像的情况下从服务器向客户端提供 pdf 数据?欲了解更多信息:我正在使用 djnago 休息框架。

【问题讨论】:

  • 通常它会像这样工作,您将字节转换为字符串或字节并将其发送到服务器,然后在服务器中将其保存为 pdf,如果客户端要求它,您将服务器上的 pdf 转换为字节并将其发送到用户 u 中的用户将其转换回 pdf

标签: django python-3.x python-2.7 pdf django-rest-framework


【解决方案1】:

您可能使用 DRF 与前端进行通信,但它妨碍了处理 PDF。您可以简单地使用 Django Documentation 中的示例来解释一切。 “Hello World”示例是:

import io
from django.http import FileResponse
from reportlab.pdfgen import canvas

def some_view(request):
    # Create a file-like buffer to receive PDF data.
    buffer = io.BytesIO()

    # Create the PDF object, using the buffer as its "file."
    p = canvas.Canvas(buffer)

    # Draw things on the PDF. Here's where the PDF generation happens.
    # See the ReportLab documentation for the full list of functionality.
    p.drawString(100, 100, "Hello world.")

    # Close the PDF object cleanly, and we're done.
    p.showPage()
    p.save()

    # FileResponse sets the Content-Disposition header so that browsers
    # present the option to save the file.
    buffer.seek(0)
    return FileResponse(buffer, as_attachment=True, filename='hello.pdf')

使用 DRF 视图接受和验证参数,重定向到为生成的响应提供服务的 Django 视图。

【讨论】:

  • 好吧,我想你误解了我,或者我无法表达我的问题......我的意思是当有人提交服务器返回文本时...... kindle 或任何其他阅读应用程序......整页以及根据屏幕大小排列的文字
  • 啊,所以你想阅读PDF并输出HTML?在这种情况下,启动here
猜你喜欢
  • 1970-01-01
  • 2015-07-25
  • 2011-05-29
  • 1970-01-01
  • 1970-01-01
  • 2021-02-15
  • 2019-05-25
  • 2019-09-22
  • 1970-01-01
相关资源
最近更新 更多