I am trying to stick a WeasyPrint html PDF into the Model. Using Django.
I can generate the PDF file fine.
pdf_file = HTML(string=html,base_url=base_url).write_pdf(stylesheets, [css_path],font_config=font_config)
Now what is best way to convert the pdf_file output to a format I can use for the database?
class Reports(models.Model):
pdf = models.FileField( upload_to='media/pdfs/', max_length=254, )
created = models.DateField(auto_now=False, auto_now_add=True)
If I do something like this it creates the file automatically before the Model.
fs = FileSystemStorage()
report = fs.save(f'pdfs/{uuid}.pdf', File(BytesIO(pdf_file)) )
And gives the table a 'pdf/...' in the database.
What is most efficent way to convert the Wesasy HTML into a format the Model can use and let the model save the file?
Thanks.
You can do it in two steps:
filefield is nullable, you can handle validation separately if needed).savemethod on thefilefield to save the pdfHere's how you can do it:
filenameis the file name you can generate however