This repository has been archived on 2025-03-03. You can view files and clone it, but cannot push or open issues or pull requests.
facturio/invoices/tasks.py
2025-03-03 21:26:27 +01:00

20 lines
731 B
Python

import dramatiq
from django.conf import settings
from django.core.files.base import ContentFile
from django.urls import reverse
from gotenberg_client import GotenbergClient
from invoices.models import Invoice
@dramatiq.actor()
def generate_pdf(invoice_id: int):
invoice = Invoice.objects.get(pk=invoice_id)
print_url = f"{settings.INTERNAL_API_URL}{reverse('invoices:print_invoice', kwargs=dict(invoice_id=invoice.id))}"
invoice_file = invoice.files.create()
with GotenbergClient(settings.GOTENBERG_API_URL) as client:
with client.chromium.url_to_pdf() as task:
res = task.url(print_url).run()
invoice_file.file.save(invoice_file.get_file_name('pdf'), ContentFile(res.content))