Download a form with user input as pdf in template

I am building an invoice form in a template where a user input stuff and then he can download it. I have built the template with the interface for the user to input things as well as the template that renders the pdf format. The document can be downloaded and the format is fine.

The issue is that data does not get transferred from the invoice edit template to the pdf render template when the user clicks download. All the download pdfs are empty of any data, whether it is db data or input data.

I feel like I am over my head with this project, I cannot figure out what is causing the problem.

Here are attached my views:

  1. invoice assembly view:
def generate_pdf_assembly(request):
        Helper function to generate pdf in case of ajax request
    context = request.GET.copy()
    context['works'] = json.loads(context['works'])
    #context['amount_in_words'] = num2words.number_to_words(round(float(context.get('grand_total')), 2)) + ' only'
    challan_number        = context.get('challan_number')
    date                  = context.get('date')
    client_name           = context.get('client_name')
    client_address = context.get('client_address')
    total_weight          = context.get('total_weight')
    total_amount          = context.get('grand_total')

    # If user enters the same challan number then the previous record for that paricular challan number
    # is deleted and new record is overriden onto the old one
        report = Report.objects.create(
                    client_name = client_name,
                    client_address = client_address,
                    total_weight = total_weight, 

        challan = ChallanNumber.objects.first()
        challan.challan_number += 1
        report = Report.objects.create(
                    client_name = client_name, 
                    client_address = client_address,
                    total_weight = total_weight,

        challan = ChallanNumber.objects.first()
        challan.challan_number += 1

    # Sometime user might delete a row dynamically and hence an empty dict is passed to server
    # Hence we will check if amount is present in the dict else we delete that particular dic record
    for index, work in enumerate(context.get('works')):
        if work.get('amount'):
            quant = QuantityRate.objects.create(
            del context.get('works')[index]
    context['date'] = datetime.datetime.strptime(date, '%Y-%m-%d').strftime('%d-%m-%Y')
    context['dated'] = datetime.datetime.strptime(context.get('dated'), '%Y-%m-%d').strftime('%d-%m-%Y')
    request.session['context'] = context
    return redirect('get_pdf_assembly')

and here is the get pdf view:

def get_pdf_assembly(request):
    #context = request.GET.copy()
    #request.session['context'] = context
    pdf = render_to_pdf('pdf/invoice_generator_assembly.html', request.session['context'])
    if pdf:
        response = HttpResponse(pdf, content_type='application/pdf')
        filename = "Assembly_Invoice_{}.pdf".format(request.session.get('context').get('challan_number'))
        content = "inline; filename={}".format(filename)
        content = "attachment; filename={}".format(filename)
        response['Content-Disposition'] = content
        return response
    return HttpResponse("Not found")

I also have some javascript code inside of my invoice-assembly template that is supposed to get the data from the form and push it to the pdf

$("#download").on("click", function(){
        var id = $("#download").attr("data-id");
        var works = new Array();
        for(var i = 1; i <= parseInt(id); i++){
                client_name        : $("input[name=client-name" + i + "]").val(),
		date           : $("input[name=date" + i + "]").val(),
		client_address       : $("input[name=client-address" + i + "]").val(),
		Reference        : $("input[name=Reference" + i + "]").val(),
		weight1      : $("input[name=weight1" + i + "]").val(),
		bags1      : $("input[name=bags1" + i + "]").val(),
		rate1      : $("input[name=rate1" + i + "]").val(),
                amount1 : $("input[name=amount1" + i + "]").val(),
		totalHT : $("input[name=TotalHT" + i + "]").val(),
		TVA  : $("input[name=TVA" + i + "]").val(),
		TVA_amount : $("input[name=TVA-amount" + i + "]").val(),
		total_weight : $("input[name=total-weight" + i + "]").val(),


            url : "{% url 'generate_pdf_assembly' %}",
            type : "GET",
            data : {
                "client_name"         : $("input[name=client-name]").val(),
		" client_address "      : $("input[name=client-address]").val(),
		"Reference"            : $("input[name=Reference]").val(),
		" weight1 "             : $("input[name=weight1]").val(),
		"bqg1"                 : $("input[name=bag1]").val(),
		"rate1"                : $("input[name=rate1]").val(),
                "amount1"             : $("input[name=amount1]").val(),
		"totalHT"             : $("input[name=TotalHT]").val(),
		"TVA"                  : $("input[name=TVA]").val(),
		"TVA_amount"           : $("input[name=TVA-amount]").val(),
		"total_weight"         : $("input[name=total-weight]").val(),
                "challan_number"    : "{{ challan_number }}",
		"company" :           "{{ }}",
		"address" : "{{ a.address }}",
		"date" : $("input[name=date]").val(),
                "dated"             : $("input[name=dated]").val(),
            dataType : "json",
            success : function(data){
                window.location = "/get/pdf/assembly/";
                }, 3000);
            error: function(data){
                // var blob = new Blob([data]);
                // alert(blob);
                // var link=document.createElement('a');
                // link.href=window.URL.createObjectURL(blob);
                window.location = "/get/pdf/assembly/";
                }, 3000);

I am wondering if the method used is okay, or if there is anything that obviously off causing the pdfs to be empty. Any help would be greatly appreciated. Thanks!

I don’t have any specific help I can provide here, just the thought that if I were facing something like this, I’d either put a bunch of print (or logging) statements in there, or walk it through the debugger, to verify each step was doing what it was supposed to do, and that the data is what’s expected at each point.

There is one thing I notice that gets me wondering - at the end of generate_pdf_assembly, you’re returning a redirect.

The redirect is going to return a 301, which will cause the browser to issue a second get at the new url - but won’t be passing any data back in through the request. (It’s a new GET.)

If I were to try something, I’d try changing the return redirect(... to return get_pdf_assembly(request) assuming you want the same request data to be passed to that method.

Thank you for your responses, I appreciate it, I will give this a try!

Thank you very much for your reply