Create Pdf with Latex in django

There is simple steps with help you make pdf using Latex and django

  • First create view
  • Create the file with help open() and write function
  • Write the data in file which you created
  • Make Latex file as your template
  • Now perform  the  rendering according your references which are stored in Latex template file
  • After this run call function with help you call the bash script with your latex file to pdf file is created.

//my_latex_template.tex

\documentclass{article}  
\begin{document}  
  {{ content }}
\end{document}

//Views.py

import os
from django.shortcuts import render, render_to_response, RequestContext, HttpResponseRedirect
from django.core.files import File
from django.template.loader import render_to_string
from subprocess import call
def home(request):
texfilename = "filepath"
texfilename_copy = "second_filepath"
f = open(texfilename ,'r+')
#cfile = file(texfilename, 'w+')
data = f.read()
f.close()
fw = open(texfilename_copy, 'w+')
fw.write(data)
fw.close()
texfile = os.open(texfilename_copy, os.O_RDWR)
os.write(texfile, render_to_string('filepath_second ', {'content':'hitesh softa'}))
os.close(texfile)
#os.remove(texfilename)
call(['sh','/home/sofat/shell.sh'])
return render(request, 'my_latex_tamplate.tex', context_instance =RequestContext(request))

Now make your Bash script file with help of editor and add this code in this script

cd [template file path]
pdflatex [filename]

rm -f *.log

rm -f *.aux
Advertisements

One Comment Add yours

  1. hero4indians says:

    Can you explain a little bit more what is happening in view.py.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s