]> git.lyx.org Git - lyx.git/blob - lib/scripts/convert_pdf.py
Update Python scripts to Python 3+
[lyx.git] / lib / scripts / convert_pdf.py
1 # file convert_pdf.py
2 # This file is part of LyX, the document processor.
3 # Licence details can be found in the file COPYING.
4
5 # author Georg Baum
6 # Full author contact details are available in file CREDITS
7
8 # This script takes a PS or PDF file and creates a low resolution version.
9 # Example usage:
10 # convert_pdf.py big.pdf small.pdf ebook
11
12 # This script takes three arguments:
13 # INFILE:        the name of the .ps or .pdf file to be converted.
14 # OUTFILE:       the name of the .pdf file to be created.
15 # PDFSETTINGS:   any PDFSETTINGS supported by ghostscript:
16
17
18 import sys
19
20 from lyxpreview_tools import error, find_exe_or_terminate, run_command
21
22
23 def usage(prog_name):
24     return "Usage: %s <ps or pdf input file> <pdf output file> <screen|ebook|printer|prepress>" \
25         % prog_name
26
27
28 def main(argv):
29
30     if len(argv) == 4:
31         source = argv[1]
32         output = argv[2]
33         pdfsettings = argv[3]
34     else:
35         error(usage(argv[0]))
36
37     gs = find_exe_or_terminate(["gswin32c", "gswin64c", "gs"])
38     gs_call = '%s -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pdfwrite ' \
39               '-dCompatibilityLevel=1.4 -dPDFSETTINGS=/%s ' \
40               '-sOutputFile="%s" "%s"' % (gs, pdfsettings, output, source)
41
42     gs_status, gs_stdout = run_command(gs_call)
43     if gs_stdout:
44         sys.stdout.write(gs_stdout)
45     return gs_status
46
47
48 if __name__ == "__main__":
49     sys.exit(main(sys.argv))