summaryrefslogtreecommitdiff
path: root/Master/tlpkg/tlgs/lib/pdf_info.ps
blob: 0b4654ab073abc62e2c2969c7577bf2c0f7c9af4 (plain)
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
%!PS
% Copyright (C) 2001-2020 Artifex Software, Inc.
% All Rights Reserved.
%
% This software is provided AS-IS with no warranty, either express or
% implied.
%
% This software is distributed under license and may not be copied,
% modified or distributed except as expressly authorized under the terms
% of the license contained in the file LICENSE in this distribution.
%
% Refer to licensing information at http://www.artifex.com or contact
% Artifex Software, Inc.,  1305 Grant Avenue - Suite 200, Novato,
% CA 94945, U.S.A., +1(415)492-9861, for further information.
%
%
% $Id: pdf_info.ps 6300 2005-12-28 19:56:24Z alexcher $

% Dump some info from a PDF file

% usage: gs -dNODISPLAY -q -sFile=____.pdf [-dDumpMediaSizes=false] [-dDumpFontsNeeded=false] [-dDumpXML]
%                                          [-dDumpFontsUsed [-dShowEmbeddedFonts] ] lib/pdf_info.ps

128 dict begin

/QUIET true def		% in case they forgot

/showoptions {
  (           where "options" are:) =
  (           -dDumpMediaSizes=false    (default true) MediaBox and CropBox for each page) =
  (           -dDumpFontsNeeded=false   (default true)Fonts used, but not embedded) =
  (           -dDumpXML                 print the XML Metadata from the PDF, if present) =
  (           -dDumpFontsUsed           List all fonts used) =
  (           -dShowEmbeddedFonts       only meaningful with -dDumpFontsUsed) =
  (\n          If no options are given, the default is -dDumpMediaSizes -dDumpFontsNeeded) =
  () =
  flush
} bind def

/DumpMediaSizes where { pop } { /DumpMediaSizes true def } ifelse
/DumpFontsNeeded where { pop } { /DumpFontsNeeded true def } ifelse

[ {.shellarguments} stopped not
  {
    { counttomark 1 eq {
        dup 0 get (-) 0 get ne {
          % File specified on the command line using:  -- lib/pdf_info.ps infile.pdf
          /File exch def
          false   % don't show usage
        } {
          true    % show usage and quit
        } ifelse
      } { true } ifelse
      {
        (\n*** Usage: gs [options] -- lib/pdf_info.ps infile.pdf  ***\n\n) print
        showoptions
        quit
      } if
    } if
  } if
/File where not {
  (\n   *** Missing input file name \(use -sFile=____.pdf\)\n) =
  (    usage: gs -dNODISPLAY -q -sFile=____.pdf [ options ] lib/pdf_info.ps\n) =
  showoptions
  quit
} if
cleartomark		% discard the dict from --where--

% ---- No more executable code on the top level after this line -----
% ---- except 2 lines at the very end                           -----

%% When checking Resources for Font objects, we must ensure that we don't end
%% up following a circular reference. Circular references in Resources should
%% not, of course, be present but we've seen a number of cases. If we do detect
%% a circular reference, then stop checking that chain.

/oforce_no_circular {
  dup type /packedarraytype eq {
    dup 0 get
    currentdict /ProcessedResources known {
      dup ProcessedResources exch known {
        pop pop //null
      } {
        dup ProcessedResources 3 1 roll put
        oforce
      } ifelse
    } {
      oforce
    } ifelse
  } {
    oforce
  } ifelse
} bind def

/printXML {	% <string> printXML -
  % print non-blank lines without trailing spaces
  dup dup length 1 sub -1 0 {
    1 index 1 index get 32 eq {
      0 exch getinterval exch
    } {
      exch = exit	% non-blank on this line
    }
    ifelse
  } for
  pop pop		% clean up
} bind def

/dump-pdf-info {    % (fname) -> -
  () = (        ) print print ( has ) print
  PDFPageCount dup =print 10 mod 1 eq { ( page.\n) } { ( pages\n) } ifelse = flush

  Trailer /Root oget /AcroForm knownoget {
    /XFA knownoget {
      (File uses XFA forms.\n\n) print
      pop
    } if
  } if

  /DumpXML where {
    pop
    Trailer /Root oget /Metadata knownoget {
      //false resolvestream
      { dup 256 string readline exch printXML not { exit } if } loop
      pop		% done with the stream
      (_____________________________________________________________) =
      flush
    } if
  } if

  % Print out the "Info" dictionary if present
  Trailer /Info knownoget {
     dup /Title knownoget { (Title: ) print = flush } if
     dup /Author knownoget { (Author: ) print = flush } if
     dup /Subject knownoget { (Subject: ) print = flush } if
     dup /Keywords knownoget { (Keywords: ) print = flush } if
     dup /Creator knownoget { (Creator: ) print = flush } if
     dup /Producer knownoget { (Producer: ) print = flush } if
     dup /CreationDate knownoget { (CreationDate: ) print = flush } if
     dup /ModDate knownoget { (ModDate: ) print = flush } if
     dup /Trapped knownoget { (Trapped: ) print = flush } if
     pop
  } if

} bind def

% <page index> <page dict> dump-media-sizes -
/dump-media-sizes {
  DumpMediaSizes {
    () =
    % Print out the Page Size info for each page.
    (Page ) print =print
    dup /UserUnit pget {
      ( UserUnit: ) print =print
    } if
    dup /MediaBox pget {
      ( MediaBox: ) print oforce_array ==only
    } if
    dup /CropBox pget {
      ( CropBox: ) print oforce_array ==only
    } if
    dup /BleedBox pget {
      ( BleedBox: ) print oforce_array ==only
    } if
    dup /TrimBox pget {
      ( TrimBox: ) print oforce_array ==only
    } if
    dup /ArtBox pget {
      ( ArtBox: ) print oforce_array ==only
    } if
    dup /Rotate pget {
       (    Rotate = ) print =print
    } if
    dup /Annots pget {
       pop
        (     Page contains Annotations) print
    } if
    pageusestransparency {
        (     Page uses transparency features) print
    } if
    () = flush
  }
  {
    pop pop
  } ifelse
} bind def

% List of standard font names for use when we are showing the FontsNeeded
/StdFontNames [
 /Times-Roman /Helvetica /Courier /Symbol
 /Times-Bold /Helvetica-Bold /Courier-Bold /ZapfDingbats
 /Times-Italic /Helvetica-Oblique /Courier-Oblique
 /Times-BoldItalic /Helvetica-BoldOblique /Courier-BoldOblique
] def

/res-type-dict 10 dict begin
  /Font {
    {
      exch pop oforce_no_circular
      dup //null ne {
        dup /DescendantFonts knownoget {
           exch pop 0 get oforce_no_circular
           %% Check to see if a potentail circular reference was detected. This could be a
           %% false positive caused by other fonts using the same DescendantFont(s), but if
           %% so then we've already cehcked them anyway.
           dup //null eq { pop << >>} if
        } if
        dup /FontDescriptor knownoget {
          dup /FontFile known 1 index /FontFile2 known or exch /FontFile3 known or
          /ShowEmbeddedFonts where { pop pop //false } if {
            pop			% skip embedded fonts
          } {
            /BaseFont knownoget { %  not embedded
              2 index exch //null put
            } if
          } ifelse
        } {
          /BaseFont knownoget { % no FontDescriptor, not embedded
            2 index exch //null put
          } if
        } ifelse
      } {
        pop
      } ifelse
    } forall	% traverse the dictionary
  } bind def

  /XObject {
    {
      exch pop oforce_no_circular
      dup //null ne {
        dup /Subtype knownoget {
          /Form eq {
            /Resources knownoget {
              get-fonts-from-res
            } if
          } {
            pop
          } ifelse
        } {
          pop
        } ifelse
      } {
        pop
      } ifelse
    } forall
  } bind def

  /Pattern {
    {
      exch pop oforce_no_circular
      dup //null ne {
        /Resources knownoget {
          get-fonts-from-res
        } if
      } {
        pop
      } ifelse
    } forall
  } bind def
currentdict end readonly def

% <dict for fonts> <<res-dict>> get-fonts-from-res -
/get-fonts-from-res {
  oforce_no_circular
  dup //null ne {
    {
      oforce_no_circular
      dup //null ne {
        //res-type-dict 3 -1 roll
        .knownget {
          exec
        } {
          pop
        } ifelse
      } {
        pop pop
      } ifelse
    } forall
  } {
    pop
  } ifelse
} bind def

currentdict /res-type-dict undef

/getPDFfonts {	%	<dict for fonts> <page dict> getPDFfonts -
  /ProcessedResources 10 dict def
  dup /Resources pget { get-fonts-from-res } if
  /Annots knownoget {
    { oforce_no_circular
      dup //null ne {
        /AP knownoget {
          { exch pop oforce_no_circular
            dup //null ne {
              dup /Resources knownoget {
                get-fonts-from-res
              } if
              { exch pop oforce_no_circular
                dup type /dicttype eq {
                  /Resources knownoget {
                    get-fonts-from-res
                  } if
                } {
                  pop
                } ifelse
              } forall
            } {
              pop
            } ifelse
          } forall
        } if
      } {
        pop
      } ifelse
    } forall
  } if
  pop
  currentdict /ProcessedResources undef
} bind def

/dump-fonts-used { % <dict for fonts> dump-fonts-used -
  % If DumpFontsUsed is not true, then remove the 'standard' fonts from the list
  systemdict /DumpFontsUsed known not {
    StdFontNames {
      1 index 1 index known { 1 index 1 index undef } if
      pop
    } forall
  } if

  % Now dump the FontsUsed dict into an array so we can sort it.
  [ 1 index { pop } forall ]
  { 100 string cvs exch 100 string cvs exch lt } .sort

  systemdict /DumpFontsUsed known
  {
    (\nFont or CIDFont resources used (plain name and ASCIIHEX string representation):) =
    { 128 string cvs dup print (    ) print (<) print 128 string cvs {16 8 string cvrs print} forall (>) print (\n) print} forall
  } {
    DumpFontsNeeded {
      dup length 0 gt {
        (\nFonts Needed that are not embedded \(system fonts required\):) =
        { (    ) print 128 string cvs dup print (    ) print (<) print {16 8 string cvrs print} forall (>) print (\n) print} forall
      } {
        pop
        (\nNo system fonts are needed.) =
      } ifelse
    } {
      pop
    } ifelse
  } ifelse
  pop
} bind def

% Copy selected subfiles to temporary files and return the file names
% as a PostScript names to protect them from restore.
% Currently, all PDF files in the Portfolio are extracted and returned.
%
% - pdf_collection_files [ /temp_file_name ... /temp_file_name
/pdf_collection_files {
  mark
  Trailer /Root oget
  dup /Collection oknown {
    /Names knownoget {
      /EmbeddedFiles knownoget {
        pdf_collection_names
      } if
    } if
  } {
    pop
  } ifelse
} bind def

% Output all the info about the file
/dump {  % (title) -> -
  /PDFPageCount pdfpagecount def
  dump-pdf-info
  % dict will be populated with fonts through a call to 'getPDFfonts'
  % per page, then the contents dumped out in 'dump-fonts-used'
  1000 dict

  1 1 PDFPageCount
  {
    dup pdfgetpage dup 3 -1 roll
    dump-media-sizes
    1 index exch getPDFfonts
  } for

  dump-fonts-used

} bind def

% Choose between collection vs plain file.
% Enumerate collections and apply the dump procedure.
/enum-pdfs {		% - -> -
  File (r) file runpdfbegin
  pdf_collection_files
  dup mark eq {
    pop
    File dump
    runpdfend
  } {
    runpdfend
    ] 0 1 2 index length 1 sub {
        2 copy get exch           %  [file ... ] file i
        1 add (0123456789) cvs    %  [file ... ] file (i+1)
        File exch ( part ) exch concatstrings concatstrings
        exch                      %  [file ... ] (fname part i+1) file
        dup type /filetype eq {
          runpdfbegin
          dump
          runpdfend
          closefile
        } {
          1024 string cvs % .namestring is deprecated
%          .namestring
          dup (r) file
          runpdfbegin
          exch dump
          runpdfend
          deletefile
        } ifelse
    } for
    pop
  } ifelse
} bind def

enum-pdfs
end
quit