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:
- 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
try:
report = Report.objects.create(
challan_number=challan_number,
date=date,
client_name = client_name,
client_address = client_address,
total_weight = total_weight,
total_amount=total_amount
)
report.save()
challan = ChallanNumber.objects.first()
challan.challan_number += 1
challan.save()
except:
QuantityRate.objects.filter(report__challan_number=challan_number).delete()
Report.objects.filter(challan_number=challan_number).delete()
report = Report.objects.create(
challan_number=challan_number,
client_name = client_name,
client_address = client_address,
date=date,
total_weight = total_weight,
total_amount=total_amount
)
report.save()
challan = ChallanNumber.objects.first()
challan.challan_number += 1
challan.save()
# 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(
quantity=work.get('quantity'),
rate=work.get('rate'),
amount=work.get('amount')
)
quant.save()
quant.report.add(report)
else:
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++){
works.push({
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(),
});
}
$.ajax({
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" : "{{ a.name }}",
"address" : "{{ a.address }}",
"date" : $("input[name=date]").val(),
"dated" : $("input[name=dated]").val(),
},
dataType : "json",
success : function(data){
window.location = "/get/pdf/assembly/";
setTimeout(function(){
window.location.replace("/invoice/assembly/");
}, 3000);
},
error: function(data){
// var blob = new Blob([data]);
// alert(blob);
// var link=document.createElement('a');
// link.href=window.URL.createObjectURL(blob);
// link.download="download.pdf";
// link.click();
window.location = "/get/pdf/assembly/";
setTimeout(function(){
window.location.replace("/invoice/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!