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

24 lines
778 B
Python
Raw Permalink Normal View History

import dramatiq
2025-03-03 14:54:04 +01:00
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()
2025-03-03 14:54:04 +01:00
def generate_pdf(invoice_id: int):
invoice = Invoice.objects.get(pk=invoice_id)
2025-03-03 21:52:20 +01:00
print_url = f'{settings.INTERNAL_API_URL}{reverse("invoices:print_invoice", kwargs=dict(invoice_id=invoice.id))}'
2025-03-03 14:54:04 +01:00
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()
2025-03-03 21:52:20 +01:00
invoice_file.file.save(
2025-03-03 21:56:04 +01:00
invoice_file.get_file_name('pdf'),
ContentFile(res.content),
2025-03-03 21:52:20 +01:00
)