47
47
TEX_TEMP = tempfile .gettempdir ()
48
48
TEX_EXT = '.tex'
49
49
50
- def render_to_latex (filepath , context_dict = None , filetype = 'pdf' , landscape = None ):
50
+ def render_to_latex (filepath , context_dict = None , filetype = 'pdf' ):
51
51
""" Render some tex source to latex. This will run the latex
52
52
interpreter and generate the necessary file type
53
53
(either pdf, tex, ps, dvi, or a log file) """
@@ -67,20 +67,12 @@ def render_to_latex(filepath, context_dict=None, filetype='pdf', landscape=None)
67
67
68
68
context ['MEDIA_ROOT' ] = settings .MEDIA_ROOT
69
69
context ['file_type' ] = filetype
70
-
71
70
72
71
rendered_source = t .render (context )
73
72
74
- # Autodetect landscape mode if 'landscape' is in the first 10 lines of output
75
- top_lines = rendered_source .split ('\n ' )[:10 ]
76
- if landscape is None :
77
- if 'landscape' in '\n ' .join (top_lines ):
78
- landscape = True
79
-
80
- return gen_latex (rendered_source , filetype , landscape )
81
-
73
+ return gen_latex (rendered_source , filetype )
82
74
83
- def gen_latex (texcode , type = 'pdf' , landscape = False , remove_files = False , stdout = PIPE , stderr = STDOUT ):
75
+ def gen_latex (texcode , type = 'pdf' , remove_files = False , stdout = PIPE , stderr = STDOUT ):
84
76
"""Generate the latex code.
85
77
86
78
:param texcode:
@@ -89,16 +81,12 @@ def gen_latex(texcode, type='pdf', landscape=False, remove_files=False, stdout=P
89
81
`unicode`
90
82
:param type:
91
83
The type of file to generate.
92
- Must be one of 'tex ', 'dvi', 'ps ', 'log', 'svg', or 'png'.
84
+ Must be one of 'pdf ', 'tex ', 'log', 'svg', or 'png'.
93
85
'tex' returns texcode itself, without processing.
94
86
'log' returns the log file from the execution of latex on texcode.
95
87
The others return the compilation of texcode into that format.
96
88
:type type:
97
- `str`, element of ('tex', 'dvi', 'ps', 'log', 'svg', 'png')
98
- :param landscape:
99
- True if the output should be in landscape format, else False.
100
- :type landscape:
101
- `bool`
89
+ `str`, element of ('pdf', 'tex', 'log', 'svg', 'png')
102
90
:param remove_files:
103
91
True if intermediate build files should be removed, else False.
104
92
:type remove_files:
@@ -132,67 +120,42 @@ def gen_latex(texcode, type='pdf', landscape=False, remove_files=False, stdout=P
132
120
texfile .close ()
133
121
134
122
135
- file_types = ['pdf' ,'dvi' , 'ps' , ' log' ,'tex' ,'svg' ,'png' ]
123
+ file_types = ['pdf' ,'log' ,'tex' ,'svg' ,'png' ]
136
124
137
125
# Get (sometimes-)necessary library files
138
126
from django .conf import settings
139
127
import shutil
140
128
141
129
# Set latex options
142
130
latex_options = ['-interaction' , 'nonstopmode' , '-halt-on-error' ]
143
- # Set dvips options
144
- dvips_options = ['-t' , 'letter' ]
145
- if landscape :
146
- dvips_options = ['-t' , 'letter,landscape' ]
147
131
148
132
# All command calls will use the same values for the cwd, stdout, and
149
133
# stderr arguments, so we define a partially-applied callable call()
150
134
# that makes it easier to call check_call() with these values.
151
135
call = partial (check_call , cwd = TEX_TEMP , stdout = stdout , stderr = stderr )
152
136
153
- if type == 'pdf' :
137
+ if type == 'pdf' :
154
138
mime = 'application/pdf'
155
- call (['latex' ] + latex_options + ['%s.tex' % file_base ])
156
- call (['dvips' ] + dvips_options + ['%s.dvi' % file_base ])
157
- call (['ps2pdf' , '%s.ps' % file_base ])
158
- if remove_files :
159
- os .remove ('%s.dvi' % file_base )
160
- os .remove ('%s.ps' % file_base )
161
-
162
- elif type == 'dvi' :
163
- mime = 'application/x-dvi'
164
- call (['latex' ] + latex_options + ['%s.tex' % file_base ])
165
-
166
- elif type == 'ps' :
167
- mime = 'application/postscript'
168
- call (['latex' ] + latex_options + ['%s.tex' % file_base ])
169
- call (['dvips' ] + dvips_options + [file_base , '-o' , '%s.ps' % file_base ])
170
- if remove_files :
171
- os .remove ('%s.dvi' % file_base )
172
-
173
- elif type == 'log' :
139
+ call (['pdflatex' ] + latex_options + ['%s.tex' % file_base ])
140
+
141
+ elif type == 'log' :
174
142
mime = 'text/plain'
175
143
call (['latex' ] + latex_options + ['%s.tex' % file_base ])
176
144
177
- elif type == 'svg' :
145
+ elif type == 'svg' :
178
146
mime = 'image/svg+xml'
179
- call (['latex' ] + latex_options + ['%s.tex' % file_base ])
180
- call (['dvips' ] + dvips_options + ['%s.dvi' % file_base ])
181
- call (['ps2pdf' , '%s.ps' % file_base ])
147
+ call (['pdflatex' ] + latex_options + ['%s.tex' % file_base ])
182
148
call (['inkscape' , '%s.pdf' % file_base , '-l' , '%s.svg' % file_base ])
183
149
if remove_files :
184
- os .remove ('%s.dvi' % file_base )
185
- os .remove ('%s.ps' % file_base )
186
150
os .remove ('%s.pdf' % file_base )
187
-
188
- elif type == 'png' :
151
+
152
+ elif type == 'png' :
189
153
mime = 'image/png'
190
- call (['latex ' ] + latex_options + ['%s.tex' % file_base ])
191
- call (['dvips' ] + dvips_options + [ '%s.dvi' % file_base ])
192
- call ([ 'convert' , '-density' , '192' , '%s.ps ' % file_base , '%s.png' % file_base ])
154
+ call (['pdflatex ' ] + latex_options + ['%s.tex' % file_base ])
155
+ call (['convert' , '-density' , '192' ,
156
+ '%s.pdf ' % file_base , '%s.png' % file_base ])
193
157
if remove_files :
194
- os .remove ('%s.dvi' % file_base )
195
- os .remove ('%s.ps' % file_base )
158
+ os .remove ('%s.pdf' % file_base )
196
159
197
160
else :
198
161
raise ESPError ('Invalid type received for latex generation: %s should be one of %s' % (type , file_types ))
0 commit comments