Module: Tabula::Render

Defined in:
lib/tabula/pdf_render.rb

Defined Under Namespace

Classes: PageDrawerNoText

Constant Summary collapse

TRANSPARENT_WHITE =

ugh jruby; suppresses “ambiguous method” warning that arises due to Java’s overloaded constructor.

java.awt.Color.java_class.constructor(Java::int, Java::int, Java::int, Java::int).new_instance(255, 255, 255, 0)

Class Method Summary collapse

Class Method Details

.pageToBufferedImage(page, width = 2048, pageDrawerClass = PageDrawerNoText) ⇒ Object

2048 width is important, if this is too small, thin lines won’t be drawn.



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
# File 'lib/tabula/pdf_render.rb', line 25

def self.pageToBufferedImage(page, width=2048, pageDrawerClass=PageDrawerNoText)
  cropbox = page.findCropBox
  widthPt, heightPt = cropbox.getWidth, cropbox.getHeight
  pageDimension = Dimension.new(widthPt, heightPt)
  rotation = java.lang.Math.toRadians(page.findRotation)

  scaling = width / (rotation == 0 ? widthPt : heightPt)
  widthPx, heightPx = (java.lang.Math.java_send :round, [Java::float], widthPt * scaling ), (java.lang.Math.java_send :round, [Java::float], heightPt * scaling)


  retval = if rotation != 0
             BufferedImage.new(heightPx, widthPx, BufferedImage::TYPE_BYTE_GRAY)
           else
             BufferedImage.new(widthPx, heightPx, BufferedImage::TYPE_BYTE_GRAY)
           end
  graphics = retval.getGraphics()
  graphics.setBackground(TRANSPARENT_WHITE)
  graphics.clearRect(0, 0, retval.getWidth, retval.getHeight)
  if rotation != 0
    graphics.java_send :translate, [Java::int, Java::int], retval.getWidth, 0.0
    graphics.rotate(rotation)
  end
  graphics.scale(scaling, scaling)
  drawer = pageDrawerClass.new()
  drawer.drawPage(graphics,  page, pageDimension)
  graphics.dispose

  return retval
end