Reputation: 33
I would like to take a rectangular packaging label in PDF format, consisting of mostly formatted text, a few images and borders, and take the four main elements of this PDF (marked each in red in the image) and reposition them into a new PDF so that it can be printed on my 62mm label printer. There are several free web services available who can do this for more common packaging labels, but not the ones that I need, so I would like to code this myself (preferrably in Perl). Any pointers on how to approach this task would be very helpful - thanks!
Upvotes: 1
Views: 187
Reputation: 16662
A simple solution is to:
For example, for the image and cropping from the sample image provided in the question, a shell script using graphicsmagick might be:
#!/bin/sh
# graphicsmagick uses the file extension to decide file type
in=in.pdf # original (pdf expected)
tmp=tmp.png # temporary image (png is lossless)
out=out.pdf # output file (or use png for image)
# convert original pdf to bitmap image
# density is dpi - larger gives higher resolution
gm convert -density 300 $in $tmp
# find size of resulting image, for use below
file $tmp
# width/height of bounding box temporary image will fit into
# probably needs to be much bigger than this
# this script just uses the resolution of the question's image
w=854
h=603
# important x/y distances in the image
# change to match width/height given above
x=284
y1=148
y2=368
# crop out the individual pieces, rotate so long side is vertical,
# then combine (append) into a single output file
# format is: [width] "x" [height] "+" [x offset] "+" [y offset]
gm convert \
$tmp -crop $((w-x))x${y1}+${x}+0 -rotate 90 \
$tmp -crop $((w-x))x$((y2-y1))+${x}+${y1} -rotate 90 \
$tmp -crop $((w-x))x$((h-y2))+${x}+${y2} -rotate 90 \
$tmp -crop ${x}x${h}+0+0 \
-append $out
# clean up
rm $tmp
Note that -append
seems to combine images in an unexpected order:
gm convert $in1 $in2 $in3 $in4 -append $out
# $out contains a vertical stack of $in4, $in1, $in2, $in3
# (ie. $in4 appears at the top rather than at the bottom)
Upvotes: 2