forked from Arborator/arborator-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_svg.cgi
executable file
·174 lines (132 loc) · 4.95 KB
/
convert_svg.cgi
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
convert_svg.py 0.1
See <http://svgkit.sourceforge.net/> for documentation, downloads, license, etc.
(c) 2006-2007 Jason Gallicchio. All rights Reserved.
"""
# Outline of code:
# Read the SVG
# Read the type to convert to
# Read the options (width, height, dpi, quality) (TODO)
# Generate a hash for caching and check to see if we've already done this (TODO)
# Convert with scripts off
# Check the output file size.
# Send the result back
# needs: apt-get install librsvg2-bin !!!!!!!!!!!! new in 2014!!!
import cgi, cgitb, sys, os, urlparse, time, re, hashlib
from subprocess import *
from lib import config
cgitb.enable() # Show errors to browser.
form = cgi.FieldStorage()
time.sleep(0.1) # Throttle requests
project = form.getvalue("project",None).decode("utf-8")
if project != "quickie":
projectconfig=None
try:
projectconfig = config.configProject(project) # read in the settings of the current project
except:
pass
if not projectconfig:
print "Content-Type: text/html\n" # blank line: end of headers
print "something went seriously wrong: can't read the configuration of the project",project.encode("utf-8")
#print "Content-Type: text/html\n" # blank line: end of headers
#print '<script type="text/javascript">window.location.href=".";</script>'
sys.exit("something's wrong")
#java = '/opt/jdk1.7.0/bin/java'
java = "java"
#batik = '/var/www/cgi-bin/arborator/batik-1.7/batik-rasterizer.jar'
batik = 'export/batik-1.7/batik-rasterizer.jar'
#svg2office = '/var/www/cgi-bin/arborator/svg2office-1.2.2.jar'
svg2office = 'export/svg2office-1.2.2.jar'
#cache='/var/www/html/cache/'
cache= 'export/cache/'
#cacheShow='http://localhost/cache/'
#cacheShow= projectconfig["configuration"]["url"]+'export/cache/'
#url = os.environ["QUERY_STRING"]
#parsed = urlparse.urlparse(url)
cacheShow= 'export/cache/'
sys.stderr = sys.stdout
cgi.maxlen = 1024*1024
mediatypes={
'pdf':'application/pdf',
'ps':'application/pdf', # Gets converted after
'odg':'application/odg',
'jpg':'image/jpeg',
'png':'image/png',
'tif': 'image/tiff',
'svg':'image/svg+xml'
}
#debug = True
debug = False
redirect = True
#redirect = False
if debug: print 'Content-type: text/plain\n\n'
if debug:
print 'Debug mode of convert_svg.py\n'
print 'form.keys(): ' + form.keys().__str__()+'\n'
for key in form.keys():
print 'form['+key+'] = '+form[key].value+'\n'
source = form.getvalue('source',None)
if source == None: source = open("images/arborator.svg","r").read()
#source = open("arborator.svg","r").read()
type = form.getvalue('type',"pdf")
#type = form['type'].value
mime = mediatypes[type]
md5hex = hashlib.md5(source).hexdigest()
svgname = cache+md5hex+'.svg'
outname = cache+md5hex+'.'+type
showname = cacheShow+md5hex+'.'+type
def execute_cmd(cmd):
#(child_stdin, child_stdout, child_stderr) = os.popen3(cmd)
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
#(child_stdin,bufsize=bufsize,
#child_stdout,
#child_stderr) = (p.stdin, p.stdout, p.stderr)
#print 1/0
str_stdout = p.stdout.read() # Read until the process quits.
str_stderr = p.stderr.read() # Read until the process quits.
if debug:
print cmd+'\n'
print 'stdout:'+str_stdout+'\n'
print 'stderr:'+str_stderr+'\n'
#execute_cmd('chown jason users files/*.*') # Redhat disables chown
# If the result doesn't already exist in cached form, create it True or
if not os.path.isfile(outname) or source!=open(svgname, 'r' ).read():
# work around for the bug in rsvg-convert (failure to render non default fonts if they are not mentioned in "font-family")!
pattern=re.compile(r"font:([^;]* ([^;]*));",re.I+re.U)
source=pattern.sub(ur"font:\1; font-family:\2;",source.decode("utf-8")).encode("utf-8")
svgfile = open(svgname, 'w')
svgfile.write(source)
svgfile.close()
#if type == 'xml':
#PrettyPrint(xmlsqlite.tree2xml(1))
if type == 'svg':
outname = svgname
elif type == "odg":
cmd = java+' -jar ' + svg2office +" "+svgname #+" "+ outname
if debug: print cmd
execute_cmd(cmd)
#print "<br>",cmd
elif type in ["png","pdf","ps"]:
cmd = "rsvg-convert --background-color white -z 3 -f {type} -o {outname} {svgname}".format(type=type,svgname=svgname,outname=outname)
execute_cmd(cmd)
else:
cmd = java+' -jar ' + batik + ' -d '+cache+' -m '+mime+' '+svgname # -dpi <resolution> -q <quality>
if debug: print cmd
execute_cmd(cmd)
if type=='ps':
inname = cache+md5hex+'.pdf'
cmd = 'pdftops '+inname+' '+outname
execute_cmd(cmd)
#time.sleep(1)
if redirect:
#print 'Content-type: text/plain\n\n'
print 'Location: '+showname+'\r\n\r\n'
else:
outfile = open( outname, 'rb')
image = outfile.read()
if not debug:
sys.stdout.write('Content-type: '+mime+'\r\n\r\n')
sys.stdout.write(image)
outfile.close()