使用 ImageMagick 转换带有重音字符的 PDF 时出现问题
Problems using ImageMagick for converting PDF with accented characters
我在使用 ImageMagick 或 Ghostscript 将 PDF 转换为图像时遇到问题。所有重音字符从转换后的图像中消失。我发现有几个人遇到了同样的问题,显然更新了 ImageMagick 包和 Ghostscript 修复了它,但对我来说不是。
我在每次测试中都使用这个 PDF 文件:https://www.dropbox.com/s/3gso0sw1e1n8f9r/error-with-accents.pdf?dl=0
我在 Azure 上有一个 Ubuntu 14.04.2 LTS 服务器,我需要 ImageMagick 在其中工作。从官方存储库我有 ImageMagick 6.7.7 和 Ghostscript 9.10。后来,我尝试升级它们以解决我的问题,现在我在 /opt/imagemagick-6.8
文件夹上也有 ImageMagick 6.8.9-10 运行ning 并且我添加了 Ubuntu 的 15.04 存储库所以我可以直接通过apt-get安装Ghostscript 9.15。 None 个解决了我的问题。
这是我在 Ubuntu 14.04 服务器上的最新尝试:
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 14.04.2 LTS
Release: 14.04
Codename: trusty
$ /opt/imagemagick-6.8/bin/convert -version
Version: ImageMagick 6.8.9-10 Q16 x86_64 2015-07-30 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
Features: DPC OpenMP
Delegates: jng jpeg png x xml zlib
$ /opt/imagemagick-6.8/bin/convert -list configure |grep DELEGATES
DELEGATES mpeg jng jpeg png ps x xml zlib
$ /opt/imagemagick-6.8/bin/convert error-with-accents.pdf -verbose -alpha off -resample 150 -density 150 -quality '80' im-test.jpg
**** Warning: considering '0000000000 XXXXX n' as a free entry.
**** This file had errors that were repaired or ignored.
**** The file was produced by:
**** >>>> Mac OS X 10.10.4 Quartz PDFContext <<<<
**** Please notify the author of the software that produced this
**** file that it does not conform to Adobe's published PDF
**** specification.
error-with-accents.pdf=>im-test.jpg PDF 595x794=>1240x1654 1240x1654+0+0 16-bit sRGB 172KB 0.440u 0:00.240
$ gs -v
GPL Ghostscript 9.15 (2014-09-22)
Copyright (C) 2014 Artifex Software, Inc. All rights reserved.
$ gs -dBATCH -dNOPAUSE -sDEVICE=jpeg -sOutputFile=gs-test.jpg error-with-accents.pdf
GPL Ghostscript 9.15 (2014-09-22)
Copyright (C) 2014 Artifex Software, Inc. All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
**** Warning: considering '0000000000 XXXXX n' as a free entry.
Processing pages 1 through 1.
Page 1
**** This file had errors that were repaired or ignored.
**** The file was produced by:
**** >>>> Mac OS X 10.10.4 Quartz PDFContext <<<<
**** Please notify the author of the software that produced this
**** file that it does not conform to Adobe's published PDF
**** specification.
$ convert -version
Version: ImageMagick 6.7.7-10 2014-03-06 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2012 ImageMagick Studio LLC
Features: OpenMP
$ convert -list configure |grep DELEGATES
DELEGATES bzlib djvu fftw fontconfig freetype jbig jpeg jng jp2 lcms2 lqr lzma openexr pango png rsvg tiff x11 xml wmf zlib
$ convert error-with-accents.pdf -verbose -alpha off -resample 150 -density 150 -quality '80' im-test-6.7.7.jpg
**** Warning: considering '0000000000 XXXXX n' as a free entry.
**** This file had errors that were repaired or ignored.
**** The file was produced by:
**** >>>> Mac OS X 10.10.4 Quartz PDFContext <<<<
**** Please notify the author of the software that produced this
**** file that it does not conform to Adobe's published PDF
**** specification.
error-with-accents.pdf=>im-test-6.7.7.jpg PDF 595x794=>1240x1654 1240x1654+0+0 16-bit DirectClass 160KB 0.490u 0:00.279
结果相同:
我能够在 Mac OS 上正确地 运行 Ghostscript 和 ImageMagick。而且,根据 this post,我在 Ubuntu 上的版本应该可以工作。所以我认为它与 FreeType 字体有关。我对如何解决这个问题一无所知。有帮助吗?
您尝试处理的 PDF 文档经常修改并重新保存:455 次 在 2010-03-06 和 2014-06-17 之间。
您可以通过 运行 pdfinfo -meta error-with-accents.pdf
.
来验证
我不会说或读葡萄牙语,所以我无法立即识别输出图像中是否缺少重音。
当我使用 IM v6.9.0-0 Q16 x86_64 2015-05-14
(使用 Ghostscript v9.16
)尝试你的命令时,我没有看到任何错误:
您的 PDF 嵌入了它使用的所有字体(请参阅 emb
列)。这意味着,将不会使用 FreeType 来查找任何 replacement/substitute 字体:
$ pdffonts error-with-accents.pdf
name type encoding emb sub uni object ID
-------------------------- ---------- ---------------- --- --- --- ---------
RUXYWW+ConduitITC-Light Type 1C MacRoman yes yes no 14 0
NOYZMG+Y2KNeophyte TrueType WinAnsi yes yes yes 10 0
MVLYKX+ConduitITC-Medium Type 1C MacRoman yes yes no 15 0
JDNVDM+ConduitITC-Bold Type 1C MacRoman yes yes no 13 0
无论如何:您应该集中精力获得一个可以正确处理您的 PDF 的 Ghostscript 版本。因为 ImageMagick 本身不做任何 PDF 处理——它依赖 Ghostscript 作为它的 "delegate" 来处理。
6年后问题依然存在。尝试将 pdf 转换为 png 图像时,渲染的文本中缺少一些重音字符。最后我通过以下方式解决了问题:我使用“gs”命令从pdf文档中转换了几页。
gs -sDEVICE=pngalpha -o file-%03d.jpg -r300 document.pdf
然后我检查了 gs 的输出,发现缺少字体的投诉。就我而言,它是 ArialMT。然后我简单地从我的 Linux 系统 TTF 字体目录中挑选了一个字体,并将其复制到 ghostscript 字体目录中,明确替换丢失的字体:
sudo cp /usr/share/fonts/TTF/DejaVuSans.ttf /usr/share/ghostscript/9.53.3/Resource/Font/ArialMT
因为 ghostscript 字体没有扩展名。然后我能够使用 imagemagick 或 graphicsmagick 的“转换”命令将 pdf 文档转换为 png 图像:
for i in $(seq 0 143);do echo $i;gm convert -density 600 document.pdf[$i] -verbose -colorspace RGB -flatten ./png/$i.png ;done
我使用了 for 循环和 graphicsmagick,据我所知,它比 imagemagick 快得多,而且是以一页一页的方式,因为一次转换大文档会导致内存问题。
我在使用 ImageMagick 或 Ghostscript 将 PDF 转换为图像时遇到问题。所有重音字符从转换后的图像中消失。我发现有几个人遇到了同样的问题,显然更新了 ImageMagick 包和 Ghostscript 修复了它,但对我来说不是。
我在每次测试中都使用这个 PDF 文件:https://www.dropbox.com/s/3gso0sw1e1n8f9r/error-with-accents.pdf?dl=0
我在 Azure 上有一个 Ubuntu 14.04.2 LTS 服务器,我需要 ImageMagick 在其中工作。从官方存储库我有 ImageMagick 6.7.7 和 Ghostscript 9.10。后来,我尝试升级它们以解决我的问题,现在我在 /opt/imagemagick-6.8
文件夹上也有 ImageMagick 6.8.9-10 运行ning 并且我添加了 Ubuntu 的 15.04 存储库所以我可以直接通过apt-get安装Ghostscript 9.15。 None 个解决了我的问题。
这是我在 Ubuntu 14.04 服务器上的最新尝试:
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 14.04.2 LTS
Release: 14.04
Codename: trusty
$ /opt/imagemagick-6.8/bin/convert -version
Version: ImageMagick 6.8.9-10 Q16 x86_64 2015-07-30 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
Features: DPC OpenMP
Delegates: jng jpeg png x xml zlib
$ /opt/imagemagick-6.8/bin/convert -list configure |grep DELEGATES
DELEGATES mpeg jng jpeg png ps x xml zlib
$ /opt/imagemagick-6.8/bin/convert error-with-accents.pdf -verbose -alpha off -resample 150 -density 150 -quality '80' im-test.jpg
**** Warning: considering '0000000000 XXXXX n' as a free entry.
**** This file had errors that were repaired or ignored.
**** The file was produced by:
**** >>>> Mac OS X 10.10.4 Quartz PDFContext <<<<
**** Please notify the author of the software that produced this
**** file that it does not conform to Adobe's published PDF
**** specification.
error-with-accents.pdf=>im-test.jpg PDF 595x794=>1240x1654 1240x1654+0+0 16-bit sRGB 172KB 0.440u 0:00.240
$ gs -v
GPL Ghostscript 9.15 (2014-09-22)
Copyright (C) 2014 Artifex Software, Inc. All rights reserved.
$ gs -dBATCH -dNOPAUSE -sDEVICE=jpeg -sOutputFile=gs-test.jpg error-with-accents.pdf
GPL Ghostscript 9.15 (2014-09-22)
Copyright (C) 2014 Artifex Software, Inc. All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
**** Warning: considering '0000000000 XXXXX n' as a free entry.
Processing pages 1 through 1.
Page 1
**** This file had errors that were repaired or ignored.
**** The file was produced by:
**** >>>> Mac OS X 10.10.4 Quartz PDFContext <<<<
**** Please notify the author of the software that produced this
**** file that it does not conform to Adobe's published PDF
**** specification.
$ convert -version
Version: ImageMagick 6.7.7-10 2014-03-06 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2012 ImageMagick Studio LLC
Features: OpenMP
$ convert -list configure |grep DELEGATES
DELEGATES bzlib djvu fftw fontconfig freetype jbig jpeg jng jp2 lcms2 lqr lzma openexr pango png rsvg tiff x11 xml wmf zlib
$ convert error-with-accents.pdf -verbose -alpha off -resample 150 -density 150 -quality '80' im-test-6.7.7.jpg
**** Warning: considering '0000000000 XXXXX n' as a free entry.
**** This file had errors that were repaired or ignored.
**** The file was produced by:
**** >>>> Mac OS X 10.10.4 Quartz PDFContext <<<<
**** Please notify the author of the software that produced this
**** file that it does not conform to Adobe's published PDF
**** specification.
error-with-accents.pdf=>im-test-6.7.7.jpg PDF 595x794=>1240x1654 1240x1654+0+0 16-bit DirectClass 160KB 0.490u 0:00.279
结果相同:
我能够在 Mac OS 上正确地 运行 Ghostscript 和 ImageMagick。而且,根据 this post,我在 Ubuntu 上的版本应该可以工作。所以我认为它与 FreeType 字体有关。我对如何解决这个问题一无所知。有帮助吗?
您尝试处理的 PDF 文档经常修改并重新保存:455 次 在 2010-03-06 和 2014-06-17 之间。
您可以通过 运行 pdfinfo -meta error-with-accents.pdf
.
我不会说或读葡萄牙语,所以我无法立即识别输出图像中是否缺少重音。
当我使用 IM v6.9.0-0 Q16 x86_64 2015-05-14
(使用 Ghostscript v9.16
)尝试你的命令时,我没有看到任何错误:
您的 PDF 嵌入了它使用的所有字体(请参阅 emb
列)。这意味着,将不会使用 FreeType 来查找任何 replacement/substitute 字体:
$ pdffonts error-with-accents.pdf
name type encoding emb sub uni object ID
-------------------------- ---------- ---------------- --- --- --- ---------
RUXYWW+ConduitITC-Light Type 1C MacRoman yes yes no 14 0
NOYZMG+Y2KNeophyte TrueType WinAnsi yes yes yes 10 0
MVLYKX+ConduitITC-Medium Type 1C MacRoman yes yes no 15 0
JDNVDM+ConduitITC-Bold Type 1C MacRoman yes yes no 13 0
无论如何:您应该集中精力获得一个可以正确处理您的 PDF 的 Ghostscript 版本。因为 ImageMagick 本身不做任何 PDF 处理——它依赖 Ghostscript 作为它的 "delegate" 来处理。
6年后问题依然存在。尝试将 pdf 转换为 png 图像时,渲染的文本中缺少一些重音字符。最后我通过以下方式解决了问题:我使用“gs”命令从pdf文档中转换了几页。
gs -sDEVICE=pngalpha -o file-%03d.jpg -r300 document.pdf
然后我检查了 gs 的输出,发现缺少字体的投诉。就我而言,它是 ArialMT。然后我简单地从我的 Linux 系统 TTF 字体目录中挑选了一个字体,并将其复制到 ghostscript 字体目录中,明确替换丢失的字体:
sudo cp /usr/share/fonts/TTF/DejaVuSans.ttf /usr/share/ghostscript/9.53.3/Resource/Font/ArialMT
因为 ghostscript 字体没有扩展名。然后我能够使用 imagemagick 或 graphicsmagick 的“转换”命令将 pdf 文档转换为 png 图像:
for i in $(seq 0 143);do echo $i;gm convert -density 600 document.pdf[$i] -verbose -colorspace RGB -flatten ./png/$i.png ;done
我使用了 for 循环和 graphicsmagick,据我所知,它比 imagemagick 快得多,而且是以一页一页的方式,因为一次转换大文档会导致内存问题。