Skip to content

Instantly share code, notes, and snippets.

@renier
Last active June 2, 2020 18:35
Show Gist options
  • Save renier/27740b2f2c75c4cbf8fa711ba1617c10 to your computer and use it in GitHub Desktop.
Save renier/27740b2f2c75c4cbf8fa711ba1617c10 to your computer and use it in GitHub Desktop.
Breaking an image into multiple pages

I sometimes use Awesome Screenshot Plus to print a web page. It comes in handy as not all web pages are optimized for printing. This can result in a very long image if the content is multiple pages long, which is also not great for printint it out, since everything will come out as a single page due to scaling (on OSX).

There are probably other ways to solve this, but I've found ImageMagick to work very well in these cases. You'll need to know how many pages you need. For that, you'll need to know the dimensions of the source image. You can also gather this info with ImageMagick.

$ convert doc.jpg -print "Size: %wx%h\n" /dev/null

Now that you know the pixel width and height, use some math to figure out how many pages you'll need. The following is an example for letter size pages (8.5 x 11 inches):

h/((11/8.5)*w)

Round up the number and that's the number of pages you'll need. Now we can use ImageMagick to create a paged pdf from the image:

convert doc.png -crop 1xnumber_of_pages_here@ +repage doc.pdf

Open up doc.pdf to check it.

I'll probably script this up one of these days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment