Reputation: 23
I am working on a ebook reader app in which people can send pdf and get pdf content on more mobile read-friendly way. anyway to serve pdf data from server to client without disturbing images? for more info: i am using djnago rest framework.
Upvotes: 0
Views: 1346
Reputation:
You probably use DRF to communicate with your frontend, but it gets in the way of dealing with PDFs. You can simply use the examples in the Django Documentation that explains everything. The "Hello World" example being:
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')
Use a DRF view to accept and verify the parameters, the redirect to the Django view that serves the generated response.
Upvotes: 1