Class: PDFBeads::PDFBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfbeads/pdfbuilder.rb

Overview

The key class where the actual generation of a PDF file is performed.

Defined Under Namespace

Classes: Dict, Doc, FontDataProvider, PDFLabels, PDFTOC, XObj

Constant Summary collapse

@@cmodes =
Hash[
  'BilevelType'              => '/DeviceGray',
  'GrayscaleType'            => '/DeviceGray',
  'PaletteType'              => '/Indexed',
  'PaletteMatteType'         => '/Indexed',
  'TrueColorType'            => '/DeviceRGB',
  'TrueColorMatteType'       => '/DeviceRGB',
  'ColorSeparationType'      => '/DeviceCMYK',
  'ColorSeparationMatteType' => '/DeviceCMYK',
  'PaletteBilevelMatteType'  => '/DeviceGray'
]

Instance Method Summary collapse

Constructor Details

#initialize(pdfargs) ⇒ PDFBuilder

Returns a new instance of PDFBuilder.



57
58
59
60
61
62
63
64
65
# File 'lib/pdfbeads/pdfbuilder.rb', line 57

def initialize( pdfargs )
  @pdfargs = pdfargs
  @now = Time.now()
  @doc = Doc.new()
  @fdata = FontDataProvider.new()

  @dictpath = ''
  @dictobj  = nil
end

Instance Method Details

#output(outpath) ⇒ Object

Output the created PDF file to the disk.



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/pdfbeads/pdfbuilder.rb', line 332

def output( outpath )
  begin
    if outpath.eql? 'STDOUT'
      out = $stdout
    else
      out = File.open( outpath,'w' )
    end

    out.binmode if /(win|w)32$/.match( RUBY_PLATFORM )
    out.write( @doc.to_s )
    out.close unless outpath.eql? 'STDOUT'
  rescue
    $stderr.puts( "Error: could not write to #{outpath}" )
  end
end

#process(pagefiles, st_format) ⇒ Object



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
# File 'lib/pdfbeads/pdfbuilder.rb', line 67

def process( pagefiles,st_format )
  labels = toc = nil
  labels = PDFLabels.new( @pdfargs[:labels] ) unless @pdfargs[:labels].nil?
  toc    = PDFTOC.new( @pdfargs[:toc] ) unless @pdfargs[:toc].nil?
  meta   = parseMeta( @pdfargs[:meta] )

  cat = XObj.new(Hash[
    'Type'       => '/Catalog',
    'PageLayout' => "/#{@pdfargs[:pagelayout]}"
    ])
  @doc.addObject(cat)

  offsign = 'Z'
  if @now.gmt_offset > 0
    offsign = "+"
  else
    offsign = "-"
  end
  creationDate = sprintf( "D:%04d%02d%02d%02d%02d%02d%s",
    @now.year, @now.month, @now.day, @now.hour, @now.min, @now.sec, offsign )
  unless offsign.eql? 'Z'
    gmt_mins = @now.gmt_offset/60
    creationDate << sprintf( "%02d'%02d", gmt_mins/60, gmt_mins%60 )
  end
  info = XObj.new(Hash[
    'Creator'      => "(PDFBeads)",
    'Producer'     => "(PDFBeads)",
    'CreationDate' => "(#{creationDate})"
  ])
  @doc.addObject(info)
  meta.each_key do |key|
    info.addToDict(key, "(\xFE\xFF#{meta[key].to_text})")
  end

  out = XObj.new(Hash[
    'Type'  => '/Outlines',
    'Count' => 0
  ])
  @doc.addObject(out)
  cat.addToDict('Outlines', ref(out.getID))

  pages = XObj.new(Hash[
    'Type' => '/Pages'
  ])
  @doc.addObject(pages)
  cat.addToDict('Pages', ref(pages.getID))

  creator = XObj.new(Hash[
    'Subtype' => '/Artwork',
    'Creator' => "(PDFBeads)",
    'Feature' => '(Layers)'
  ])
  @doc.addObject(creator)

  ocFore = XObj.new(Hash[
    'Type'   => '/OCG',
    'Name'   => '(Foreground)',
    'Usage'  => "<</CreatorInfo #{ref(creator.getID)}>>",
    'Intent' => '[/View/Design]'
  ])
  @doc.addObject(ocFore)
  ocBack = XObj.new({
    'Type'   => '/OCG',
    'Name'   => '(Background)',
    'Usage'  => "<</CreatorInfo #{ref(creator.getID)}>>",
    'Intent' => '[/View/Design]'
  })
  @doc.addObject(ocBack)
  cat.addToDict('OCProperties', 
    sprintf("<< /OCGs[%s %s] /D<< /Intent /View /BaseState (ON) /Order[%s %s] >>>>",
      ref(ocFore.getID), ref(ocBack.getID), ref(ocFore.getID), ref(ocBack.getID)))

  page_objs = Array.new()
  pages_by_num = Hash.new()
  symd = nil
  font = nil
  pidx = 0

  if labels != nil and labels.length > 0
    nTree = "<</Nums[\n"
    labels.each do |rng|
      nTree << "#{rng[:first]} << "
      if rng.has_key? :prefix
        begin
          # If possible, use iso8859-1 (aka PDFDocEncoding) for page labels:
          # it is at least guaranteed to be safe
          ltitl = Iconv.iconv( "iso8859-1", "utf-8", rng[:prefix] ).first
          nTree << "/P (#{ltitl.to_text}) " 
        rescue Iconv::InvalidCharacter, Iconv::IllegalSequence
          ltitl = Iconv.iconv( "utf-16be", "utf-8", rng[:prefix] ).first
          # If there is no number (just prefix) then put a zero character after the prefix:
          # this makes acroread happy, but prevents displaying the number in evince
          unless rng.has_key? :style
            nTree << "/P (\xFE\xFF#{ltitl.to_text}\x00\x00) "
          # Otherwise put a formally correct Unicode string, which, however, may stumble acroread
          else
            nTree << "/P (\xFE\xFF#{ltitl.to_text}) "
          end
        end
      end
      nTree << "/S /#{rng[:style]} " if rng.has_key? :style
      nTree << "/St #{rng[:start]}" if rng.has_key? :start
      nTree << ">>\n"
    end

    nTree << "]\n>>"
    cat.addToDict('PageLabels', nTree)
    cur_range_id = 0
  end

  needs_font = false
  fonts = encodings = nil
  pagefiles.each do |p|
    unless p.hocr_path.nil?
      needs_font = true
      break
    end
  end

  if needs_font
    fonts = Array.new()
    encodings = [ [' '] ]
    fdict = XObj.new( Hash[] )
    @doc.addObject( fdict )

    descr = XObj.new( Hash[
      'Type'     => '/FontDescriptor',
      'BaseFont' => '/Times-Roman',
      ] )
    @fdata.header.each_key do |key|
      descr.addToDict( key,@fdata.header[key] )
    end
    @doc.addObject( descr )
  end

  pagefiles.each do |p|
    procSet = ['/PDF', '/ImageB']
    c_str = ''
    doc_objs = Array.new()
    lastimg = 0

    width = p.width; height = p.height
    xres  = p.x_res; yres   = p.y_res
    pwidth  = width.to_f  / xres * 72
    pheight = height.to_f / yres * 72

    p.stencils.each do |s|
      if st_format.eql? 'JBIG2'
        xobj,width,height,xres,yres = loadJBIG2Page( s[:jbig2path],s[:jbig2dict],ref(ocFore.getID) )
      else
        xobj,width,height,xres,yres = loadCCITTPage( s[:path],ref(ocFore.getID) )
      end
      break if xobj.nil?

      color = s[:rgb].join(' ') << ' rg'
      doc_objs << xobj

      c_str << "#{color} /Im#{lastimg} Do "
      lastimg += 1
    end

    fg_image = bg_image = nil
    fg_image = loadImage( p.fg_layer,ocFore.getID,procSet ) unless p.fg_layer.nil?
    bg_image = loadImage( p.bg_layer,ocBack.getID,procSet ) unless p.bg_layer.nil?

    contents = XObj.new(Hash[
      'Filter' => '/FlateDecode'
    ])
    resobj = XObj.new(Hash.new())
    resources = XObj.new(Hash[
      'XObject' => ref(resobj.getID)
    ])

    unless fg_image.nil?
      xobj = doc_objs[0]
      fg_image.addToDict('SMask', ref(xobj.getID))
      xobj.removeFromDict('ImageMask')
      xobj.addToDict('Decode', '[1 0]')
      resobj.addToDict('Im0', ref(fg_image.getID))
      doc_objs << fg_image
      c_str = '/Im0 Do '
    else
      doc_objs.each_index do |i|
        resobj.addToDict( "Im#{i}", ref(doc_objs[i].getID) )
      end
    end

    unless bg_image.nil?
      c_str = "/Im#{resobj.dictLength} Do " << c_str
      resobj.addToDict( "Im#{resobj.dictLength}", ref(bg_image.getID) )
      doc_objs << bg_image
    end
    c_str = sprintf( "q %.2f 0 0 %.2f 0 0 cm %sQ",pwidth,pheight,c_str )

    doc_objs.concat( [contents, resobj, resources] )

    hocr = nil
    unless p.hocr_path.nil?
      hocr = open( p.hocr_path ) { |f| Hpricot.parse( f ) }
      procSet << '/Text'
      c_str   << getPDFText( hocr,pheight,72.0/xres,72.0/yres,encodings )
    end

    contents.reinit( Hash[
      'Filter' => '/FlateDecode'
    ], Zlib::Deflate.deflate( c_str,9 ) )
    resources.addToDict( 'ProcSet', "[ #{procSet.join(' ')} ]" )
    resources.addToDict( 'Font', ref( fdict.getID ) ) unless hocr.nil?

    page = XObj.new(Hash[
      'Type'      => '/Page',
      'Parent'    => "#{pages.getID} 0 R",
      'MediaBox'  => sprintf( "[ 0 0 %.02f %.02f ]",pwidth,pheight ),
      'Contents'  => ref( contents.getID ),
      'Resources' => ref( resources.getID )
    ])
    # By default acroread uses /DeviceCMYK as a transparency blending space,
    # so adding an SMask image to a page would result to colors being shifted,
    # uless we take a special care of this. For more details see
    # http://comments.gmane.org/gmane.comp.tex.pdftex/3747
    unless fg_image.nil?
      cspace = '/DeviceRGB'
      cspace = fg_image.getFromDict( 'ColorSpace' ) if fg_image.hasInDict( 'ColorSpace' )
      page.addToDict( 'Group', "<< /S /Transparency /CS #{cspace} >>" )
    end
    doc_objs  << page
    doc_objs.each{ |x| @doc.addObject(x) }
    page_objs << page

    pages.addToDict( 'Count', page_objs.length )
    pages.addToDict( 'Kids', '[' << page_objs.map{|x| ref(x.getID).to_s}.join(' ') << ']' )

    pkey = (pidx + 1).to_s
    pkey = labels.getPageLabel( cur_range_id,pidx ) if labels != nil and labels.length > 0
    pages_by_num[pkey] = page.getID
    pidx += 1
    if labels != nil and labels.length > 0
      if cur_range_id < labels.length - 1 and labels[cur_range_id + 1][:first] == pidx
        cur_range_id += 1
      end
    end

    $stderr.puts("Processed #{p.name}\n")
    $stderr.puts("  Added background image from #{p.bg_layer}\n") unless bg_image.nil?
    $stderr.puts("  Added foreground image from #{p.fg_layer}\n") unless fg_image.nil?
  end

  if needs_font
    fidx = 1
    encodings.each do |enc|
      font = addFont( descr,enc,"Fnt#{fidx}" )
      fdict.addToDict( "Fnt#{fidx}",ref(font.getID) )
      fonts << font
      fidx += 1
    end
  end

  if toc != nil and toc.length > 0
    getOutlineObjs( toc,pages_by_num,page_objs[0].getID )
    cat.addToDict('Outlines', ref(toc[0][:pdfobj].getID))
    cat.addToDict('PageMode', "/UseOutlines")
  end
end