Sitemap
Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Follow publication

Django PDF Actions: How to Export PDF from Django Admin

--

Are you tired of building custom PDF export solutions for your Django admin interface? Let me walk you through how I created Django PDF Actions, a powerful Django package that adds seamless PDF export capabilities to your admin interface. Whether you’re handling multilingual content, complex data tables, or need professional-looking reports, this actions app has got you covered.

django-pdf-actions

Checkout django-pdf-actions!

The Problem Space

Django’s admin interface is compelling out of the box, but when it comes to exporting data to PDF, developers often find themselves reinventing the wheel. Common challenges include:

  • Handling different page orientations
  • Supporting multiple languages (especially RTL scripts)
  • Creating professional-looking tables with proper styling
  • Managing pagination and layout consistently
  • Dealing with custom fonts and logos

Technical Implementation

Let’s dive into how to implement this in your Django project:

1. Initial Setup

First, install the package using pip:

pip install django-pdf-actions

Add it to your INSTALLED_APPS :

INSTALLED_APPS = [

'django_pdf_actions',
]

2. Font Configuration

The package uses RebortLabunder the hood, but we’ve simplified font management. Set up your fonts directory:

python manage.py setup_fonts

This command creates the necessary directory structure:

your_project/
├── static/
│ └── assets/
│ └── fonts/
│ └── DejaVuSans.ttf

3. Model Integration

Here’s how to add PDF export actions to your models:

from django.contrib import admin
from django_pdf_actions.actions import export_to_pdf_landscape, export_to_pdf_portrait

@admin.register(YourModel)
class YourModelAdmin(admin.ModelAdmin):
list_display = ('id', 'name', 'created_at')
actions = [export_to_pdf_landscape, export_to_pdf_portrait]

4. Customization Through Settings

The real power comes from the configuration options. Create a settings configuration through the admin interface:

from django_pdf_actions.models import ExportPDFSettings


# Example settings configuration
settings = ExportPDFSettings.objects.create(
items_per_page=15,
page_margin=20,
header_font_size=12,
body_font_size=10,
grid_line_color='#333333',
is_active=True
)

Export Example ( .pdf )

Press enter or click to view image in full size
.pdf (landscape) export example

Advanced Features

Multilingual Support

One of the standout features is the robust multilingual support:

# Arabic text example
class ArabicModelAdmin(admin.ModelAdmin):
list_display = ('arabic_name', 'english_name')
actions = [export_to_pdf_landscape, export_to_pdf_portrait]

def get_pdf_font(self):
return 'Cairo-Regular.ttf' # Arabic-compatible font

Custom Styling

The package allows deep customization of your PDF output:

settings.update(
header_background_color='#F8F9FA',
grid_line_width=0.5,
table_spacing=2.0,
max_chars_per_line=50
)

Performance Considerations

The package is optimized for performance:

1. Lazy Loading: Font files are loaded only when needed
2. Batch Processing: Efficient handling of large datasets
3. Memory Management: Smart pagination to handle large exports
4. Caching: Font metrics are cached for better performance

Future Improvements

The roadmap includes exciting features:

  • Additional page sizes formats beyond A4
  • Chart and graph (long-term)
  • Custom watermarks

The full source code is available on GitHub (https://github.com/ibrahimroshdy/django-pdf-actions), and we welcome contributions from the community!

--

--

Dev Genius
Dev Genius

Published in Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Ibrahim Roshdy
Ibrahim Roshdy

Written by Ibrahim Roshdy

Machine Learning Engineer @ WitnessAI — Data Analytics & Machine Learning | DevOps

No responses yet