File: pdfdiv.rb

package info (click to toggle)
ruby-gnome 4.2.2-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 26,648 kB
  • sloc: ruby: 67,701; ansic: 67,431; xml: 350; sh: 201; cpp: 45; makefile: 42
file content (36 lines) | stat: -rwxr-xr-x 740 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/env ruby

require "poppler"

if ARGV.size != 2
  puts "usage: #{$0} input.pdf output.pdf"
  exit(-1)
end

input, output = ARGV

doc = Poppler::Document.new(input)

width, height = doc[0].size
Cairo::PDFSurface.new(output, width / 2, height) do |surface|
  context = Cairo::Context.new(surface)

  doc.each do |page|
    width, height = page.size
    half_width = width / 2
    context.save do
      context.rectangle(0, 0, half_width, height)
      context.clip
      page.render(context)
      context.show_page
    end

    context.save do
      context.translate(-half_width, 0)
      context.rectangle(half_width, 0, half_width, height)
      context.clip
      page.render(context)
      context.show_page
    end
  end
end