6

I have a pdf

and I'd like to convert it to a series of images, numbered 0001.jpg - XXXX.jpg ?

I found this so far

sips -s format png your_pdf_file.pdf --out your_png_file.png

but it seems to only to the first page of the pdf, and the quality of the outputted png seems not so good. Any suggestions?

2

4 Answers 4

2

Alternatively you can use ghostscript which is on every mac preinstalled (and what is what imagemagic uses under the hood)

gs -dSAFER -dQUIET -dNOPLATFONTS -dNOPAUSE -dBATCH \ 
  # When converting multiple-page PDFs you should add "%d" to the filename-string 
  # which will be replaced with the sequence-number of the file
  -sOutputFile="outputFileName" \ 
  # Create a PNG-File with alpha-transparency
  -sDEVICE=pngalpha
  # resolution in DPI
  -r72 \ 
  # Use high grade text antialiasing. Can be 0, 1, 2 or 4
  -dTextAlphaBits=4 \
  # Use high grade graphics anti-aliasing. Can be 0, 1, 2 or 4
  -dGraphicsAlphaBits=4 \
  # If you are converting a CMYK-PFD to RGB-color you should use CIE-Color
  -dUseCIEColor \
  # use the PDFs Trim-Box to define the final image
  -dUseTrimBox \
  # start converting on the first side of the PDF
  -dFirstPage=1 \
  # convert only until the first page of the PDF
  -dLastPage=1 \
  # Path to the file you want to convert
  InputFile.pdf

For more information on how to use the CLI-Parameters, have a look at https://www.ghostscript.com/doc/9.27/Use.htm

4
  • thank you so much. I get a FC_WEIGHT didn't match (?) Aug 9, 2019 at 17:01
  • From what I read at forum.manjaro.org/t/ghostscript-problem-april-2019/81385/4 it looks as though your PDF file uses a font that is neither embedded in the PDF nor is available on the font path. so the PDF file is faulty. To fix the problem you can add the path to the location of the font files using -sFONTPATH=/path/to/fontfilesfolder Aug 9, 2019 at 20:08
  • To find out which font that might be have a look at the font-info in your PDF and watch outnfor fonts not marked as "embedded" or "partially embedded" Aug 9, 2019 at 20:09
  • 1
    I just realized that you could possibly remove the -dNOPLATFONTS to solve the FC_WEIGHT issue. Aug 14, 2019 at 20:53
4

ImageMagick

The easiest way is to use ImageMagick. Install ImageMagick if you haven't already

brew install imagemagick --with-fontconfig --with-ghostscript --with-openjpeg --with-webp 

Convert the PDF document into a series of images

convert -quality 100 -density 200 -colorspace sRGB "The_Artificial_Intelligence_Crush_2018.pdf" -flatten output-%02d.jpg

More details on producing sharp, high-quality image using ImageMagick.

Ghostscript

ImageMagick uses Ghostscript in the background to perform the PDF conversion. So why not use Ghostscript directly?

gs -dNOPAUSE -sDEVICE=jpeg -r200 -dJPEGQ=100 -sOutputFile=document-%02d.jpg "The_Artificial_Intelligence_Crush_2018.pdf" -dBATCH

Reference

Convert PDF to image with high resolution How to convert multi-page PDF to images in command line How to convert a PDF document into Powerpoint slides

1

You can use convert ( ImageMagick ) to parse PDF files.

PNG output

Convert test.pdf to numbered PNG images:

convert -density 150 -trim test.pdf page%d.png

JPEG output

Convert test.pdf to numered JPEG images:

convert -density 150 -trim test.pdf -quality 100 -flatten -sharpen 0x1.0 page%d.jpg

As per @JBWhitmore's explanation from his great answer):

convert                  \
   -verbose              \
   -density 150          \
   -trim                 \
    <your-PDF-file>.pdf  \
   -quality 100          \
   -flatten              \
   -sharpen 0x1.0        \
    page%d.{png,pdf}

It results in the left image. Compare this to the result of my original command (the image on the right):

  

(To really see and appreciate the differences between the two, right-click on each and select "Open Image in New Tab...".)

Also keep the following facts in mind:

  • The worse, blurry image on the right has a file size of 1.941.702 Bytes (1.85 MByte). Its resolution is 3060x3960 pixels, using 16-bit RGB color space.
  • The better, sharp image on the left has a file size of 337.879 Bytes (330 kByte). Its resolution is 758x996 pixels, using 8-bit Gray color space.

So, no need to resize; add the -density flag. The density value 150 is weird -- trying a range of values results in a worse looking image in both directions!

4
  • can you explain what exactly I am supposed to write in the last line, where you've written <0001-XXX page numbers>.png ? I've tried output_%d.png but this didn't work. Aug 9, 2019 at 17:17
  • use page-%d.png to number images
    – Yasen
    Aug 9, 2019 at 17:52
  • 1
    Would be nice if you quote the answer stackoverflow.com/a/6605085/399759
    – hanxue
    Aug 10, 2019 at 16:18
  • @hanxue thx added link
    – Yasen
    Aug 11, 2019 at 4:32
-1

just rename sample.pdf to sample.zip and then unzip sample.zip - it will create a folder with the images

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.