A simple way to generate PDF documents in Django apps.
Use cases:
- Generate pdf reports
- Generate PDF documents from transactions, such as invoices or other documents derived from a transaction.
To define the position within the document, it uses X and Y coordinates.
- Image of X and Y coordinate usage:
- Image of rectangles with X and Y coordinates:
The final goal is to offer a drag-and-drop interface for designing the document, enabling collaborative document creation. This project is still in its early stages of development and welcomes all contributors.
Currently, it has only been tested with Django 4.0 and higher. For older versions of Django, use with caution.
- If you would like to see a feature added, please request it!
- If you find a bug, please report it!
- Add to
INSTALLED_APPS
in yoursettings.py
file:
INSTALLED_APPS = [
...
'django_document_pdf',
...
]
- Include URLs in
urls.py
file:
from django.urls import path, include
urlpatterns = [
...
path('django_document_pdf/', include('django_document_pdf.urls')),
...
]
-
Create folders: Create fonts and images folder inside the MEDIA path.
-
Configure settings: Make sure to include
MEDIA_URL
andMEDIA_ROOT
settings in your project'ssettings.py
file:
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'
-
Manage Fonts and Styles
- Create and upload your fonts.
- Create your fonts styles.
-
Define Document Elements
Create a DocumentSpecs, fields, labels, rects, images, fonts
- Fields: Access values from model instances.
- Labels: Define static text elements.
- Rects: Create lines, rectangles with borders and/or fills.
- Images: Add images or logos, including transparency options for watermarks.
- Fonts: Specify fonts to prevent loading unused ones.
-
Accessing Data
With
django_document_pdf
, you can access not only the fields of the record but also related fields through foreign key relationships.
class Supplier(models.Model):
Code = models.CharField(max_length=20)
Name = models.CharField(max_length=100)
class PurchaseInvoice(models.Model):
Supplier = models.ForeignKey(Supplier, on_delete=models.CASCADE)
Total = models.DecimalField(max_digits=10, decimal_places=2)
...
class Item(models.Model):
Code = models.CharField(max_length=20, unique=True)
Description = models.CharField(max_length=50)
Price = models.DecimalField(max_digits=10, decimal_places=2)
class PurchaseInvoiceItems(models.Model):
PurchaseInvoice = models.ForeignKey(
PurchaseInvoice, on_delete=models.CASCADE)
Item = models.ForeignKey(Item, on_delete=models.CASCADE)
Price = models.DecimalField(max_digits=10, decimal_places=2)
...
-
Thats like
record.Supplier.Name
-
Thats like
record.purchaseinvoiceitems_set.all()[idx].Price
-
Related Detail Record Field: To get field with the foreing key relation of detail related record:
Thats like
record.purchaseinvoiceitems_set.all()[idx].Item.Description
Them
to make your document
from django_document_pdf.document_wrapper import DocumentPDF
from .models import PurchaseInvoice
record = PurchaseInvoice.objects.get(pk=1)
doc = DocumentPDF(document_spec_code='Invoice_Test',
filename='file.pdf',
record=record)
doc.generatePDF()