Skip to content

Instantly share code, notes, and snippets.

@fukajun
Created May 28, 2019 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fukajun/85472085c3e7f0f8b27a9c718449d144 to your computer and use it in GitHub Desktop.
Save fukajun/85472085c3e7f0f8b27a9c718449d144 to your computer and use it in GitHub Desktop.
Convert pdf size
require 'combine_pdf'
#require 'prawn'
def crop(page, x1, y1, x2, y2)
size = page.page_size
x1_new = size[0] - x1
y1_new = size[1] - y1
x2_new = size[2] + x2
y2_new = size[3] + y2
page.mediabox = [x1_new, y1_new, x2_new, y2_new]
page.cropbox = [x1_new, y1_new, x2_new, y2_new]
end
pdf2 = CombinePDF.load('./sample.pdf')
puts pdf2.pages[0].page_size
pdf2.pages.each do |page|
crop(page, 40, 40, 40, 40)
end
puts pdf2.pages[0].page_size
pdf2.save('converted2.pdf')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment