如何通过 Mac 上的终端将 PDF 转换为一系列图像(JPG 或 PNG)?

R. *_*sch 11 terminal pdf command-line image-conversion macos

我有一个pdf

我想将其转换为一系列图像,编号为0001.jpg- XXXX.jpg

到目前为止我发现了这个

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

但它似乎只针对pdf的第一页,并且输出的png的质量似乎不太好。有什么建议么?

han*_*xue 11

图像魔术师

最简单的方法是使用 ImageMagick。如果尚未安装 ImageMagick,请安装

brew install imagemagick --with-fontconfig --with-ghostscript --with-openjpeg --with-webp 
Run Code Online (Sandbox Code Playgroud)

将 PDF 文档转换为一系列图像

convert -quality 100 -density 200 -colorspace sRGB "The_Artificial_Intelligence_Crush_2018.pdf" -flatten output-%02d.jpg
Run Code Online (Sandbox Code Playgroud)

有关使用 ImageMagick 生成清晰、高质量图像的更多详细信息

鬼脚本

ImageMagick 在后台使用 Ghostscript 来执行 PDF 转换。那么为什么不直接使用 Ghostscript 呢?

gs -dNOPAUSE -sDEVICE=jpeg -r200 -dJPEGQ=100 -sOutputFile=document-%02d.jpg "The_Artificial_Intelligence_Crush_2018.pdf" -dBATCH
Run Code Online (Sandbox Code Playgroud)

参考

将 PDF 转换为高分辨率图像 如何在命令行中将多页 PDF 转换为图像 如何将 PDF 文档转换为 Powerpoint 幻灯片


hei*_*eas 5

或者,您可以使用ghostscript并非每台 Mac 上都预安装的软件(这就是 ImageMagick 在幕后使用的功能)。您必须单独安装它。

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
Run Code Online (Sandbox Code Playgroud)

有关如何使用 CLI 参数的更多信息,请查看使用 Ghostscript