April 10, 2013 Adem Bilican 8 comments

How to convert PDF to PNG from the command line on a Mac

How to convert PDF to PNG from the command line on a Mac
    Instead of opening your PDF file with the Preview app (or other PDF reader) and export it to PNG, you can easily use the command line.
To do so, you need to use the sips (scriptable image processing system) command installed by default in Mac OS X. Open your terminal and run:

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

The script also works for the jpeg format the same way.
Voilà 😉
UPDATE: this solution works for the first page of the PDF only (thanks Joe Papa for the feedback).


source
 : straylink
A.P. Lawrence

Source: New feed

8 thoughts on “How to convert PDF to PNG from the command line on a Mac

  1. You can also you the commandline tool convert.

    Example
    convert -density 300 -quality 95 your_pdf_file.pdf your_png_file.png

    To select the specified page in the pdf file, say the second page, use
    convert -density 300 -quality 95 your_pdf_file.pdf[1] your_png_file.png

    Note that the page counter starts from 0. That is, the first page is 0.

    1. I tried the sips method and got a Segmentation fault: 11.
      And I tried the convert method and got a bash error, “convert: command not found”.
      I’m new at this. I’ll keep plugging away at this. Am I correct though that convert is actually using ImageMagick under the covers but that sips is not? Pretty sure that in order to convert pdf with text to png or preferably jpg and keep it readable, there are other parameters that I need to tweak in ImageMagick as well, but I have found what those are elsewhere and will try it if I decide to push this along further.

      1. You are right about “convert”, it is a tool from ImageMagick. Maybe you can try “magick convert” (https://imagemagick.org/script/convert.php) or check that ImageMagick is properly installed.
        The “sips” utility should be installed by default on macOS, I don’t understand why you are getting a Segmentation fault error.

  2. Very informative! Thanks for showing clearly how to convert PDF to PNG, I used to do that with Acethinker PDF Converter Lite, free and works pretty well. Share it here as an alternative solution.

  3. If you have a directory with a set of 1 page pdf files to be converted, this will generate a SIPS command for each entry.
    Then just put the list into another shell script, preceeded by
    #!/bin/bash

    And you can convert everything to a .png in a couple of easy steps.

    for entry in “/Users/macuser1/pdf-files”/*.pdf
    do
    firstString=$entry
    secondString=”png”
    #echo “${firstString/pdf/$secondString}”
    targetFileName=${firstString/pdf/$secondString}
    cmdString=”sips -s format png \””$entry”\” –out “\”$targetFileName\”
    echo $cmdString
    done

Leave a Reply

Your email address will not be published. Required fields are marked *