将PDF转换为PNG

扫码查看
本文介绍了将PDF转换为PNG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将PDF转换为PNG图像(至少是一个的封面)。我用pdftk成功地提取了PDF的第一页。我正在使用imagemagick进行转换:

I'm trying to convert a PDF to a PNG image (at least the cover of one). I'm successfully extracting the first page of the PDF with pdftk. I'm using imagemagick to do the conversion:

convert cover.pdf cover.png

这很有效,但遗憾的是,cover.png通过错误渲染(PDF中的某些alpha对象未正确呈现)。我知道ImageMagick使用GhostScript进行转换,如果我直接使用gs我可以得到所需的结果,但我宁愿使用转换库,因为它有其他我想要利用的工具。

This works, but unfortunately the cover.png comes through incorrectly rendered (some of the alpha object in the PDF aren't rendered properly). I know ImageMagick uses GhostScript to do the conversion and if I do it directly with gs I can get the desired results, but I'd rather use the convert library as it has other tools I'd like to leverage.

GhostScript中的这个命令可以完成所需的图像:

This command in GhostScript accomplishes the desired image:

gs -sDEVICE=pngalpha -sOutputFile=cover.png -r144 cover.pdf

我想知道有没有办法通过参数传递转换为GhostScript还是直接调用GhostScript?

I'm wondering is there any way to pass arguments through convert to GhostScript or am I stuck with calling GhostScript directly?

推荐答案

您可以使用一个命令行和两个命令( gs convert )通过管道连接,如果第一个命令可以将其输出写入stdout,如果第二个命令可以读取它从stdin输入。

You can use one commandline with two commands (gs, convert) connected through a pipe, if the first command can write its output to stdout, and if the second one can read its input from stdin.


  1. 幸运的是,gs可以写入stdout( ... -o%stdout ... )。

  2. 幸运的是,转换可以从stdin读取( convert -background transparent - output.png )。

  1. Luckily, gs can write to stdout (... -o %stdout ...).
  2. Luckily, convert can read from stdin (convert -background transparent - output.png).

问题解决了:


  • GS用于处理特殊图像的alpha通道,

  • 转换用于创建透明背景,

  • 用于避免在磁盘上写出临时文件的管道。

完整解决方案:

Complete solution:

gs -sDEVICE=pngalpha       \
   -o %stdout              \
   -r144 cover.pdf         \
   |                       \
convert                    \
   -background transparent \
   -                       \
    cover.png






更新



如果您希望每个PDF页面都有一个单独的PNG,你可以使用%d 语法:

gs -sDEVICE=pngalpha -o file-%03d.png -r144 cover.pdf

这将创建名为<$的PNG文件c $ c> page-000.png , page-001.png ,...(注意% d -counting从零开始 - file-000.png 对应于PDF的第1页, 001 到第2页...

This will create PNG files named page-000.png, page-001.png, ... (Note that the %d-counting is zero-based -- file-000.png corresponds to page 1 of the PDF, 001 to page 2...

或者,如果你想保持透明的背景,对于100页的PDF,

Or, if you want to keep your transparent background, for a 100-page PDF, do

for i in {1..100}; do        \
                             \
  gs -sDEVICE=pngalpha       \
     -dFirstPage="${i}"      \
     -dLastPage="${i}"       \
     -o %stdout              \
     -r144 input.pdf         \
     |                       \
  convert                    \
     -background transparent \
     -                       \
      page-${i}.png ;        \
                             \
done

这篇关于将PDF转换为PNG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 21:30
查看更多