Debian Codegraph @

scripts/svg2pdf

raw symbols SCIP
1 #!/usr/bin/env python
2 """A simple converter from SVG to PDF.
3
4 For further information please check the file README.txt!
5 """
6
7 import sys
8 import argparse
9 import textwrap
10 from datetime import datetime
11 from os.path import dirname, basename, splitext, exists
12
13 from reportlab.graphics import renderPDF
14
15 from svglib import svglib
16
17
18 def svg2pdf(path, outputPat=None):
19 "Convert an SVG file to a PDF one."
20
21 # derive output filename from output pattern
22 file_info = {
23 'dirname': dirname(path) or '.',
24 'basename': basename(path),
25 'base': basename(splitext(path)[0]),
26 'ext': splitext(path)[1],
27 'now': datetime.now(),
28 'format': 'pdf'
29 }
30 out_pattern = outputPat or '%(dirname)s/%(base)s.%(format)s'
31 # allow classic %%(name)s notation
32 out_path = out_pattern % file_info
33 # allow also newer {name} notation
34 out_path = out_path.format(**file_info)
35
36 # generate a drawing from the SVG file
37 try:
38 drawing = svglib.svg2rlg(path)
39 except:
40 print('Rendering failed.')
41 raise
42
43 # save converted file
44 if drawing:
45 renderPDF.drawToFile(drawing, out_path, showBoundary=0)
46
47
48 # command-line usage stuff
49
50 def _main():
51 ext = 'pdf'
52 ext_caps = ext.upper()
53 args = dict(
54 prog=basename(sys.argv[0]),
55 version=svglib.__version__,
56 author=svglib.__author__,
57 license=svglib.__license__,
58 copyleft_year=svglib.__date__[:svglib.__date__.find('-')],
59 ts_pattern="{{dirname}}/out-"\
60 "{{now.hour}}-{{now.minute}}-{{now.second}}-"\
61 "%(base)s",
62 ext=ext,
63 ext_caps=ext_caps
64 )
65 args['ts_pattern'] += ('.%s' % args['ext'])
66 desc = '{prog} v. {version}\n'.format(**args)
67 desc += 'A converter from SVG to {} (via ReportLab Graphics)\n'.format(ext_caps)
68 epilog = textwrap.dedent('''\
69 examples:
70 # convert path/file.svg to path/file.{ext}
71 {prog} path/file.svg
72
73 # convert file1.svg to file1.{ext} and file2.svgz to file2.{ext}
74 {prog} file1.svg file2.svgz
75
76 # convert file.svg to out.{ext}
77 {prog} -o out.{ext} file.svg
78
79 # convert all SVG files in path/ to PDF files with names like:
80 # path/file1.svg -> file1.{ext}
81 {prog} -o "%(base)s.{ext}" path/file*.svg
82
83 # like before but with timestamp in the PDF files:
84 # path/file1.svg -> path/out-12-58-36-file1.{ext}
85 {prog} -o {ts_pattern} path/file*.svg
86
87 issues/pull requests:
88 https://github.com/deeplook/svglib
89
90 Copyleft by {author}, 2008-{copyleft_year} ({license}):
91 https://www.gnu.org/licenses/lgpl-3.0.html'''.format(**args))
92 p = argparse.ArgumentParser(
93 description=desc,
94 epilog=epilog,
95 formatter_class=argparse.RawDescriptionHelpFormatter
96 )
97
98 p.add_argument('-v', '--version',
99 help='Print version number and exit.',
100 action='store_true')
101
102 p.add_argument('-o', '--output',
103 metavar='PATH_PAT',
104 help='Set output path (incl. the placeholders: dirname, basename,'
105 'base, ext, now) in both, %%(name)s and {name} notations.'
106 )
107
108 p.add_argument('input',
109 metavar='PATH',
110 nargs='*',
111 help='Input SVG file path with extension .svg or .svgz.')
112
113 args = p.parse_args()
114
115 if args.version:
116 print(svglib.__version__)
117 sys.exit()
118
119 if not args.input:
120 p.print_usage()
121 sys.exit()
122
123 paths = [a for a in args.input if exists(a)]
124 for path in paths:
125 svg2pdf(path, outputPat=args.output)
126
127
128 if __name__ == '__main__':
129 _main()