1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
#!/usr/bin/env python
# coding=utf-8
#
# Copyright (C) 2008 Stephen Silver
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#
"""
Simple wrapper around ps2pdf
"""
import sys
import os
import inkex
from inkex.command import ProgramRunError, call, which
from inkex.localization import inkex_gettext as _
class PostscriptInput(inkex.CallExtension):
"""Load Postscript/EPS Files by calling ps2pdf program"""
input_ext = "ps"
output_ext = "pdf"
multi_inx = True
def add_arguments(self, pars):
pars.add_argument("--crop", type=inkex.Boolean, default=False)
pars.add_argument(
"--autorotate", choices=["None", "PageByPage", "All"], default="None"
)
def call(self, input_file, output_file):
crop = "-dEPSCrop" if self.options.crop else None
if sys.platform == "win32":
params = [
"-q",
"-P-",
"-dSAFER",
"-dNOPAUSE",
"-dBATCH",
"-sDEVICE#pdfwrite",
"-dCompatibilityLevel#1.4",
crop,
"-sOutputFile#" + output_file,
"-dAutoRotatePages#/" + self.options.autorotate,
input_file,
]
gs_execs = ["gswin64c", "gswin32c"]
gs_exec = None
for executable in gs_execs:
try:
which(executable)
gs_exec = executable
except:
pass
if gs_exec is None:
if "PYTEST_CURRENT_TEST" in os.environ:
gs_exec = "gswin64c" # In CI, we have neither available,
# but there are mock files for the 64 bit version
else:
raise inkex.AbortExtension(_("No GhostScript executable was found"))
try:
call(gs_exec, *params)
except ProgramRunError as err:
self.handle_gs_error(err)
else:
try:
call(
"ps2pdf",
crop,
"-dAutoRotatePages=/" + self.options.autorotate,
input_file,
output_file,
)
except ProgramRunError as err:
self.handle_gs_error(err)
def handle_gs_error(self, err: ProgramRunError):
inkex.errormsg(
_(
"Ghostscript was unable to read the file. \nThe following error message was returned:"
)
+ "\n"
)
inkex.errormsg(err.stderr.decode("utf8"))
inkex.errormsg(err.stdout.decode("utf8"))
raise inkex.AbortExtension()
if __name__ == "__main__":
PostscriptInput().run()
|