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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
|
#!/usr/bin/env ruby
# program : pstopdf
# copyright : PRAGMA Advanced Document Engineering
# version : 2002-2005
# author : Hans Hagen
#
# project : ConTeXt / eXaMpLe
# concept : Hans Hagen
# info : j.hagen@xs4all.nl
# www : www.pragma-ade.com
banner = ['PsToPdf', 'version 2.0.1', '2002-2006', 'PRAGMA ADE/POD']
$: << File.expand_path(File.dirname($0)) ; $: << File.join($:.last,'lib') ; $:.uniq!
# todo: paden/prefix in magick and inkscape
# todo: clean up method handling (pass strings, no numbers)
# --method=crop|bounded|raw|...
# --resolution=low|normal|medium|high|printer|print|screen|ebook|default
# + downward compatible flag handling
require 'base/switch'
require 'base/tool'
require 'base/logger'
require 'graphics/gs'
require 'graphics/magick'
require 'graphics/inkscape'
require 'rexml/document'
exit if defined?(REQUIRE2LIB)
class Commands
include CommandBase
# nowadays we would force a directive, but
# for old times sake we handle default usage
def main
filename = @commandline.argument('first')
pattern = @commandline.option('pattern')
if filename.empty? && ! pattern.empty? then
pattern = "**/#{pattern}" if @commandline.option('recurse')
globfiles(pattern)
end
filename = @commandline.argument('first')
if filename.empty? then
help
elsif filename =~ /\.exa$/ then
request
else
convert
end
end
# actions
def convert
ghostscript = GhostScript.new(logger)
magick = ImageMagick.new(logger)
inkscape = InkScape.new(logger)
outpath = @commandline.option('outputpath')
unless outpath.empty? then
begin
File.expand_path(outpath)
outpath = File.makedirs(outpath) unless FileTest.directory?(outpath)
rescue
# sorry
end
end
@commandline.arguments.each do |filename|
filename = Tool.cleanfilename(filename,@commandline)
inppath = @commandline.option('inputpath')
if inppath.empty? then
inppath = '.'
fullname = filename # avoid duplicate './'
else
fullname = File.join(inppath,filename)
end
if FileTest.file?(fullname) then
handle_whatever(ghostscript,inkscape,magick,filename)
else
report("file #{fullname} does not exist")
end
end
end
def request
# <exa:request>
# <exa:application>
# <exa:command>pstopdf</exa:command>
# <exa:filename>E:/tmp/demo.ps</exa:filename>
# </exa:application>
# <exa:data>
# <exa:variable label='gs:DoThumbnails'>false</exa:variable>
# <exa:variable label='gs:ColorImageDepth'>-1</exa:variable>
# </exa:data>
# </exa:request>
ghostscript = GhostScript.new(logger)
magick = ImageMagick.new(logger)
inkscape = InkScape.new(logger)
dataname = @commandline.argument('first') || ''
filename = @commandline.argument('second') || ''
if dataname.empty? || ! FileTest.file?(dataname) then
report('provide valid exa file')
return
else
begin
request = REXML::Document.new(File.new(dataname))
rescue
report('provide valid exa file (xml error)')
return
end
end
if filename.empty? then
begin
if filename = REXML::XPath.first(request.root,"exa:request/exa:application/exa:filename/text()") then
filename = filename.to_s
else
report('no filename found in exa file')
return
end
rescue
filename = ''
end
end
if filename.empty? then
report('provide valid filename')
return
elsif ! FileTest.file?(filename) then
report("invalid filename #{filename}")
return
end
[ghostscript,inkscape,magick].each do |i|
i.setvariable('inputfile',filename)
end
# set ghostscript variables
REXML::XPath.each(request.root,"/exa:request/exa:data/exa:variable") do |v|
begin
if (key = v.attributes['label']) and (value = v.text.to_s) then
case key
when /gs[\:\.](var[\:\.])*(offset)/io then ghostscript.setoffset(value)
when /gs[\:\.](var[\:\.])*(method)/io then ghostscript.setvariable('method',value)
when /gs[\:\.](var[\:\.])*(.*)/io then ghostscript.setpsoption($2,value)
end
end
rescue
end
end
# no inkscape and magick variables (yet)
handle_whatever(ghostscript,inkscape,magick,filename)
end
def watch
ghostscript = GhostScript.new(logger)
magick = ImageMagick.new(logger)
inkscape = InkScape.new(logger)
pathname = commandline.option('watch')
unless pathname and not pathname.empty? then
report('empty watchpath is not supported')
exit
end
if pathname == '.' then
report("watchpath #{pathname} is not supported")
exit
end
if FileTest.directory?(pathname) then
if Dir.chdir(pathname) then
report("watching path #{pathname}")
else
report("unable to change to path #{pathname}")
exit
end
else
report("invalid path #{pathname}")
exit
end
waiting = false
loop do
if waiting then
report("waiting #{getvariable('delay')}")
waiting = false
sleep(getvariable('delay').to_i)
end
files = Dir.glob("**/*.*")
if files and files.length > 0 then
files.each do |fullname|
next unless fullname
if FileTest.directory?(fullname) then
debug('skipping path', fullname)
next
end
unless magick.supported(fullname) then
debug('not supported', fullname)
next
end
if (! FileTest.file?(fullname)) || (FileTest.size(fullname) < 100) then
debug("skipping small crap file #{fullname}")
next
end
debug("handling file #{fullname}")
begin
next unless File.rename(fullname,fullname) # access trick
rescue
next # being written
end
fullname = Tool.cleanfilename(fullname,@commandline)
fullname.gsub!(/\\/io, '/')
filename = File.basename(fullname)
filepath = File.dirname(fullname)
next if filename =~ /gstemp.*/io
if filepath !~ /(result|done|raw|crop|bound|bitmap)/io then
begin
File.makedirs(filepath+'/raw')
File.makedirs(filepath+'/bound')
File.makedirs(filepath+'/crop')
File.makedirs(filepath+'/bitmap')
debug("creating prefered input paths on #{filepath}")
rescue
debug("creating input paths on #{filepath} failed")
end
end
if filepath =~ /^(.*\/|)(done|result)$/io then
debug("skipping file #{fullname}")
else
report("start processing file #{fullname}")
if filepath =~ /^(.*\/*)(raw|crop|bound)$/io then
donepath = $1 + 'done'
resultpath = $1 + 'result'
case $2
when 'raw' then method = 1
when 'bound' then method = 2
when 'crop' then method = 3
else method = 2
end
report("forcing method #{method}")
else
method = 2
donepath = filepath + '/done'
resultpath = filepath + '/result'
report("default method #{method}")
end
begin
File.makedirs(donepath)
File.makedirs(resultpath)
rescue
report('result path creation fails')
end
if FileTest.directory?(donepath) && FileTest.directory?(resultpath) then
resultname = resultpath + '/' + filename.sub(/\.[^\.]*$/,'') + '.pdf'
@commandline.setoption('inputpath', filepath)
@commandline.setoption('outputpath', resultpath)
@commandline.setoption('method', method)
if ghostscript.psfile?(fullname) then
handle_ghostscript(ghostscript,filename)
else
handle_magick(magick,filename)
end
sleep(1) # calm down
if FileTest.file?(fullname) then
begin
File.copy(fullname,donepath + '/' + filename)
File.delete(fullname)
rescue
report('cleanup fails')
end
end
end
end
end
end
waiting = true
end
end
private
def handle_whatever(ghostscript,inkscape,magick,filename)
if ghostscript.psfile?(filename) then
# report("processing ps file #{filename}")
ghostscript.setvariable('pipe',false) if @commandline.option('nopipe')
# ghostscript.setvariable('pipe',not @commandline.option('nopipe'))
ghostscript.setvariable('colormodel',@commandline.option('colormodel'))
ghostscript.setvariable('offset',@commandline.option('offset'))
handle_ghostscript(ghostscript,filename)
elsif ghostscript.pdffile?(filename) && ghostscript.pdfmethod?(@commandline.option('method')) then
# report("processing pdf file #{filename}")
handle_ghostscript(ghostscript,filename)
elsif inkscape.supported?(filename) then
# report("processing non ps/pdf file #{filename}")
handle_inkscape(inkscape,filename)
elsif magick.supported?(filename) then
# report("processing non ps/pdf file #{filename}")
handle_magick(magick,filename)
else
report("option not supported for #{filename}")
end
end
def handle_magick(magick,filename)
report("converting non-ps file #{filename} into pdf")
inppath = @commandline.option('inputpath')
outpath = @commandline.option('outputpath')
inppath = inppath + '/' if not inppath.empty?
outpath = outpath + '/' if not outpath.empty?
prefix = @commandline.option('prefix')
suffix = @commandline.option('suffix')
inpfilename = "#{inppath}#{filename}"
outfilename = "#{outpath}#{prefix}#{filename.sub(/\.([^\.]*?)$/, '')}#{suffix}.pdf"
magick.setvariable('inputfile' , inpfilename)
magick.setvariable('outputfile', outfilename)
magick.autoconvert
end
def handle_inkscape(inkscape,filename)
report("converting svg(z) file #{filename} into pdf")
inppath = @commandline.option('inputpath')
outpath = @commandline.option('outputpath')
inppath = inppath + '/' if not inppath.empty?
outpath = outpath + '/' if not outpath.empty?
prefix = @commandline.option('prefix')
suffix = @commandline.option('suffix')
inpfilename = "#{inppath}#{filename}"
outfilename = "#{outpath}#{prefix}#{filename.sub(/\.([^\.]*?)$/, '')}#{suffix}.pdf"
inkscape.setvariable('inputfile' , inpfilename)
inkscape.setvariable('outputfile', outfilename)
if @commandline.option('verbose') || @commandline.option('debug') then
logname = filename.gsub(/\.[^\.]*?$/, '.log')
report("log info saved in #{logname}")
inkscape.convert(logname) # logname ook doorgeven
else
inkscape.convert
end
end
def handle_ghostscript(ghostscript,filename)
ghostscript.reset
method = ghostscript.method(@commandline.option('method'))
force = ghostscript.method(@commandline.option('force'))
ghostscript.setvariable('method', method)
ghostscript.setvariable('force', force)
# report("conversion method #{method}")
inppath = @commandline.option('inputpath')
outpath = @commandline.option('outputpath')
inppath = inppath + '/' if not inppath.empty?
outpath = outpath + '/' if not outpath.empty?
prefix = @commandline.option('prefix')
suffix = @commandline.option('suffix')
ok = false
if ghostscript.pdfmethod?(method) then
report("converting pdf file #{filename} into pdf")
if prefix.empty? && suffix.empty? && inppath.empty? && outpath.empty? then
prefix = ghostscript.pdfprefix(method)
end
if ghostscript.pdffile?(filename) then
filename = filename.sub(/\.pdf$/, '')
inpfilename = "#{inppath}#{filename}.pdf"
outfilename = "#{outpath}#{prefix}#{filename}#{suffix}.pdf"
ghostscript.setvariable('inputfile' ,inpfilename)
ghostscript.setvariable('outputfile',outfilename)
if FileTest.file?(inpfilename) then
ok = ghostscript.convert
else
report("no file found #{filename}")
end
else
report("no pdf file #{filename}")
end
elsif ghostscript.psfile?(filename) then
if filename =~ /(.*)\.([^\.]*?)$/io then
filename, filesuffix = $1, $2
else
filesuffix = 'eps'
end
report("converting #{filesuffix} (ps) into pdf")
inpfilename = "#{inppath}#{filename}.#{filesuffix}"
outfilename = "#{outpath}#{prefix}#{filename}#{suffix}.pdf"
ghostscript.setvariable('inputfile' , inpfilename)
ghostscript.setvariable('outputfile', outfilename)
if FileTest.file?(inpfilename) then
ok = ghostscript.convert
if ! ok && FileTest.file?(outfilename) then
begin
File.delete(outfilename)
rescue
end
end
else
report("no file with name #{filename} found")
end
else
report('file must be of type eps/ps/ai/pdf')
end
return ok
end
end
# ook pdf -> pdf onder optie 0, andere kleurruimte
logger = Logger.new(banner.shift)
commandline = CommandLine.new
commandline.registerflag('debug')
commandline.registerflag('verbose')
commandline.registerflag('nopipe')
commandline.registervalue('method',2)
commandline.registervalue('offset',0)
commandline.registervalue('prefix')
commandline.registervalue('suffix')
commandline.registervalue('inputpath')
commandline.registervalue('outputpath')
commandline.registerflag('watch')
commandline.registerflag('force')
commandline.registerflag('recurse')
commandline.registervalue('delay',2)
commandline.registervalue('colormodel','cmyk')
commandline.registervalue('pattern','')
commandline.registeraction('help')
commandline.registeraction('version')
commandline.registeraction('convert', 'convert ps into pdf')
commandline.registeraction('request', 'handles exa request file')
commandline.registeraction('watch', 'watch folders for conversions (untested)')
commandline.expand
logger.verbose if (commandline.option('verbose') || commandline.option('debug'))
Commands.new(commandline,logger,banner).send(commandline.action || 'main')
|