Thread: Cropping PDFs
View Single Post
Old 08-14-2006, 12:07 PM   #9
harryo
Enthusiast
harryo began at the beginning.
 
Posts: 34
Karma: 10
Join Date: Aug 2006
Device: Palm Tungsten TX / Iliad
Quote:
Originally Posted by ali
What did you do, and how?
I don't know how ali did it, but I used this piece of Ruby code
Code:
cropLeft   = ARGV[0].to_i
cropRight  = ARGV[1].to_i
cropTop    = ARGV[2].to_i
cropBottom = ARGV[3].to_i

originalPath = ARGV[4]
croppedPath  = originalPath.sub(/\.pdf$/, " (cropped).pdf")

File.open(originalPath, "rb") do |input|
  pdf = input.read

  pdf.gsub!(/\/MediaBox \[\s*(\d*)\s*(\d*)\s*(\d*)\s*(\d*)\s*\]/) do
      n1 = $1.to_i + cropLeft
      n2 = $2.to_i + cropTop
      n3 = $3.to_i - cropRight
      n4 = $4.to_i - cropBottom

      "/MediaBox [#{n1} #{n2} #{n3} #{n4}]"
  end

  File.open(croppedPath, "wb") { |output| output.write pdf }
end
I don't have a reference for the /MediaBox parameters, so I did it by trial and error. In reality, I didn't take much care, just ran the program and fiddled the four values until I got what looked good for each document.

I just wanted to get something working. I'll clean it up later, once I have the details worked out a bit better.
harryo is offline   Reply With Quote