Converion of pdf page to fixed size images - imagemagick

I want to convert pdf pages to images of same size(aspect ratio maintained) irrespective of the pdf quality. Say A4 size. I am using convert from Imagemagick for this but struggling to get the fixed size of images for different pdfs of different quality.
Can anyone help me?

Something like:
convert -density 288 document.pdf -resize 2480x3508 result.png
The density must come before the PDF to ensure the rasterisation is done at sufficient quality/resolution.
If you want that centred on a piece of A4 and padded with white to fill the page:
convert -density 288 document.pdf -resize 2480x3508 -background white -gravity center -extent 2480x3508 result.png

Related

Imagemagick conversion process

I was trying to resize a pdf file to 8.5x11 with ImageMagick and in doing so, I am loosing the quality of my pdf file.
I was using convert source.pdf -extent 612x792 destination.pdf command.
Can someone please help me with this one?
Imagemagick is rasterizing the PDF, then scaling it, then producing an output PDF of differing dimensions, which contains raster data only.
Scale a PDF to fit directly like this:
cpdf -scale-to-fit "612 792" in.pdf -o out.pdf
This keeps the PDF's original vector artwork and fonts intact.
Imagemagick uses Ghostscript and will rasterize your PDF. It will not maintain its vectors. If you want higher quality, use a higher -density to rasterize and then resize back to whatever you want. For example, I use a density 4x the default of 72 or 288 and then resize by 25%=1/4 to get back to the nominal size.
convert -density 288 -units pixelsperinch image.pdf -resize 25% result.pdf
You can then include your -density 72 -gravity XX -extent 612x792 before the output.
Or you can compute do -density 288 etc and then just resize to 612x792.
I've tried this and it worked.
convert -density 300 -define pdf:fit-page=Letter -gravity Center source.pdf output.pdf

How to convert PNG to pdf and center the image?

I'm using Magick's convert tool to convert and combine all my images into PDF.
magick convert *.png -page a4 out.pdf
The resolution of the images are 1079 x 1397
The images in the generated PDF file are off-centered. There's some white space on the top of each page.
Here's a screenshot : https://i.imgur.com/EpvxZNU.png
I don't want to "fill" the entire page with my image. I want to simply "fit" the image the way it has in the screenshot above, BUT be centered (have equal whitespace on the top and bottom)
I don't believe that you can center with -page in ImageMagick 7 unless you do inline computations using the actual desired A4 size in pixels (592x842) via distort and its viewport setting. See page sizes in pixels at https://imagemagick.org/script/command-line-options.php#page
In ImageMagick 7, you should use magick and not magick convert. The latter will emulate ImageMagick 6. However there is a bug currently in ImageMagick 7. The following command works fine with convert in ImageMagick 6, but fails with magick in ImageMagick 7. But it works fine with magick convert in ImageMagick 7.
magick convert *.jpg -virtual-pixel white -set option:distort:viewport "592x842-%[fx:(592-u[t].w)/2]-%[fx:(842-u[t].h)/2]" -distort SRT 0 +repage result.pdf
Here is a simple example:
Input Images:
magick convert lena.jpg barn.jpg -virtual-pixel white -set option:distort:viewport "592x842-%[fx:(592-u[t].w)/2]-%[fx:(842-u[t].h)/2]" -distort SRT 0 +repage result.pdf
Result:

ImageMagick convert format, crop and resize

I have a large number of research pdf figures, and I need to preform the following actions in ImageMagick:
convert all pdf to png
crop png from left/top corner x:334/y:244; from right/bottom corner x:214/y:340;
original size 2100x2100, cropped size 1552x1552 pixels
resize cropped png to 240x240 pixels
Here is how it should be cropped for point 2, the pink area is what I want to have:
I was only be able to get 1st action done with my knowledge:
mogrify -format png -density 300 -flatten *.pdf
How can I do the 2nd and 3rd actions please? And do I need to run three separated commands or could they be combined into one command?
I do not know what exact order you need for mogrify as I do not use it. I also do not know why you need flatten Try:
mogrify -format png -density 300 -crop 1552x1552+344+244 +repage -resize 240x240 *.pdf

ImageMagick Crop Tall image into A4 PDF pages with same width dynamic height

how can we make a tall-dynamicheight-screenshot.png into onefile.pdf with multiple pages in A4 so it could be readable when its printed?
example like this :
into A4 pages like
so when we print it would be like
not like (unreadable)
This is maybe good enough for a crude first approach - it doesn't look for good places (white space) to divide the pages along though, so it slices through the image at the bottom of your first page:
convert article.jpg -resize 2480x -crop 2480x3508 +repage result.pdf
In imagemagick you can crop to multiple pages. But it depends upon whether you want to keep your original size or resize the width to correspond to A4. The A4 file dimensions are 595x842.
If you want to keep the original dimensions and create a multipage PDF, then you will get 12 pages at your original resolution
convert V0twr.jpg -crop 595x842 +repage onefile1.pdf
If you want to resize to the width of an A4 and then crop, then you will get 2 pages at reduced resolution.
convert V0twr.jpg -resize 595x -crop 595x842 +repage onefile2.pdf
You can choose some other in-between resolution for your resize, if you want as a compromise, say twice the width of A4 = 2*595 = 1180

ImageMagick: convert image to PDF with A4 page size and image fit to page

I want to convert different image formats (bmp,jpg,gif,png,tiff-incluging multipaged) into a PDF format with A4 page size and with images fit to page (resized if necessary). Image should be positioned at the center of the page and I'd like to define an offset.
I tried the code below but there is no offset at the top and the image quality is really poor.
convert png.png -gravity North -resize 500x500 -quality 100 -page a4x5x5 myout.pdf
Is there any way to do that?
Thanks in advance for any help,
Mariusz
If you want to keep the original resolution (lossless) you can try the following command:
convert png.png -background white -page a4 myoutput.pdf
Based on a comment posted before: https://stackoverflow.com/a/24573341/6747994
#m4tx This command only makes sense if the picture has a resolution above 500x800px, it does not zoom in, to avoid pixelated thumbnails.
You can convert to pdf using ImageMagick
convert png.png myout.pdf
but use pdfjam instead of ImageMagick to adjust the page size
pdfjam --paper a4paper --outfile myoutA4.pdf myout.pdf
pdfjam offers other options, which may fit your needs.
Found this somewhere on stackoverflow:
convert *.jpg -resize 1240x1753 \
-extent 1240x1753 -gravity center \
-units PixelsPerInch -density 150x150 multipage.pdf
Thanks to the ImageMagick support forum I was able to find a solution:
convert image.tif -resize 575x823^> -gravity center -background white -extent 595x842 image.pdf
If you get an error try:
convert image.tif -resize 595x842^\> -gravity center -background white -extent 595x842 image.pdf
You need to specify the density:
convert -density 80 -page a4 input_A.jpg input_B.jpg output.pdf
Otherwise, there can be an issue when printing with "actual size" instead of "fit". I have used a public printer where I could not choose "fit" and had to choose "actual size", in which case the printed page was only a part of the page which I wanted to print.

Resources