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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
|
#!/usr/bin/env ruby
# program : pdftools
# copyright : PRAGMA Advanced Document Engineering
# version : 2003-2005
# author : Hans Hagen
#
# project : ConTeXt / eXaMpLe
# concept : Hans Hagen
# info : j.hagen@xs4all.nl
# www : www.pragma-ade.com
# This script will harbor some handy manipulations on tex
# related files.
banner = ['PDFTools', 'version 1.2.1', '2003/2005', 'PRAGMA ADE/POD']
$: << File.expand_path(File.dirname($0)) ; $: << File.join($:.last,'lib') ; $:.uniq!
require 'base/switch'
require 'base/logger'
require 'ftools'
class File
def File.deletefiles(*filenames)
filenames.flatten.each do |filename|
begin
delete(filename) if FileTest.file?(filename)
rescue
end
end
end
def File.needsupdate(oldname,newname)
begin
return File.stat(oldname).mtime != File.stat(newname).mtime
rescue
return true
end
end
def File.syncmtimes(oldname,newname)
begin
t = Time.now # i'm not sure if the time is frozen, so we do it here
File.utime(0,t,oldname,newname)
rescue
end
end
def File.replacesuffix(oldname,subpath='')
newname = File.expand_path(oldname.sub(/\.\w+?$/,'.pdf'))
File.join(File.dirname(newname),subpath,File.basename(newname))
end
end
class ImageMagick
def initialize
begin
version = `convert -version`
rescue
@binary = nil
ensure
if (version) && (! version.empty?) && (version =~ /ImageMagick/mo) && (version =~ /version/mio) then
@binary = 'convert'
else
@binary = 'imagemagick'
end
end
end
def process(arguments)
begin
@binary && system("#{@binary} #{arguments}")
rescue
false
end
end
end
class TexExec
def initialize
@binary = 'texmfstart texexec.pl --pdf --batch --silent --purge'
end
def process(arguments,once=true)
begin
if once then
@binary && system("#{@binary} --once #{arguments}")
else
@binary && system("#{@binary} #{arguments}")
end
rescue
false
end
end
end
class PdfImages
def initialize
@binary = "pdfimages"
end
def process(arguments)
begin
@binary && system("#{@binary} #{arguments}")
rescue
false
end
end
end
class ConvertImage
def initialize(command=nil)
@command = command
end
def convertimage(filename)
return if filename =~ /\.(pdf)$/io
retain = @command.option('retain')
subpath = @command.option('subpath')
if filename =~ /\s/ then
@command.report("skipping strange filename '#{filename}'")
else
newname = File.replacesuffix(filename,subpath)
# newname.gsub!(s/[^a-zA-Z0-9\_-\.]/o, '-')
begin
File.makedirs(File.dirname(newname))
rescue
end
if ! retain || File.needsupdate(filename,newname) then
imagemagick = ImageMagick.new
if imagemagick then
ok = imagemagick.process("-compress zip -quality 99 #{filename} #{newname}")
File.syncmtimes(oldname,newname) if retain
end
end
end
end
end
class DownsampleImage
def initialize(command=nil)
@command = command
end
def convertimage(filename)
return if filename =~ /\.(pdf)$/io
retain = @command.option('retain')
subpath = @command.option('subpath')
if @command.option('lowres') then
method = '4'
elsif @command.option('medres') || @command.option('normal') then
method = '5'
else
method = '4'
end
if filename =~ /\s/ then
@command.report("skipping strange filename '#{filename}'")
else
newname = File.replacesuffix(filename,subpath)
begin
File.makedirs(File.dirname(newname))
rescue
end
if ! retain || File.needsupdate(filename,newname) then
ok = system("texmfstart pstopdf.rb --method=#{method} #{filename} #{newname}")
File.syncmtimes(oldname,newname) if retain
end
end
end
end
class ColorImage
def initialize(command=nil,tmpname='pdftools')
@command = command
@tmpname = tmpname
@colorname = nil
@colorspec = nil
@colorspace = nil
end
def registercolor(spec='.5',name='c')
name = name || 'c'
spec = spec.split(',')
case spec.length
when 4
@colorname, @colorspec, @colorspace = name, spec.join('/'), 'cmyk'
when 3
@colorname, @colorspec, @colorspace = name, spec.join('/'), 'rgb'
when 1
@colorname, @colorspec, @colorspace = name, spec.join('/'), 'gray'
else
@colorname, @colorspec, @colorspace = nil, nil, nil
end
end
def convertimage(filename)
invert = @command.option('invert')
retain = @command.option('retain')
subpath = @command.option('subpath')
subpath += '/' unless subpath.empty?
if @colorname && ! @colorname.empty? && @colorspec && ! @colorspec.empty? then
basename = filename.sub(/\.\w+?$/,'')
oldname = filename
ppmname = @tmpname + '-000.ppm'
jpgname = @tmpname + '-000.jpg'
newname = File.expand_path(oldname)
newname = File.dirname(newname) + '/' + subpath + @colorname + '-' + File.basename(newname)
newname.sub!(/\.\w+?$/, '.pdf')
begin
File.makedirs(File.dirname(newname))
rescue
end
if ! retain || File.needsupdate(filename,newname) then
pdfimages = PdfImages.new
imagemagick = ImageMagick.new
if pdfimages && imagemagick then
File.deletefiles(ppmname,jpgname,newname)
if filename =~ /\.(pdf)$/io then
ok = pdfimages.process("-j -f 1 -l 1 #{filename} #{@tmpname}")
if ok then
if FileTest.file?(ppmname) then
inpname = ppmname
elsif FileTest.file?(jpgname) then
inpname = jpgname
else
ok = false
end
if ok then
switch = if ! invert then '-negate' else '' end
# make sure that we keep the format
tmpname = File.basename(inpname)
tmpname = tmpname.sub(/(.*)\..*?$/,@tmpname) # somehow sub! fails here
ok = imagemagick.process("-colorspace gray #{switch} #{inpname} #{tmpname}")
if ! ok || ! FileTest.file?(tmpname) then
# problems
else
ok = imagemagick.process("-colorspace #{switch} #{@colorspace} -colorize #{@colorspec} -compress zip #{tmpname} #{newname}")
if ! ok || ! FileTest.file?(newname) then
# unable to colorize image
else
# conversion done
end
end
end
end
else
# make sure that we keep the format
tmpname = File.basename(basename)
tmpname = tmpname.sub(/(.*)\..*?$/,@tmpname) # somehow sub! fails here
ok = imagemagick.process("-colorspace gray #{oldname} #{tmpname}")
if ! ok || ! FileTest.file?(tmpname) then
# unable to convert color to gray
else
ok = imagemagick.process("-colorspace #{@colorspace} -colorize #{@colorspec} -compress zip #{tmpname} #{newname}")
if ! ok || ! FileTest.file?(newname) then
# unable to colorize image
else
# conversion done
end
end
end
File.deletefiles(ppmname,jpgname,tmpname)
File.syncmtimes(filename,newname) if retain
end
end
end
end
end
class SpotColorImage
def initialize(command=nil, tmpname='pdftools')
@command = command
@tmpname = tmpname
@colorname = nil
@colorspec = nil
@colorspace = nil
@colorfile = nil
end
def registercolor(spec='.5',name='unknown')
name = name || 'unknown'
if spec =~ /^[\d\.\,]+$/ then
spec = spec.split(',')
case spec.length
when 4
@colorname, @colorspec, @colorspace = name, ["c=#{spec[0]}","m=#{spec[1]}","y=#{spec[2]}","k=#{spec[3]}"].join(','), 'cmyk'
when 3
@colorname, @colorspec, @colorspace = name, ["r=#{spec[0]}","g=#{spec[1]}","b=#{spec[2]}"].join(','), 'rgb'
when 1
@colorname, @colorspec, @colorspace = name, ["s=#{spec[0]}"].join(','), 'gray'
else
@colorname, @colorspec, @colorspace = nil, nil, nil
end
else
@colorname, @colorfile = name, spec
end
end
def convertgrayimage(filename)
invert = @command.option('invert')
retain = @command.option('retain')
subpath = @command.option('subpath')
subpath += '/' unless subpath.empty?
if @colorname && ! @colorname.empty? && ((@colorspec && ! @colorspec.empty?) || (@colorfile && ! @colorfile.empty?)) then
basename = filename.sub(/\.\w+?$/,'')
oldname = filename # png jpg pdf
newname = File.expand_path(oldname)
ppmname = @tmpname + '-000.ppm'
jpgname = @tmpname + '-000.jpg'
outname = @tmpname + '-000.pdf'
texname = @tmpname + '-temp.tex'
pdfname = @tmpname + '-temp.pdf'
newname = File.dirname(newname) + '/' + subpath + @colorname + '-' + File.basename(newname)
newname.sub!(/\.\w+?$/, '.pdf')
begin
File.makedirs(File.dirname(newname))
rescue
end
if ! retain || File.needsupdate(filename,newname) then
pdfimages = PdfImages.new
imagemagick = ImageMagick.new
texexec = TexExec.new
if pdfimages && imagemagick && texexec then
if filename =~ /\.(jpg|png|pdf)$/io then
@command.report("processing #{basename}")
File.deletefiles(ppmname,jpgname,newname)
switch = if ! invert then '-negate' else '' end
if filename =~ /\.(pdf)$/io then
ok = pdfimages.process("-j -f 1 -l 1 #{oldname} #{@tmpname}")
if ok then
if FileTest.file?(ppmname) then
inpname = ppmname
elsif FileTest.file?(jpgname) then
inpname = jpgname
else
ok = false
end
if ok then
ok = imagemagick.process("-colorspace gray #{switch} -compress zip #{inpname} #{outname}")
end
end
else
ok = imagemagick.process("-colorspace gray #{switch} -compress zip #{oldname} #{outname}")
end
if ok then
ok = false unless FileTest.file?(outname)
end
if ok then
if f = File.open(texname, 'w') then
f.puts(conversionfile(filename,outname,newname))
f.close
ok = texexec.process(texname)
else
ok = false
end
@command.report("error in processing #{newname}") unless ok
if FileTest.file?(pdfname) then
if f = File.open(pdfname,'r') then
f.binmode
begin
if g = File.open(newname,'w') then
g.binmode
data = f.read
# pdftex (direct) & imagemagick (indirect)
if data =~ /(\d+)\s+0\s+obj\s+\[\/Separation\s+\/#{@colorname}/mos then
@command.report("replacing separation color")
object = $1
data.gsub!(/(\/Type\s+\/XObject.*?)(\/ColorSpace\s*(\/DeviceGray|\/DeviceCMYK|\/DeviceRGB|\d+\s+\d+\s+R))/mois) do
$1 + "/ColorSpace #{object} 0 R".ljust($2.length)
end
elsif data =~ /(\d+)\s+0\s+obj\s+\[\/Indexed\s*\[/mos then
@command.report("replacing indexed color")
# todo: more precise check on color
object = $1
data.gsub!(/(\/Type\s+\/XObject.*?)(\/ColorSpace\s*(\/DeviceGray|\/DeviceCMYK|\/DeviceRGB|\d+\s+\d+\s+R))/mois) do
$1 + "/ColorSpace #{object} 0 R".ljust($2.length)
end
elsif data =~ /(\d+)\s+0\s+obj\s+\[\/Separation/mos then
@command.report("replacing separation color")
object = $1
data.gsub!(/(\/Type\s+\/XObject.*?)(\/ColorSpace\s*(\/DeviceGray|\/DeviceCMYK|\/DeviceRGB|\d+\s+\d+\s+R))/mois) do
$1 + "/ColorSpace #{object} 0 R".ljust($2.length)
end
end
g.write(data)
g.close
end
rescue
@command.report("error in converting #{newname}")
else
@command.report("#{newname} is converted")
end
f.close
end
else
@command.report("error in writing #{newname}")
end
else
@command.report("error in producing #{newname}")
end
File.deletefiles(ppmname,jpgname,outname)
# File.deletefiles(texname,pdfname)
File.syncmtimes(filename,newname) if retain
end
else
@command.report("error in locating binaries")
end
else
@command.report("#{newname} is not changed")
end
end
end
private
# % example colorfile:
#
# \definecolor [darkblue] [c=1,m=.38,y=0,k=.64] % pantone pms 2965 uncoated m
# \definecolor [darkyellow] [c=0,m=.28,y=1,k=.06] % pantone pms 124 uncoated m
#
# % \definecolor [darkblue-100] [darkblue] [p=1]
# % \definecolor [darkyellow-100] [darkyellow] [p=1]
#
# \definecolorcombination [pdftoolscolor] [darkblue=.12,darkyellow=.28] [c=.1,m=.1,y=.3,k=.1]
def conversionfile(originalname,filename,finalname)
tex = "\\setupcolors[state=start]\n"
if @colorfile then
tex += "\\readfile{#{@colorfile}}{}{}\n"
tex += "\\starttext\n"
# tex += "\\predefineindexcolor[pdftoolscolor]\n"
tex += "\\startTEXpage\n"
tex += "\\pdfimage{#{filename}}\n"
tex += "\\stopTEXpage\n"
tex += "\\stoptext\n"
else
tex += "\\definecolor[#{@colorname}][#{@colorspec}]\n"
tex += "\\definecolor[pdftoolscolor][#{@colorname}][p=1]\n"
tex += "\\starttext\n"
tex += "\\startTEXpage\n"
tex += "\\hbox{\\color[pdftoolscolor]{\\pdfimage{#{filename}}}}\n"
tex += "\\stopTEXpage\n"
tex += "\\stoptext\n"
end
tex += "\n"
tex += "% old: #{originalname}\n"
tex += "% new: #{finalname}\n"
return tex
end
end
module XML
def XML::version
"<?xml version='1.0'?>"
end
def XML::start(element, attributes='')
if attributes.empty? then
"<#{element}>"
else
"<#{element} #{attributes}>"
end
end
def XML::end(element)
"</#{element}>"
end
def XML::empty(element, attributes='')
if attributes && attributes.empty? then
"<#{element}/>"
else
"<#{element} #{attributes}/>"
end
end
def XML::element(element, attributes='', content='')
if content && ! content.empty? then
XML::start(element,attributes) + content + XML::end(element)
else
XML::empty(element,attributes)
end
end
def XML::box(tag, rect, type=1)
case type
when 1
if rect && ! rect.empty? then
rect = rect.split(' ')
XML::element("#{tag}box", '',
XML::element("llx", '', rect[0]) +
XML::element("lly", '', rect[1]) +
XML::element("ulx", '', rect[2]) +
XML::element("uly", '', rect[3]) )
else
XML::empty("#{tag}box")
end
when 2
if rect && ! rect.empty? then
rect = rect.split(' ')
XML::element("box", "type='#{tag}'",
XML::element("llx", '', rect[0]) +
XML::element("lly", '', rect[1]) +
XML::element("ulx", '', rect[2]) +
XML::element("uly", '', rect[3]) )
else
XML::empty("box", "type='#{tag}'")
end
when 3
if rect && ! rect.empty? then
rect = rect.split(' ')
XML::element("box", "type='#{tag}' llx='#{rect[0]}' lly='#{rect[1]}' ulx='#{rect[2]}' uly='#{rect[3]}'")
else
XML::empty("box", "type='#{tag}'")
end
else
''
end
end
def XML::crlf
"\n"
end
def XML::skip(n=1)
' '*n
end
end
class Commands
include CommandBase
# alias savedhelp :help
# def help
# savedhelp
# report("under construction (still separate tools)")
# end
# filename.pdf --spotimage --colorname=darkblue --colorspec=1,0.38,0,0.64
def spotimage
if ! @commandline.argument('first').empty? && files = findfiles() then
colorname = @commandline.option('colorname')
colorspec = @commandline.option('colorspec')
if colorname && ! colorname.empty? && colorspec && ! colorspec.empty? then
files.each do |filename|
s = SpotColorImage.new(self)
s.registercolor(colorspec,colorname)
s.convertgrayimage(filename)
end
else
report("provide --colorname=somename --colorspec=c,m,y,k")
end
else
report("provide filename (png, jpg, pdf)")
end
end
def colorimage
if ! @commandline.argument('first').empty? && files = findfiles() then
colorname = @commandline.option('colorname')
colorspec = @commandline.option('colorspec')
if colorspec && ! colorspec.empty? then
files.each do |filename|
s = ColorImage.new(self)
s.registercolor(colorspec,colorname) # name optional
s.convertimage(filename)
end
else
report("provide --colorspec=c,m,y,k")
end
else
report("provide filename")
end
end
def convertimage
if ! @commandline.argument('first').empty? && files = findfiles() then
files.each do |filename|
s = ConvertImage.new(self)
s.convertimage(filename)
end
else
report("provide filename")
end
end
def downsampleimage
if ! @commandline.argument('first').empty? && files = findfiles() then
files.each do |filename|
s = DownsampleImage.new(self)
s.convertimage(filename)
end
else
report("provide filename")
end
end
def info
if files = findfiles() then
print(XML.version + XML.crlf)
print(XML.start('pdfinfo', "xmlns='http://www.pragma-ade.com/schemas/pdfinfo.rng'") + XML.crlf)
files.each do |filename|
if filename =~ /\.pdf$/io then
begin
data = `pdfinfo -box #{filename}`.chomp.split("\n")
rescue
data = nil
end
if data then
pairs = Hash.new
data.each do |d|
if (d =~ /^\s*(.*?)\s*\:\s*(.*?)\s*$/mois) then
key, val = $1, $2
pairs[key.downcase.sub(/ /,'')] = val
end
end
print(XML.skip(1) + XML.start('pdffile', "filename='#{filename}'") + XML.crlf)
print(XML.skip(2) + XML.element('path', '', File.expand_path(filename)) + XML.crlf)
if pairs.key?('error') then
print(XML.skip(2) + XML.element('comment', '', pairs['error']) + XML.crlf)
else
print(XML.skip(2) + XML.element('version', '', pairs['pdfversion']) + XML.crlf)
print(XML.skip(2) + XML.element('pages', '', pairs['pages' ]) + XML.crlf)
print(XML.skip(2) + XML.element('title', '', pairs['title' ]) + XML.crlf)
print(XML.skip(2) + XML.element('subject', '', pairs['subject' ]) + XML.crlf)
print(XML.skip(2) + XML.element('author', '', pairs['author' ]) + XML.crlf)
print(XML.skip(2) + XML.element('producer', '', pairs['producer' ]) + XML.crlf)
if pairs.key?('creationdate') then
pairs['creationdate'].sub!(/(\d\d)\/(\d\d)\/(\d\d)/) do
'20' + $3 + '-' + $1 + '-' +$2
end
pairs['creationdate'].sub!(/(\d\d)\/(\d\d)\/(\d\d\d\d)/) do
$3 + '-' + $1 + '-' + $2
end
print(XML.skip(2) + XML.element('creationdate', '', pairs['creationdate']) + XML.crlf)
end
if pairs.key?('moddate') then
if pairs['moddate'] =~ /(\d\d\d\d)(\d\d)(\d\d)/ then
pairs['moddate'] = "#{$1}-#{$2}-#{$3}"
end
print(XML.skip(2) + XML.element('modificationdate', '', pairs['moddate']) + XML.crlf)
end
print(XML.skip(2) + XML.element('tagged', '', pairs['tagged' ]) + XML.crlf)
print(XML.skip(2) + XML.element('encrypted', '', pairs['encrypted']) + XML.crlf)
print(XML.skip(2) + XML.element('optimized', '', pairs['optimized']) + XML.crlf)
if pairs.key?('PageSize') then
print(XML.skip(2) + XML.element('width', '', pairs['pagesize'].sub(/\s*(.*?)\s+(.*?)\s+.*/, $1)) + XML.crlf)
print(XML.skip(2) + XML.element('height', '', pairs['pagesize'].sub(/\s*(.*?)\s+(.*?)\s+.*/, $2)) + XML.crlf)
end
if pairs.key?('FileSize') then
print(XML.skip(2) + XML.element('size', '', pairs['filesize'].sub(/\s*(.*?)\s+.*/, $1)) + XML.crlf)
end
print(XML.skip(2) + XML.box('media', pairs['mediabox']) + XML.crlf)
print(XML.skip(2) + XML.box('crop' , pairs['cropbox' ]) + XML.crlf)
print(XML.skip(2) + XML.box('bleed', pairs['bleedbox']) + XML.crlf)
print(XML.skip(2) + XML.box('trim' , pairs['trimBox' ]) + XML.crlf)
print(XML.skip(2) + XML.box('art' , pairs['artbox' ]) + XML.crlf)
end
print(XML.skip(1) + XML.end('pdffile') + XML.crlf)
end
end
end
print(XML.end('pdfinfo') + XML.crlf)
end
end
def countpages
if @commandline.option('pattern') then
filenames, n = globfiles(@commandline.option('pattern'),'pdf'), 0
else
filenames, n = findfiles('pdf'), 0
end
threshold = @commandline.option('threshold').to_i rescue 0
filenames.each do |filename|
if `pdfinfo #{filename}`.chomp =~ /^pages\s*\:\s*(\d+)/mois then
p = $1
m = p.to_i rescue 0
if threshold == 0 or m > threshold then
report("#{p.rjust(4)} pages found in #{filename}")
n += m
end
end
end
report("")
report("#{n.to_s.rjust(4)} pages in total")
end
def analyzefile
# needs an update
filenames = @commandline.arguments
filenames.each do |filename|
if filename && FileTest.file?(filename) && filename =~ /\.pdf/io then
filesize = FileTest.size(filename)
report("analyzing file : #{filename}")
report("file size : #{filesize}")
if pdf = File.open(filename) then
pdf.binmode
nofobject, nofxform, nofannot, noflink, nofwidget, nofnamed, nofscript, nofcross = 0, 0, 0, 0, 0, 0, 0, 0
while data = pdf.gets do
data.scan(/\d+\s+\d+\s+obj/o) do nofobject += 1 end
data.scan(/\/Type\s*\/XObject/o) do nofxform += 1 end
data.scan(/\/Type\s*\/Annot/o) do nofannot += 1 end
data.scan(/\/GoToR\s*\/F/o) do nofcross += 1 end
data.scan(/\/Subtype\s*\/Link/o) do noflink += 1 end
data.scan(/\/Subtype\s*\/Widget/o) do nofwidget += 1 end
data.scan(/\/S\s*\/Named/o) do nofnamed += 1 end
data.scan(/\/S\s*\/JavaScript/o) do nofscript += 1 end
end
pdf.close
report("objects : #{nofobject}")
report("xforms : #{nofxform}")
report("annotations : #{nofannot}")
report("links : #{noflink} (#{nofnamed} named / #{nofscript} scripts / #{nofcross} files)")
report("widgets : #{nofwidget}")
end
end
end
end
end
logger = Logger.new(banner.shift)
commandline = CommandLine.new
commandline.registeraction('spotimage' , 'filename --colorspec= --colorname= [--retain --invert --subpath=]')
commandline.registeraction('colorimage', 'filename --colorspec= [--retain --invert --colorname= ]')
commandline.registeraction('convertimage', 'filename [--retain --subpath]')
commandline.registeraction('downsampleimage', 'filename [--retain --subpath --lowres --normal]')
commandline.registeraction('info', 'filename')
commandline.registeraction('countpages', '[--pattern --threshold]')
commandline.registeraction('analyzefile' , 'filename')
commandline.registeraction('help')
commandline.registeraction('version')
commandline.registervalue('colorname')
commandline.registervalue('colorspec')
commandline.registervalue('subpath')
commandline.registervalue('pattern')
commandline.registervalue('threshold',0)
commandline.registerflag('lowres')
commandline.registerflag('medres')
commandline.registerflag('normal')
commandline.registerflag('invert')
commandline.registerflag('retain')
commandline.expand
Commands.new(commandline,logger,banner).send(commandline.action || 'help')
|