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))