Skip to content

Commit 3a04551

Browse files
authored
Merge pull request #3 from wanglongqi/pdf_crop
waiting for confirmation of the bug.
2 parents e29781d + fdccf03 commit 3a04551

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

writetex.py

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
An Latex equation editor for Inkscape.
66
77
:Author: WANG Longqi <[email protected]>
8-
:Date: 2017-03-27
9-
:Version: v1.5
8+
:Date: 2017-03-31
9+
:Version: v1.5.2
1010
1111
This file is a part of WriteTeX extension for Inkscape. For more information,
1212
please refer to http://wanglongqi.github.io/WriteTeX.
@@ -17,6 +17,7 @@
1717
import tempfile
1818
import sys
1919
import copy
20+
import subprocess
2021
WriteTexNS = u'http://wanglongqi.github.io/WriteTeX'
2122
# from textext
2223
SVG_NS = u"http://www.w3.org/2000/svg"
@@ -151,34 +152,50 @@ def effect(self):
151152
tex.close()
152153

153154
if self.options.latexcmd.lower() == "xelatex":
154-
os.popen('xelatex "-output-directory=%s" -interaction=nonstopmode -halt-on-error "%s" > "%s"'
155-
% (tmp_dir, tex_file, out_file))
155+
subprocess.call('xelatex "-output-directory=%s" -interaction=nonstopmode -halt-on-error "%s" > "%s"'
156+
% (tmp_dir, tex_file, out_file), shell=True)
156157
elif self.options.latexcmd.lower() == "pdflatex":
157-
os.popen('pdflatex "-output-directory=%s" -interaction=nonstopmode -halt-on-error "%s" > "%s"'
158-
% (tmp_dir, tex_file, out_file))
158+
subprocess.call('pdflatex "-output-directory=%s" -interaction=nonstopmode -halt-on-error "%s" > "%s"'
159+
% (tmp_dir, tex_file, out_file), shell=True)
159160
else:
160161
# Setting `latexcmd` to following string produces the same result as xelatex condition:
161162
# 'xelatex "-output-directory={tmp_dir}" -interaction=nonstopmode -halt-on-error "{tex_file}" > "{out_file}"'
162-
os.popen(self.options.latexcmd.format(
163-
tmp_dir=tmp_dir, tex_file=tex_file, out_file=out_file))
163+
subprocess.call(self.options.latexcmd.format(
164+
tmp_dir=tmp_dir, tex_file=tex_file, out_file=out_file), shell=True)
164165

165166
try:
166-
os.popen('pdfcrop %s' % pdf_file)
167-
os.remove(pdf_file)
168-
os.rename(crop_file, pdf_file)
167+
# Here is a bug in pdfcrop, no idea how to fix.
168+
crop_cmd = 'pdfcrop "%s"' % pdf_file
169+
crop = subprocess.Popen(crop_cmd,
170+
stdout=subprocess.PIPE,
171+
stderr=subprocess.PIPE,
172+
shell=True)
173+
out = crop.communicate()
174+
if len(out[1]) > 0:
175+
inkex.errormsg("Error in pdfcrop:\n")
176+
inkex.errormsg(" CMD executed: %s\n" % crop_cmd)
177+
for msg in out:
178+
inkex.errormsg(msg)
179+
inkex.errormsg("Process will continue without crop")
180+
181+
if os.path.exists(crop_file):
182+
os.remove(pdf_file)
183+
os.rename(crop_file, pdf_file)
169184
except:
170185
pass
171186

172187
if not os.path.exists(pdf_file):
173188
print >>sys.stderr, "Latex error: check your latex file and preamble."
174189
print >>sys.stderr, open(log_file).read()
190+
return
175191
else:
176192
if self.options.pdftosvg == '1':
177-
os.popen('pdf2svg %s %s' % (pdf_file, svg_file))
193+
subprocess.call('pdf2svg %s %s' %
194+
(pdf_file, svg_file), shell=True)
178195
self.merge_pdf2svg_svg(svg_file)
179196
else:
180-
os.popen('pstoedit -f plot-svg "%s" "%s" -dt -ssp -psarg -r9600x9600 > "%s" 2> "%s"'
181-
% (pdf_file, svg_file, out_file, err_file))
197+
subprocess.call('pstoedit -f plot-svg "%s" "%s" -dt -ssp -psarg -r9600x9600 > "%s" 2> "%s"'
198+
% (pdf_file, svg_file, out_file, err_file), shell=True)
182199
self.merge_pstoedit_svg(svg_file)
183200

184201
os.remove(tex_file)

0 commit comments

Comments
 (0)