Django documentation has the following example for ReportLab.
from reportlab.pdfgen import canvas
from django.http import HttpResponse
def some_view(request):
# Create the HttpResponse object with the appropriate PDF headers.
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"'
# Create the PDF object, using the response object as its "file."
p = canvas.Canvas(response)
# 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()
return response
However. I want to use RML to generate my PDF. ReportLab Plus offers an rml2pdf function that can turn an RML document into a PDF using a similar markup to Django's templating. How can I provide Django the RML template and have Django return the PDF in the API response, similar to the example in the documentation?
Figured it out.
RML Template
Put this in your Django templates folder so Django can find it.
Call it
templates/rml_template.rmlDjango View
This view loads the template as a string and rml2pdf uses the response object as its "file" to write to, similar to the Django documentation example.