Page 1 of 1

Join two pdf with gravity and no margin between them

Posted: 2008-09-22T08:23:06-07:00
by neo007
I know that IM isn't the best method to do this, but currently I see no alternative to it. (everything I've seen is just adding the second pdf like the next page)

I need to simply join two pdf single-paged documents into another one, also with only one page. Both should be vertical-aligned bottom (South), there should be no spacing (margin, padding?) between them. The width of a new file should be: first.width + second.width, whilst the height: max(first.height, second.height)

Now I'm using:

Code: Select all

montage -geometry +0+0 -density 100x100 1.pdf 2.pdf out.pdf
But it makes my image 278mm heigh although the heightest of the two is 200mm.

Re: Join two pdf with gravity and no margin between them

Posted: 2008-09-22T10:52:28-07:00
by fmw42

Re: Join two pdf with gravity and no margin between them

Posted: 2008-09-23T03:16:43-07:00
by neo007
You've probably saved my life.

But still remains the gravity problem:

Code: Select all

convert 1.png 2.png  +append -gravity South 3.png
I thought the two images will be aligned to the bottom of the image.

--- edit

I've found that

Code: Select all

Only the text primitive is affected by the -gravity option.
But no solution.

-- edit
I've also found: (http://www.imagemagick.org/Usage/montage/#concatenate)

Code: Select all

When concatenating images of different sizes, the images are concatenated with 'top' vertical alignment, then 'left' horizontal row alignment.
And I want to change this to bottom

Re: Join two pdf with gravity and no margin between them

Posted: 2008-09-23T09:30:25-07:00
by fmw42
You have several options.

1) Pad the image with the smaller height with whatever background color you want so as the same size as the larger iimage, then append

You can pad by either 1) appending a solid color (or transparent) image to the top of the smaller image or 2) composite it in a solid color (or transparent) image the same size as the larger image or 3) using -extent (for -extent and gravity you need 6.3.2)

see
http://www.imagemagick.org/Usage/crop/#extent

2) Use convert ... -gravity ... -compose -composite

convert -size WxH xc:backgroundcolor leftimage -gravity southwest -compose over -composite \
rightimage -gravity southeast -composite outputimage

where H is the height of the larger image and W is the sum of the widths of the two images


Here is an example of the latter ( I use IM to compute the dimensions of the two images and then compute the required dimension for the composited image)

image1="monet3.jpg"
image2="cyclops.jpg"
w1=`convert $image1 -format "%w" info:`
h1=`convert $image1 -format "%h" info:`
w2=`convert $image2 -format "%w" info:`
h2=`convert $image2 -format "%h" info:`
ww=`convert xc: -format "%[fx: $w1 + $w2]" info:`
hh=`convert xc: -format "%[fx: max($h1,$h2)]" info:`
convert -size ${ww}x${hh} xc:black $image1 -gravity southwest -composite \
$image2 -gravity southeast -composite merge1.jpg

monet3.jpg
Image

cyclops.jpg
Image

merge1.jpg (resulting composite)
Image

Re: Join two pdf with gravity and no margin between them

Posted: 2008-09-23T21:37:25-07:00
by anthony
A quick way to append with bottom alignment is to do
-flip +append -flip

it is center append that is trickier and requires seom very fancy bit of image handling. See IM examples, Layers, Append.

Re: Join two pdf with gravity and no margin between them

Posted: 2008-09-24T12:51:28-07:00
by neo007
Great, thanks a lot, guys.

I thought especially about the last solution, but with rotation. But it'd require a lot of manipulation. Double -flip is much easier and just works.

You saved hundreds of hours of my life. Thanks.

Re: Join two pdf with gravity and no margin between them

Posted: 2008-09-28T08:02:17-07:00
by neo007
After being quite satisfied I have to admit that the output quality is quite bad. (I'm aware of the -density option) I believe that you know an alternative to do such a thing (can be without bottom vertical alignment) with two pdf.

Re: Join two pdf with gravity and no margin between them

Posted: 2008-09-28T13:39:51-07:00
by fmw42
you can try setting the jpg quality higher using -quality

see:
http://www.imagemagick.org/script/comma ... hp#quality
http://www.imagemagick.org/Usage/formats/#jpg


Also you can try using -density to make the image much bigger, then resize it back to a smaller size using -resize

Some of this may depend upon the quality of the image buried in your pdf files, if that is the case.

Re: Join two pdf with gravity and no margin between them

Posted: 2008-09-28T14:29:51-07:00
by neo007
It doesn't help.

However, maybe I'll describe my problem a little longer.

Generally speaking, I have a couple of CAD drawings (a lot of lines, different angles, paths and arcs, text; fixed with of each drawing for example 500x300mm). I print it via pdf printer in order to get pdf image (a program I'm using has it's own format)

I need to join two pdfs because of tables. I have to add a table (from openoffice) to that images. I use a bash script which reads the name of each drawing and generates appropriate table. Then I export that (f)odt to pdf. (via odt2pdf) And finally I join that two pdfs. Let's assume that table is created by someone else and it's always in (f)odt.

The only solution I see so far is to convert each pdf (because some of my drawings are svg) to svg then run script which joins that to svg - some xml operations - then export to pdf.

Re: Join two pdf with gravity and no margin between them

Posted: 2008-09-28T14:40:06-07:00
by fmw42
neo007 wrote:It doesn't help.

However, maybe I'll describe my problem a little longer.

Generally speaking, I have a couple of CAD drawings (a lot of lines, different angles, paths and arcs, text; fixed with of each drawing for example 500x300mm). I print it via pdf printer in order to get pdf image (a program I'm using has it's own format)

I need to join two pdfs because of tables. I have to add a table (from openoffice) to that images. I use a bash script which reads the name of each drawing and generates appropriate table. Then I export that (f)odt to pdf. (via odt2pdf) And finally I join that two pdfs. Let's assume that table is created by someone else and it's always in (f)odt.

The only solution I see so far is to convert each pdf (because some of my drawings are svg) to svg then run script which joins that to svg - some xml operations - then export to pdf.
I don't follow much of this, but perhaps your issue in your generating your pdf files and conversion between pdf and svg. If you are using IM to convert between pdf and svg, that may not be the best. See
http://www.imagemagick.org/Usage/formats/#vector


Also perhaps you should provide an example of your pdfs that you want to append?

Re: Join two pdf with gravity and no margin between them

Posted: 2008-09-28T15:22:56-07:00
by neo007
Yeah, quite complicated.

An example pdf you can find on my website - here.

Just try to join that pdf with its copy and observe the quality.

And, yes, IM is not appropriate tool to do this but I can't find anything better.

Re: Join two pdf with gravity and no margin between them

Posted: 2008-09-28T16:15:52-07:00
by anthony
neo007 wrote:Generally speaking, I have a couple of CAD drawings (a lot of lines, different angles, paths and arcs, text; fixed with of each drawing for example 500x300mm). I print it via pdf printer in order to get pdf image (a program I'm using has it's own format)
As Fred mentioned, I do not believe IM is the best method for this. If you can generate SVG of the images, then I suggest that you add the tables using a SVG editor (lots available, many open source and free). Alturnativally you could try the Abobe Acrobat program which is a full PDF editor. And finally I do like some vector image convertor in the link about Vector image formats

Basically you should try to keep your vector image as a vector image and even add you table as a vector image.

If worse comes to worse, print your pages twice, once with the drawing and again with the table.

PS: I once needed to modify a Postscript document. I used a object editor and loaded the postscript as one element on the page, then I draw white boxes to erase what I didn't want and added text and other bits to add what I did. When I output it as postscript again, I had a postscript modified postscript. and it was exactly what I wanted. A GIF image of the result is at http://www.cit.gu.edu.au/~anthony/kites ... twisty.gif

Re: Join two pdf with gravity and no margin between them

Posted: 2008-09-29T01:42:45-07:00
by neo007
I'll try to do it all using svg since I can modify svg using bash and command line. If I succeed I'll tell you how I did it :-)

Re: Join two pdf with gravity and no margin between them

Posted: 2008-09-29T05:59:36-07:00
by neo007
So I've done it somehow but performance... terrible. Luckily most of my images are svg.

I have, however, a question. In my script:

Code: Select all

# inkscape default resolution is 90 dpi so use it

if [ "$3" ]; then

# convert image to png in order to get their correct width
inkscape --export-dpi=90 --export-png=tmp.png "$1" # we could get it here
w1=`convert tmp.png -format "%w" info:`
h1=`convert tmp.png -format "%h" info:`

# delete tmp image
rm tmp.png

inkscape --export-dpi=90 --export-png=tmp.png "$2"
w2=`convert tmp.png -format "%w" info:`
h2=`convert tmp.png -format "%h" info:`

# delete tmp image
rm tmp.png

# count necessary sizes (http://www.imagemagick.org/discourse-server/viewtopic.php?p=39767#p39767)
ww=`convert xc: -format "%[fx: $w1 + $h2]" info:`
hh=`convert xc: -format "%[fx: max($h1,$w2)]" info:`

# echo everything
echo "<svg  width='$ww' height='$hh' version='1.1' xmlns='http://www.w3.org/2000/svg'>" > "$3"working

# echo first image
echo `cat "$1" >> "$3"working`

# rotate and move
echo "<g transform='translate($w1, $hh) rotate(-90)'>" >> "$3"working

# echo second
echo `cat "$2" >> "$3"working`

# close image
echo '</g></svg>' >> "$3"working

# delete <?xml ?>
sed -e 's/<?xml.*?>//g' "$3"working > "$3"

# delete additional file
rm "$3"working

else
	echo Usage fistSvg secondSvg outputSvg
fi
I stupidly convert both images to png in order to get their sizes in 90 dpi (used by Inkscape). Is there a fast method to get those values without having to convert it? Some svgs are 35MB so it takes very long time. And, what is more, when outputting to png through Inkscape - it gives different results although used with the same dpi (inkscape --export-png=test.png --export-dpi=90 test.svg vs convert -density 90 tesst.svg -format "%w" info:)

Re: Join two pdf with gravity and no margin between them

Posted: 2008-10-02T14:07:47-07:00
by neo007
I found something like iText but currently can't check how it works. I'll tell you whether I managed to do it or not.