Reputation: 6128
I have a script which let to generate a PDF file and then send it to another application (LogicalDOC) in order to save it.
I'm getting a problem with payload
line. The filename should be my filename variable but it doesn't take account.
If I write filename.pdf
, I find filename.pdf in LogicalDOC with the good content.
But filename have to change automatically for each new BirthCertificate.
How I can pass the path as filename ?
This is my script :
@login_required
def BirthCertificate_PDF(request, id) :
birthcertificate = get_object_or_404(BirthCertificate, pk=id)
data = {"birthcertificate" : birthcertificate}
template = get_template('BC_raw.html')
html = template.render(Context(data))
filename_directory = str(BirthCertificate.objects.get(pk=id).lastname.encode('utf-8')) + "_" + str(BirthCertificate.objects.get(pk=id).firstname.encode('utf-8')) + "_" + str(BirthCertificate.objects.get(pk=id).birthday)
filename = 'Acte_Naissance_' + filename_directory + '.pdf'
path = '/Users/valentinjungbluth/Desktop/Django/Individus/' + filename
file = open(path, "w+b")
pisaStatus = pisa.CreatePDF(html.encode('utf-8'), dest=file, encoding='utf-8')
file.seek(0)
pdf = file.read()
if pdf :
payload = '{ "language":"fr","fileName":filename,"folderId":3309569 }'
upfile = path
files = {
'document': (None, payload, 'application/json'),
'content': (os.path.basename(upfile), open(upfile, 'rb'), 'application/octet-stream')
}
url = 'http://localhost:8080/services/rest/document/create'
headers = {'Content-Type': 'multipart/form-data'}
r = requests.post(url, files=files, headers=headers, auth=('admin', 'admin'))
context = {"birthcertificate":birthcertificate,
"path":path}
return render(request, 'BC_PDF.html', context)
file.close()
return HttpResponse(pdf, 'application/pdf')
If I wrote :
"FileName":"test.pdf"
I obtain the test.pdf file in logicalDoc (see picture)"FileName":"filename"
I obtain the filename file with unknown format in logicalDoc (see picture)I would like to get my filename variable
as document name
Upvotes: 0
Views: 156
Reputation: 47354
You can use string formatting for this. Just do follow:
payload = '{{ "language":"fr","fileName":"{0}","folderId":3309569 }}'.format(filename)
Upvotes: 1