File: ps2pdf.py

package info (click to toggle)
python-ghostscript 0.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 368 kB
  • sloc: python: 1,570; sh: 36; makefile: 11
file content (77 lines) | stat: -rw-r--r-- 2,329 bytes parent folder | download
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
#!/usr/bin/env python
"""
This is a rough Python implementation of the shellscript ps2pdfwr
which comes with Ghostscript.
"""
# -*- coding: utf-8 -*-
#
# This file is part of python-ghostscript.
# Copyright 2010-2023 by Hartmut Goebel <h.goebel@crazy-compilers.com>
#
# 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 3 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, see <https://www.gnu.org/licenses/>.
#

__author__ = "Hartmut Goebel <h.goebel@crazy-compilers.com>"
__copyright__ = "Copyright 2010-2023 by Hartmut Goebel <h.goebel@crazy-compilers.com>"
__licence__ = "GNU General Public License version 3 (GPL v3)"

import sys
import os

import ghostscript

options = ["-dSAFER"]
args = sys.argv[1:]
while args:
    arg = args[0]
    if arg.startswith('-') and len(arg) > 1:
        options.append(arg)
        arg = args.pop(0)
    else:
        break

def usage():
    print("Usage:", os.path.basename(sys.argv[0]),
          "[options...] (input.[e]ps|-) [output.pdf|-]",
          file=sys.stderr)
    sys.exit(1)
    
if len(args) not in (1,2):
    usage()

infile = args[0]
if len(args) == 1:
    if infile == '-':
        outfile = '-'
    else:
        base, ext = os.path.splitext(os.path.basename(infile))
        if ext in ('.ps', '.eps'):
            outfile = base + '.pdf'
        else:
            outfile = base + ext + '.pdf'
else:
    outfile = args[1]

# We have to include the options twice because -I only takes effect if
# it appears before other options.
args = []
args.extend(options)
args.extend(["-q", "-dNOPAUSE","-dBATCH", "-sDEVICE=pdfwrite",
             "-sstdout=%stderr",
             "-sOutputFile=" + outfile])
args.extend(options)
args.extend(["-f", infile])

# After all these preperations, calling Ghostscript is a piece of cake:
ghostscript.Ghostscript(os.path.basename(sys.argv[0]), *args)