My goal is to create a pdf report using ReportLab where some of the text is dynamic based on a primary key.
I have a dropdown listing each primary key currently created in the program (#1 - 5 at present) and as more "shipments" are created, the primary key is added to the dropdown.
When I select the primary key and then click the "submit" button I would like for my pdf to generate where the dynamic text is related specifically to that pk.
Below is the view for the PDF I currently have with static text (pulled from a source on reportlab) where I would like values such as "JOHN DOE" and "Name goes here" to be replaced dynamically based on the pk selected.
views.py
def write_pdf_view(request):
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'inline; filename="mypdf.pdf"'
buffer = BytesIO()
p = canvas.Canvas(buffer)
# Start writing the PDF here
p.setLineWidth(.3)
p.setFont('Helvetica', 12)
p.drawString(30,750,'Name goes here')
p.drawString(30,735,'OF ACME INDUSTRIES')
p.drawString(500,750,"12/12/2010")
p.line(480,747,580,747)
p.drawString(275,725,'AMOUNT OWED:')
p.drawString(500,725,"$1,000.00")
p.line(378,723,580,723)
p.drawString(30,703,'RECEIVED BY:')
p.line(120,700,580,700)
p.drawString(120,703,"JOHN DOE")
# End writing
p.showPage()
p.save()
pdf = buffer.getvalue()
buffer.close()
response.write(pdf)
return response
Here is the dropdown being created
ViEWS.py
def reference_view(request):
query_results = Orders.objects.all()
reference_list = DropDownMenuReferences()
context = {
'query_results': query_results,
'reference_list': reference_list
}
return render(request, 'proforma_select.html', context)
Forms.py
class DropDownMenuReferences(forms.Form):
Reference_IDs = forms.ModelChoiceField(queryset=Orders.objects.values_list('reference', flat=True).distinct(),
empty_label=None)
TEMPLATE
{% extends 'base.html' %}
{% block body %}
<div class="container">
<br>
<form method=POST action="">
{{ reference_list }}
<button type="submit" class="btn btn-primary" name="button">Add Order</button>
</form>
</div>
{% endblock %}
I am presently completely unsure how to approach this problem. I have the dropdown and I understand how to create a pdf I just don't know where to tie them together.