- 1. Converting PDF pages to low-quality images
- 1.1 Fixing permission issue
- 1.2 Adding more numbers to the output filenames for easier sorting
- 2. Converting PDF pages to high-quality images
- 3. Converting a particular PDF page to a single image
- 4. Converting a range of PDF pages to images
- 5. Converting all PDF pages to a single image
imagemagick provides many useful utilities to manipulate images. We can now use it on Termux conveniently.
Check whether you have already installed imagemagick on Termux:
magick -version
# the output will be something like:
Version: ImageMagick 7.1.0-3 Q16 aarch64 2021-07-17 https://imagemagick.org Copyright: (C) 1999-2021 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
...
If you haven’t installed it yet or want to update it, simply run:
pkg install imagemagick
1. Converting PDF pages to low-quality images
Now try converting all PDF pages to images. For now, just use the simple command below to test first.
The default output images will be of low quality. If it is working, we will add more options for higher resolution later.
convert a.pdf demo.jpg
If the PDF has more than 1 page, the output files will be named as demo-1.jpg, demo-2.jpg, and so on… for all pages of the PDF file.
1.1 Fixing permission issue
On Termux, if you get errors related to permission like:
...
no images defined 'demo.jpg' @ error
...
Then you may try this solution: make sure you have an updated ghostscript >=9.24. (Read)
# my current version
gs --version
9.53.2
On my device, I simply installed the latest ghostscript version and it solved the error. I did not need to do any further configuration. If the ghostscript version is < 9.24 or you haven’t installed it yet, you can update or install it with this command:
pkg install ghostscript
Re-run the convert command, if it still doesn’t work, you may need to google more for your particular error. See this useful Stackoverflow post.
1.2 Adding more numbers to the output filenames for easier sorting
Sometimes, when sorting files by filename in ascending order, some File Manager apps on Android will wrongly list demo-20.jpg before demo-3.jpg.
To avoid this issue, we need to add more leading 0 or use the same digit length for all numbers.
For 3 digit length, add this format pattern %03d to the output filename in the command.
convert a.pdf demo_%03d.jpg
It will save files with demo_001.jpg, demo_002.jpg and so on.
Depending on the total page number of the PDF, we can increase the number in the format pattern, like %04d and alike.
2. Converting PDF pages to high-quality images
We will add more optional parameters for a better quality of the output images:
convert -density 200 -antialias a.pdf -quality 100 hires_%03d.jpg
In the above options:
-density geometry horizontal and vertical density of the image
-antialias remove pixel-aliasing
-quality value JPEG/MIFF/PNG compression level
hires_%03d.jpg : output filename pattern with 3 digits appended
3. Converting a particular PDF page to a single image
It uses a zero-index style, similar to a Javascript array or Python list. So a.pdf[9] means the page number 10 of a.pdf
convert -density 200 -antialias a.pdf[9] -quality 100 hires_%03d.jpg
The above command will produce output filename hires_009.jpg, but the content is of page 10 in the PDF file.
4. Converting a range of PDF pages to images
Use a.pdf[x-y] for selecting a range of pages.
convert -density 200 -antialias a.pdf[5-9] -quality 100 hires_%03d.jpg
The above command will convert PDF page 6-10, output files will be named hires_005.jpg, …, hires_009.jpg.
5. Converting all PDF pages to a single image
Simply add -append option to the above command.
-append append an image sequence
So the command will be:
convert -density 200 -antialias a.pdf -append -quality 100 hires_%03d.jpg
- References and further reading:
- https://www.binarytides.com/convert-pdf-image-imagemagick-commandline/
- +
- https://stackoverflow.com/questions/52998331/imagemagick-security-policy-pdf-blocking-conversion
- +
- https://aleksandarjakovljevic.com/convert-pdf-images-using-imagemagick/
May it be helpful!