Git Product home page Git Product logo

django-persian-pdf's Introduction

Django Persian PDF

Django Persian PDF is a set of class based views allowing you to generate PDF reports using html or latex templates. Django Persian PDF under the hood uses two compiler to render template and build the actual pdf file:

  • Xelatex
  • Google Chrome

Using this approach avoids regular overhead for parsing html tags, improving overall response time and memory usage when generating large pdf files. Django Persian PDF follows django class based views and base on the compilers provide 4 generic view classes. The only difference is that these classes render their associated template and return a pdf file as response. Just like django views you can pass queryset or make context to be used in template. Here are the view classes available in django_persian_pdf:

  • HTMLToPDFTemplateView is an extension of django TemplateView using .html template to generate pdf file.
  • HTMLToPDFDetailView is an extension of django DetailView using .html template to generate pdf file.
  • LatexToPDFTemplateView is an extension of django TemplateView using .tex template to generate pdf file.
  • LatexToPDFDetailView is an extension of django DetailView using .tex template to generate pdf file.

Status

image

image

image

image

Dependencies

  • Django >= 3.2
  • Google Chrome Stable
  • xelatex

Install

pip install django-persian-pdf
  • Make sure you have installed either google-chrome-stable or texlive-xetex on your machine.
sudo apt install google-chrome-stable
sudo apt install texlive-xetex

Usage

  1. Edit settings.py and add django_persian_pdf to your INSTALLED_APPS.
  2. Inherit your views from appropriate Template or Detail view classes.

Example #1: Using HTML Template to generate a PDF response

from django_persian_pdf import views as pdf_views

class TemplatePrint(pdf_views.HTMLToPDFTemplateView):
    template_name = 'payment_reports.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data()
        context['payments'] = Payments.objects.all()
        return context

class DetailPrint(pdf_views.HTMLToPDFDetailView):
    template_name = 'payment_detail.html'
    queryset = Payments.objects.all()

Example #2: Using LaTeX Template to generate a PDF response

from django_persian_pdf import views as pdf_views

class TemplatePrint(pdf_views.LatexToPDFTemplateView):
    template_name = 'payment_reports.tex'

    def get_context_data(self, **kwargs):
        context = super().get_context_data()
        context['payments'] = Payments.objects.all()
        return context

class DetailPrint(pdf_views.LatexToPDFDetailView):
    template_name = 'payment_detail.tex'
    queryset = Payments.objects.all()

Notes on LaTeX

  1. Using latex template with persian fonts requires you to have installed your persian fonts in home directory.
mkdir ~/.fonts
cp /path_to_fonts/Vazirmatn.ttf ~/.fonts/
fc-cache -f -v
  1. In latex template make sure you have used xepersian package as last package.
  2. Define persian fonts in latex template.
  3. You can use django template tags in latex template.

Here is an example of latex template for a given view:

\documentclass[a4paper,9pt]{letter}
\usepackage[portrait,margin=0.1in]{geometry}
\usepackage{xepersian}

\settextfont{Vazirmatn}
\setlatintextfont{Vazirmatn}
\setdigitfont[Scale=1.1]{Vazirmatn}


\begin{document}

  {% for payment in payments %}
    {{ payment.trace_code }}
    \newline
    {{ payment.amount }}
    \newline
  {% endfor %}

\end{document}

django-persian-pdf's People

Contributors

bindruid avatar

Stargazers

Mohammad Khavari avatar Behrooz Ghorbani avatar Ysn E avatar AmirAli Irvany avatar amir avatar  avatar ALi.w avatar Yousof Shahrabi avatar Mohsen Davari avatar Hootan Hemmati avatar Erfan Arefmehr avatar Hamed Khosravi avatar Ali Poorbazargan avatar ali rahmani avatar Ali Rahmani avatar saeed avatar Sadegh Karimi avatar ali sharifi avatar Bobby Babazadeh avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.