0

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.

GXM100
  • 417
  • 6
  • 20

1 Answers1

0

You can include the related primary key of the desired person in the form the form area of your program. When the user clicks Submit button, that primary key will included in your request object.

Then in your write_pdf_view function, you can pull the related person from the database by such as:

def write_pdf_view(request):

    if request.method == 'POST':
        primary_key = request.POST.get('primary_key')
        person = models.Person.objects.get(pk=primary_key)

        ...

        p.drawString(120, 703, person.full_name)

    ...

    return response

cagrias
  • 1,847
  • 3
  • 13
  • 24
  • I appreciate the help however I am a bit unclear on what you mean by 'include the related primary key of the desired person (order) in the form area of your program". I've edited the above to show the view, form, and html which I am using to create the dropdown containing the primary keys. Would you be able to help detail your answer using this? Thank you! – GXM100 May 29 '19 at 20:31
  • OK I got your problem. You want to get drop-down list value to be passed to your ddjango view. To do that, you can follow https://stackoverflow.com/a/11586830/4665915 – cagrias May 30 '19 at 05:53