我需要由weasyprint產生PDF文件是一些指定的文件夾,例如文件夾「MY_PROJECT/pdf_files/」如何將由weasyprint生成的PDF文件保存(存儲)到指定的目錄?
注意的:我使用Django框架爲我的項目。這裏是項目的結構。
my_project/
----pdf_generator_app/
--------admin.py
--------models.py
--------views.py
pdf_files/
目前我有這在我的views.py
def generate(student_id):
student = get_object_or_404(Student, application_id=student_id)
html = render_to_string('contract.html', {'student': student})
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'filename="some_file.pdf"'
weasyprint.HTML(string=html).write_pdf('myfile.pdf',
presentational_hints=True,
stylesheets=[weasyprint.CSS(settings.STATIC_ROOT + '/css/styles.css')])
return response
但這種觀點並不PDF文件保存到一個目錄。它只是在瀏覽器上顯示文件。我需要的文件存儲在目錄my_project/pdf_files/
如果我用**'response ['Content-Disposition']替換**'response ['Content-Disposition'] ='filename =「some_file.pdf」**',它將被直接下載。 ='附件;文件名= 「some_file.pdf」''** – UMAR