Previously, I have talked about how to convert PDF to images using pdftoppm
here.
In this post, I want to share how to accomplish this task with
Imagemagick.
Imagemagick provides the convert
tool that can be used to do various
complicated image processing tasks.
Convert All Pages of PDF File to Images#
Use convert
to convert PDF pages to images with the following command:
convert -density 150 presentation.pdf -quality 90 output-%3d.jpg
In the above command, we convert all the pages of the PDF files to images.
-density
is used to specify the DPI of the output images. For PDF files, it
must be the first option since PDF files has no notion of DPI. This option must
be used first so that convert
know how to sample the PDF pages. -quality
specify the quality for the generated images. %3d
is used to specify the
format for generated image names. The generated images will be named
output-001.jpg
, output-002.jpg
……
Convert Single Page of PDF File to Image#
To convert a single page of PDF to image, use the following command:
convert -density 150 presentation.pdf[0] -quality 90 test.jpg
The number inside the bracket is used to select a page. Note that the page
index starts at 0 instead of 1.
To resize the converted image, you can supply the -resize
option:
convert -density 150 presentation.pdf[0] -quality 90 -resize 50% test.jpg
Convert A Range of PDF Pages to Images#
You can also specify a range of pages with the following command:
# convert from page 0 to page 5
convert -density 150 presentation.pdf[0-5] -quality 90 test.jpg
References#
- https://aleksandarjakovljevic.com/convert-pdf-images-using-imagemagick/
- https://askubuntu.com/questions/50170/how-to-convert-pdf-to-image
- https://stackoverflow.com/questions/6605006/convert-pdf-to-image-with-high-resolution
- Convert multi-page PDFs to multiple images.
- Imagemagick: command line options.
- Imagemagick: command line processing.
There is a quick and convenient way to convert PDF to one or more images. Command line tool ImageMagick does that (and a lot more). You can convert an entire PDF document to a single image, or, if you like, there is an option to output pages as a series of enumerated image files.
Note: It is advisable to use density, antialias and quality options to get the conversion quality that suits your needs. In following examples we are going to use -density 150, -quality 100 and -antialias options. ImageMagick is intuitive and it will guess the output format based on output filename extension.
Examples:
Convert an entire PDF to a single image:
convert -density 150 -antialias "input_file_name.pdf" -append -resize 1024x -quality 100 "output_file_name.png"
Key parameter here is -append which actually makes a difference if PDF is converted to a single image or to a series of images.
Convert a PDF document to a series of enumerated images:
convert -density 150 -antialias "input_file_name.pdf" -resize 1024x -quality 100 "output_file_name.png"
As a result of this command a series of image files named output_file_name-0.png, output_file_name-1.png, output_file_name-2.png …. etc, will be created in the working directory. If you have more then 10 pages, it would come in handy to have those enumerated file names with multiple digits, for the convenience of easy sorting. If you include a C-style integer format string, for example if you add %03d to the end of your output file name, you will get output_file_name-001.png, output_file_name-002.png, output_file_name-003.png, etc.
convert -density 150 -antialias "input_file_name.pdf" -resize 1024x -quality 100 "output_file_name-%03d.png"
Convert only specified pages to images:
convert "input_file_name.pdf[1]" "output_file_name.png"
This will actually convert page 2 of PDF to PNG, since numbering starts with 0. To convert range of pages, from i to j, use this command:
convert "input_file_name.pdf[i-j]" "output_file_name.png"
Using with PHP
Since this is a command line utility, you can access it from PHP using some of the functions to execute external commands. For example, exec, system, passthru, etc.
My name is Aleksandar Jakovljević and I am a web developer from Belgrade, Serbia.
I was born in Smederevo, in 1980. I started focusing on web development since late 2003. I am mostly focused on open source web technologies.
I specialised in PHP, focusing on Symfony framework, with skills in Drupal and WordPress, too.
I worked more than 7 years for art_net studio and 10 years for Computer Rock (former Spoiled Milk). Now I am working on developing industry leading subscription management system as a part of Plenigo team since August 2021.
I love the web and everything related to it. The possibility to publish your ideas quickly and make them available to the entire world is what made me decide to pursue a career in web development.
Follow on:
Twitter | GitHub | LinkedIn | Facebook | Instagram
Hire me on Upwork
- pdf-conversion,
- imagemagick
ImageMagick and PDF Conversion #
Converting PDFs to images is a common task in various applications. ImageMagick, a popular image processing library, provides an efficient way to achieve this.
Converting PDF to Images #
To convert a PDF to images, you can use the convert
command from ImageMagick. Here’s an example:
convert -density 300 input.pdf output.jpg
This will convert the PDF to a JPEG image with a density of 300 DPI.
Converting PDF to Specific Formats #
To convert a PDF to a specific format, you can use the -format
option. For example:
convert -format PNG input.pdf output.png
This will convert the PDF to a PNG image.
Converting PDF to Multi-Page Images #
To convert a multi-page PDF to a set of images, you can use the convert
command with the -page
option. Here’s an example:
convert -page 1x1 input.pdf output-%d.jpg
This will convert each page of the PDF to a separate JPEG image, named output-1.jpg
, output-2.jpg
, etc.
Converting PDF to Image Sequences #
To convert a PDF to an image sequence, you can use the convert
command with the -delay
option. Here’s an example:
convert -delay 10 input.pdf output-%d.png
This will convert the PDF to a set of PNG images, with a delay of 10 milliseconds between each image.
Converting PDF to Image with Specific Resolution #
To convert a PDF to an image with a specific resolution, you can use the -resolution
option. Here’s an example:
convert -resolution 72x72 input.pdf output.png
This will convert the PDF to a PNG image with a resolution of 72×72 DPI.
These are just a few examples of how you can use ImageMagick to convert PDFs to images. The possibilities are endless, and the best way to learn more is to experiment with different options and parameters.
- Previous: ImageMagick and image effects: creating unique effects with ImageMagick
- Next: ImageMagick convert command and its options
Как в Windows сделать из PDF картинку через imagemagick
Время создания: 12.12.2011 13:26
Текстовые метки: PDF, картинка, страница, установка, imagemagick, Ghost Script
Раздел: Компьютер — Windows — Графика
Запись: xintrea/mytetra_syncro/master/base/13236819860za1a8vl14/text.html на raw.github.com
|
Вначале нужно установить Ghost Script. Нужно с сайта ghostscript.com взять инсталлятор (может называться например gs904w32.exe) и проинсталлировать его. Затем надо скопировать файлы из каталога /bin, который находится по пути инсталляции, в каталог c:\windows\system32.
Затем надо установить imagemagick. Его берут с сайта www.imagemagick.org.
Далее вызывается команда, входящая в пакет ImageMagick:
convert file.pdf[n] page.png
или
convert file.pdf[n-m] page.png
где n и m — номера страниц, счет страниц с нуля.
liys_0 wrote:In this case, can we convert one page portion by portion to use less resources? : )
If you know the PDF is just a single page document, you could start by using ImageMagick to determine the output dimensions of the image using something like this from the command line…
Code: Select all
convert -density 200 largefile.pdf info:
That will print what the dimensions will be after converting it to a 200dpi image, and some other information.
Then when you know the size in pixels, you can read just a part of the PDF into IM’s «convert» by putting the geometry of the requested portion in square brackets at the end of the file name.
For example, I checked the dimensions of my «largefile.pdf» using the command above and found it will make a 2400×2400 pixel PNG. Then if I want to get just the top left quarter of the PDF I would use a command like this…
Code: Select all
convert -density 200 largefile.pdf[1200x1200+0+0] -flatten part1A.png
With that command IM will take just a 1200×1200 piece starting at the first pixel in the upper left corner, pixel «+0+0». I can get all four quarters of the PDF into separate PNG files with a series of commands like this…
Code: Select all
convert -density 200 largefile.pdf[1200x1200+0+0] -flatten part1A.png
convert -density 200 largefile.pdf[1200x1200+1200+0] -flatten part1B.png
convert -density 200 largefile.pdf[1200x1200+0+1200] -flatten part2A.png
convert -density 200 largefile.pdf[1200x1200+1200+1200] -flatten part2B.png
To reassemble those four PNG images into a single image later I could use a command like this…
Code: Select all
convert ( part1A.png part1B.png +append ) ( part2A.png part2B.png +append ) -append largefile.png
Or maybe tile them back together with a properly constructed IM «montage» command.
To make the dis-assembly automated so it can use varying sizes of input files would require a slightly tricky BAT file with some nested «for» loops to get the image size into some variables, break it into parts by calculating those variables, and creating unique meaningful file names for all the output files. But that’s a Windows programming issue, not ImageMagick.
I don’t know how to run a «convert» command on a particular page of a multi-page PDF and have it use just a segment of the page, since both those processes use square brackets at the end of the file names to specify the details. I tried using two sets of square brackets and had no success. Someone else here might know a way to make that happen. It may not be possible.