Skip to content

Commit 7e32bdd

Browse files
committed
Merge pull request #1509 from learning-unlimited/fix-letterhead
Fix letterhead
2 parents b073148 + 53d94c3 commit 7e32bdd

File tree

8 files changed

+47
-18522
lines changed

8 files changed

+47
-18522
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ esp/esp/local_settings.py
66
*#
77
esp/public/media/subsection_images/*
88
esp/public/media/latex/*
9+
esp/public/media/latex_media/letterhead.*
910
esp/public/media/email_images/*
1011
esp/public/media/uploaded/*
1112
esp/public/media/styles

esp/esp/survey/templatetags/survey.py

+17-8
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
from django.template import loader
3737
from esp.program.models.class_ import ClassSubject
3838

39+
import os
40+
import subprocess
41+
3942
try:
4043
import cPickle as pickle
4144
except ImportError:
@@ -109,8 +112,6 @@ def histogram(answer_list, format='html'):
109112
from django.conf import settings
110113
HISTOGRAM_PATH = 'images/histograms/'
111114
HISTOGRAM_DIR = settings.MEDIA_ROOT + HISTOGRAM_PATH
112-
from esp.web.util.latex import get_rand_file_base
113-
import os
114115
import tempfile
115116

116117
image_width = 2.75
@@ -158,7 +159,8 @@ def histogram(answer_list, format='html'):
158159
import hashlib
159160
file_base = hashlib.sha1(pickle.dumps(context)).hexdigest()
160161
file_name = os.path.join(tempfile.gettempdir(), file_base+'.eps')
161-
template_file = settings.TEMPLATE_DIRS[0] + '/survey/histogram_base.eps'
162+
template_file = os.path.join(settings.TEMPLATE_DIRS[0],
163+
'survey', 'histogram_base.eps')
162164

163165
context['file_name'] = file_name # This guy depends on the SHA-1
164166

@@ -170,13 +172,20 @@ def histogram(answer_list, format='html'):
170172

171173
# We have the necessary EPS file, now we do any necessary conversions and include
172174
# it into the output.
175+
png_filename = "%s.png" % file_base
173176
if format == 'tex':
174-
return '\includegraphics[width=%fin]{%s}' % (image_width, file_name)
177+
image_path = os.path.join(tempfile.gettempdir(), png_filename)
175178
elif format == 'html':
176-
image_path = '%s%s.png' % (HISTOGRAM_DIR, file_base)
177-
if not os.path.exists(image_path):
178-
os.system('gs -dBATCH -dNOPAUSE -dTextAlphaBits=4 -dDEVICEWIDTHPOINTS=216 -dDEVICEHEIGHTPOINTS=162 -sDEVICE=png16m -R96 -sOutputFile=%s %s' % (image_path, file_name))
179-
return '<img src="%s.png" />' % ('/media/' + HISTOGRAM_PATH + file_base)
179+
image_path = os.path.join(HISTOGRAM_DIR, png_filename)
180+
if not os.path.exists(image_path):
181+
subprocess.call(['gs', '-dBATCH', '-dNOPAUSE', '-dTextAlphaBits=4',
182+
'-dDEVICEWIDTHPOINTS=216', '-dDEVICEHEIGHTPOINTS=162',
183+
'-sDEVICE=png16m', '-R96',
184+
'-sOutputFile=' + image_path, file_name])
185+
if format == 'tex':
186+
return '\includegraphics[width=%fin]{%s}' % (image_width, image_path)
187+
if format == 'html':
188+
return '<img src="%s" />' % ('/media/' + HISTOGRAM_PATH + png_filename)
180189

181190
@register.filter
182191
def answer_to_list(ans):

esp/esp/web/util/latex.py

+18-55
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
TEX_TEMP = tempfile.gettempdir()
4848
TEX_EXT = '.tex'
4949

50-
def render_to_latex(filepath, context_dict=None, filetype='pdf', landscape=None):
50+
def render_to_latex(filepath, context_dict=None, filetype='pdf'):
5151
""" Render some tex source to latex. This will run the latex
5252
interpreter and generate the necessary file type
5353
(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)
6767

6868
context['MEDIA_ROOT'] = settings.MEDIA_ROOT
6969
context['file_type'] = filetype
70-
7170

7271
rendered_source = t.render(context)
7372

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)
8274

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):
8476
"""Generate the latex code.
8577
8678
:param texcode:
@@ -89,16 +81,12 @@ def gen_latex(texcode, type='pdf', landscape=False, remove_files=False, stdout=P
8981
`unicode`
9082
:param type:
9183
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'.
9385
'tex' returns texcode itself, without processing.
9486
'log' returns the log file from the execution of latex on texcode.
9587
The others return the compilation of texcode into that format.
9688
: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')
10290
:param remove_files:
10391
True if intermediate build files should be removed, else False.
10492
:type remove_files:
@@ -132,67 +120,42 @@ def gen_latex(texcode, type='pdf', landscape=False, remove_files=False, stdout=P
132120
texfile.close()
133121

134122

135-
file_types = ['pdf','dvi','ps','log','tex','svg','png']
123+
file_types = ['pdf','log','tex','svg','png']
136124

137125
# Get (sometimes-)necessary library files
138126
from django.conf import settings
139127
import shutil
140128

141129
# Set latex options
142130
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']
147131

148132
# All command calls will use the same values for the cwd, stdout, and
149133
# stderr arguments, so we define a partially-applied callable call()
150134
# that makes it easier to call check_call() with these values.
151135
call = partial(check_call, cwd=TEX_TEMP, stdout=stdout, stderr=stderr)
152136

153-
if type=='pdf':
137+
if type == 'pdf':
154138
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':
174142
mime = 'text/plain'
175143
call(['latex'] + latex_options + ['%s.tex' % file_base])
176144

177-
elif type=='svg':
145+
elif type == 'svg':
178146
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])
182148
call(['inkscape', '%s.pdf' % file_base, '-l', '%s.svg' % file_base])
183149
if remove_files:
184-
os.remove('%s.dvi' % file_base)
185-
os.remove('%s.ps' % file_base)
186150
os.remove('%s.pdf' % file_base)
187-
188-
elif type=='png':
151+
152+
elif type == 'png':
189153
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])
193157
if remove_files:
194-
os.remove('%s.dvi' % file_base)
195-
os.remove('%s.ps' % file_base)
158+
os.remove('%s.pdf' % file_base)
196159

197160
else:
198161
raise ESPError('Invalid type received for latex generation: %s should be one of %s' % (type, file_types))

0 commit comments

Comments
 (0)