Weasyprint (Django) save to file or model and not use

60 views Asked by At

is it possible to save the pdf file directly to model without rendering? I just want to database the PDF.

I am using Weasyprint although i imagine it would be same for other services like DocRaptor.

first just create the pdf. simple.

context = {}
template = get_template(template_path)
html = template.render(context)

# Generate the PDF
pdf_file = HTML(string=html).write_pdf('test.pdf)

then this model.

class Reports(models.Model):

    name = models.CharField()
    report = models.FileField(upload_to='reports/', null=True, blank=True)

and then to save.

_pdf = Reports.objects.create(

    name = 'test',
    report = pdf_file,

)

the name is added put nothing for the pdf.

I have a feeling their is some sort of File(BytesIO(pdf_file))) required???

I was able to add the pdf by using this after the save:

_pdf.report.save(f'{uuid}.pdf', File(BytesIO(pdf_file)))

Why cant I include in the Reports.objects.create?

Also when i use this approach the media/reports/pdf will not get deleted when the record gets deleted? is that normal?

Thank you

0

There are 0 answers