为什么在使用ImageMagick/Ghostscript时转换此PDF文件失败?

为什么在使用ImageMagick/Ghostscript时转换此PDF文件失败?,pdf,imagemagick,ghostscript,imagemagick-convert,Pdf,Imagemagick,Ghostscript,Imagemagick Convert,我想转换这个用LaTeX编译的PDF文件(Xeletex引擎,以便使用阿拉伯语字体),我想把它上传到网上,防止复制和粘贴它的内容 由于我正在寻找一个免费软件来做这项工作,我遇到了两个强大的野兽来做这项工作,即,ImageMagick和Ghostscript。我所需要的就是一次将一个文本PDF转换为图像PDF,如果可能的话,最好是批处理(一次转换多个PDF) 我在命令行中运行这段代码,它对于英文PDF非常有效: convert someenglish.pdf output.pdf 现在,当我

我想转换这个用LaTeX编译的PDF文件(Xeletex引擎,以便使用阿拉伯语字体),我想把它上传到网上,防止复制和粘贴它的内容

由于我正在寻找一个免费软件来做这项工作,我遇到了两个强大的野兽来做这项工作,即,
ImageMagick
Ghostscript
。我所需要的就是一次将一个文本PDF转换为图像PDF,如果可能的话,最好是批处理(一次转换多个PDF)

我在命令行中运行这段代码,它对于英文PDF非常有效:

convert someenglish.pdf output.pdf  
现在,当我对阿拉伯文PDF执行相同操作时,会出现以下错误:

convert.exe: PDFDelegateFailed `[ghostscript library] -q -dQUIET -dSAFER -dBATCH
 -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sD
EVICE=pngalpha" -dTextAlphaBits=4 -dGraphicsAlphaBits=4 "-r72x72"  "-sOutputFile
=C:/Users/doctorate/AppData/Local/Temp/magick-65203BNMxTDhXtkF%d" "-fC:/Users/doctorate/Ap
pData/Local/Temp/magick-65206AK54hOoKA62" "-fC:/Users/doctorate/AppData/Local/Temp/ma
gick-6520hDn-KMyTyxy2"':    **** Error reading a content stream. The page may be
 incomplete.
   **** Incorrect object count in object stream.
Error: /rangecheck in resolveobjectstream
Operand stack:
   78424   10   1   10   --dict:7/15(L)--   26   --nostringval--   35   --nostri
ngval--   --dict:2/2(L)--   --dict:2/2(L)--   --dict:2/2(L)--   --dict:2/2(L)--
  --dict:4/4(L)--   --dict:4/4(L)--   --dict:4/4(L)--   --dict:4/4(L)--   --dict
:4/4(L)--   --dict:3/3(L)--   --dict:2/2(L)--   --nostringval--   --dict:7/7(L)-
-   --dict:10/10(L)--   --nostringval--   --nostringval--   Type   Font   Subtyp
e   CIDFontType2   BaseFont   MYCROL+(AH
Execution stack:
   %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval-
-   2   %stopped_push   --nostringval--   --nostringval--   --nostringval--   fa
lse   1   %stopped_push   1983   1   3   %oparray_pop   1982   1   3   %oparray_
pop   1966   1   3   %oparray_pop   --nostringval--   --nostringval--   --nostri
ngval--   --nostringval--   --nostringval--   --nostringval--   --nostringval--
  --nostringval--   --nostringval--
Dictionary stack:
   --dict:1193/1684(ro)(G)--   --dict:1/20(G)--   --dict:82/200(L)--   --dict:82
/200(L)--   --dict:116/127(ro)(G)--   --dict:280/300(ro)(G)--   --dict:24/32(L)-
-
Current allocation mode is local
GPL Ghostscript 9.15: Unrecoverable error, exit code 1
 @ error/pdf.c/InvokePDFDelegate/263.
convert.exe: no images defined `test.pdf' @ error/convert.c/ConvertImageCommand/
3210.
问题
我错过了什么?我不是程序员,所以请在你的答案中考虑这个问题。我非常感谢您能展示如何在批处理过程中做到这一点

注释

  • Windows 7 32位

  • 鬼脚本版本9.15

  • 图像质量对我来说不是问题,即使72dpi也可以

  • 我想在输出的大小和文本的清晰度之间取得平衡。我只希望文本在网络上可读,而不是用它做一些OCR处理,所以图像不需要非常清晰。产出的规模更重要,越少越好,老实说,我不知道什么可能更好;在本例中,将PDF文件转换为PNG或JPEG

  • 我不想将一个PDF压缩成多个连续命名的PNG或JPEG,只需将一个PDF转换成另一个PDF,但将其作为内部图像,而不再复制和粘贴文本

更新
我试图制作一个简单的PDF工作示例,以模仿原始PDF,发现问题是因为包含了一种称为
(AH)Manal Black
的阿拉伯字体。在此MWE PDF上从命令行运行
pdffonts
,可提供:

Syntax Error (18062): Illegal character ')'
Syntax Error (18076): Dictionary key must be a name object
Syntax Error (18085): Dictionary key must be a name object
Syntax Error (18248): Illegal character ')'
Syntax Error (18248): Dictionary key must be a name object
Syntax Error (18253): Dictionary key must be a name object
Syntax Error (18599): Illegal character ')'
Syntax Error (18599): Dictionary key must be a name object
Syntax Error (18607): Dictionary key must be a name object
name                                 type              emb sub uni object ID
------------------------------------ ----------------- --- --- --- ---------
GAKHDJ+(AH                           CID TrueType      yes yes yes      5  0
HTCSVQ+Amiri-Regular                 CID TrueType      yes yes yes      7  0
通过在使用LaTeX/XeTeX引擎编译文档时排除此阿拉伯文字体,convert命令与其他英文PDF中的命令一样工作正常。所以这个问题很可能和字体解析有关



更新:这里有一个最小工作示例:

最小工作示例有一个PDF对象10,作为一个
ObjStm
(对象流),在这里可以找到这部分内容(为了提高可读性,我编辑了空白格式):

这是罪魁祸首。这是PDF生成软件中的一个错误


更新 也许我应该透露我是如何解析和解压缩MWE PDF的:

  • 使用QPDF进行尝试无效:

    qpdf --qdf --object-streams=disable mwe_ar.pdf qdf.pdf
    
     object stream 10 (file position 585): unexpected )
    
  • 使用
    pdftk
    尝试也不起作用:

    pdftk mwe_ar.pdf cat pdftk.pdf uncompress
    
     Error: Unable to find file.
     Error: Failed to open PDF file: 
        mwe_ar.pdf
     Errors encountered.  No output created.
     Done.  Input errors, so no output created.
    
  • 使用MuPDF的
    mutool
    尝试此操作也失败:

    mutool clean -d mwe_ar.pdf mutool.pdf
    
     warning: lexical error (unexpected ')')
     error: invalid key in dict
     error: cannot parse dict
     error: cannot open object stream (10 0 R)
     error: cannot load object stream containing object (1 0 R)
     warning: cannot load object (1 0 R) into cache
     warning: lexical error (unexpected ')')
     error: invalid key in dict
     error: cannot parse dict
     error: cannot open object stream (10 0 R)
     error: cannot load object stream containing object (4 0 R)
     error: cannot load object (4 0 R) into cache
    
  • 最后,作为最后手段,拯救:

    $ cat peepdf-commands.txt
    
     object 10
    
    $ peepdf.py -s peepdf-commands.txt
    
      << /Length 1000
      /N 13
      /Type /ObjStm
      /Filter /FlateDecode
      /First 84 >>
      stream
      9 0 3 72 11 133 2 197 1 312 15 343 4 446 14 625 19 876 6 1344 18 1514 5 1758 7 1886 <</Font<</F1 5 0 R/F2 7 0 R>>/ProcSet[/PDF/Text/ImageC/ImageB/ImageI]>>
      <</Resources 9 0 R/Type/Page/Parent 11 0 R/Contents[8 0 R]>>
      <</Type/Pages/Count 1/Kids[3 0 R]/MediaBox[0 0 595.28 841.89]>>
      <</Creator( XeTeX output 2015.05.01:1207)/Producer(xdvipdfmx \(20140317\))/CreationDate(D:20150501120749+01'00')>>
      <</Pages 11 0 R/Type/Catalog>>
      [417[251]421[257]424[368]443[470]445[355]450[380]480[322]498[480 233]505[461]508[256]514[326]520[264]]
      <</Type/Font/Subtype/CIDFontType2/BaseFont/GAKHDJ+#28AH)#20Manal#20Black/FontDescriptor 14 0 R/CIDSystemInfo<</Registry(Adobe)/Ordering(Identity)/Supplement 0>>/DW 199/W 15 0 R>>
      <</Type/FontDescriptor/Ascent 529/Descent -415/StemV 109/CapHeight 529/AvgWidth 392/FontBBox[-112 -321 1006 1137]/ItalicAngle 0/Flags 6/Style<</Panose<000000000000000000000000>>>/FontName/GAKHDJ+#28AH)#20Manal#20Black/FontFile2 16 0 R/CIDSet 17 0 R>>
      [39[693]41[522]51[535]108[415]124[415]388[218 926]402[1213]406[541]446[586]1886[317]1992[229]2016[366]2021[366]2105[244]2108[244]2139[1006]2150[295]2162[378]2227[379 452]2272[589]2294[176]2300[198]2308[389]2339[343]2356[723]2359[1079]2397[552]2413[346]2457[177]2491[299]2912[349]2952[219]2969[209]2973[148]2976[302]2981[341]3027[168]3149[550]3297[259]3325[292]3726[248]3732[319]3853[411]3893[179]4021[55]4323[104]4627[560]5068[238]5106[476]5322[159]5328[222]6366[93]]
      <</Type/Font/Subtype/CIDFontType2/BaseFont/HTCSVQ+Amiri-Regular/FontDescriptor 18 0 R/CIDSystemInfo<</Registry(Adobe)/Ordering(Identity)/Supplement 0>>/DW 190/W 19 0 R>>
      <</Type/FontDescriptor/Ascent 1123/Descent -635/StemV 87/CapHeight 1123/AvgWidth 685/FontBBox[-581 -900 11467 1815]/ItalicAngle 0/Flags 6/Style<</Panose<000000000500000000000000>>>/FontName/HTCSVQ+Amiri-Regular/FontFile2 20 0 R/CIDSet 21 0 R>>
      <</Type/Font/Subtype/Type0/BaseFont/GAKHDJ+#28AH)#20Manal#20Black/Encoding/Identity-H/DescendantFonts[4 0 R]/ToUnicode 12 0 R>>
      <</Type/Font/Subtype/Type0/BaseFont/HTCSVQ+Amiri-Regular/Encoding/Identity-H/DescendantFonts[6 0 R]/ToUnicode 13 0 R>>
    
      endstream
    
    $cat peepdf-commands.txt
    对象10
    $peepdf.py-s peepdf-commands.txt
    >
    流动
    9 0 3 72 11 133 2 197 1 312 15 343 4 446 14 625 19 876 6 1344 18 1514 5 1758 7 1886 
    [417[251]421[257]424[368]443[470]445[355]450[380]480[322]498[480 233]505[461]508[256]514[326]520[264]]
    [39[693]41[522]51[535]108[415]124[415]388[218 926]402[1213]406[541]446[586]1886[317]1992[229]2016[366]2021[366]2105[244]2108[244]2139[1006]2150[295]2162[378]2227[379 452]2272[589]2294[176]2300[198]2308[389]2339[343]2356[723]2359[1079]2397[552]2413[346]2457[177]2491[299]2912[349]2952[219]2969[209]2973[148]2976[302]2981[341]3027[168]3149[550]3297[259]3325[292]3726[248]3732[319]3853[411]3893[179]4021[55]4323[104]4627[560]5068[238]5106[476]5322[159]5328[222]6366[93]]
    尾流
    
  • 我越是频繁地使用PeePDF.py,我就越喜欢它。谢谢你,Jose Miguel,这是一个很棒的工具!

    我通常用来解决这个问题:

    pdftocairo corruptedinfile.pdf -pdf outfile.pdf
    

    之后,ghostscript可以正确处理该问题。

    ghostscript告诉您的PDF文件有问题。它试图从该问题中恢复,但情况太严重,它放弃了。无法判断PDF文件有什么问题(甚至无法判断ghostscript是否有问题)在没有看到PDF的情况下,我在这个链接中提供了一个简单的PDF示例:@KenS我更新了帖子。提前谢谢。我能告诉你的是,仍然有问题。字体“看起来”嵌入了一个非法字符,但没有转义,如果你能把一个简单的示例放在某个地方,我可以看看,但事实是Pdfonts告诉您也有问题,这是非常有力的支持,表明存在错误。您可能应该打开LaTeX的错误报告(或用于制作PDF文件的任何文件)。实际上,问题似乎是字体名称包含括号(),我怀疑这是否合法,它们可能需要表示为转义符,即#xxx数值,而不是表示为(或)感谢,但使用不同的字体会导致问题消失。因此,XeTeX可能无法在某些unicode字符中恢复为默认字体(啊)Manal Black未能提供?!出于好奇,您如何查看对象流的信息?如果字体没有这样的问题,那么批处理如何?在这种情况下,使用
    .png
    .jpg
    使用
    ghost
    sricpt?@博士学位:“使用不同的字体导致问题消失”。我打赌不同字体的名称中没有(关闭)括号?没错,它没有这些不寻常的括号。这直接让我想到了一个问题:如何更改字体名称并删除麻烦的关闭父项?好吧,不仅仅是重命名文件名:
    ah manal black.ttf
    $ cat peepdf-commands.txt
    
     object 10
    
    $ peepdf.py -s peepdf-commands.txt
    
      << /Length 1000
      /N 13
      /Type /ObjStm
      /Filter /FlateDecode
      /First 84 >>
      stream
      9 0 3 72 11 133 2 197 1 312 15 343 4 446 14 625 19 876 6 1344 18 1514 5 1758 7 1886 <</Font<</F1 5 0 R/F2 7 0 R>>/ProcSet[/PDF/Text/ImageC/ImageB/ImageI]>>
      <</Resources 9 0 R/Type/Page/Parent 11 0 R/Contents[8 0 R]>>
      <</Type/Pages/Count 1/Kids[3 0 R]/MediaBox[0 0 595.28 841.89]>>
      <</Creator( XeTeX output 2015.05.01:1207)/Producer(xdvipdfmx \(20140317\))/CreationDate(D:20150501120749+01'00')>>
      <</Pages 11 0 R/Type/Catalog>>
      [417[251]421[257]424[368]443[470]445[355]450[380]480[322]498[480 233]505[461]508[256]514[326]520[264]]
      <</Type/Font/Subtype/CIDFontType2/BaseFont/GAKHDJ+#28AH)#20Manal#20Black/FontDescriptor 14 0 R/CIDSystemInfo<</Registry(Adobe)/Ordering(Identity)/Supplement 0>>/DW 199/W 15 0 R>>
      <</Type/FontDescriptor/Ascent 529/Descent -415/StemV 109/CapHeight 529/AvgWidth 392/FontBBox[-112 -321 1006 1137]/ItalicAngle 0/Flags 6/Style<</Panose<000000000000000000000000>>>/FontName/GAKHDJ+#28AH)#20Manal#20Black/FontFile2 16 0 R/CIDSet 17 0 R>>
      [39[693]41[522]51[535]108[415]124[415]388[218 926]402[1213]406[541]446[586]1886[317]1992[229]2016[366]2021[366]2105[244]2108[244]2139[1006]2150[295]2162[378]2227[379 452]2272[589]2294[176]2300[198]2308[389]2339[343]2356[723]2359[1079]2397[552]2413[346]2457[177]2491[299]2912[349]2952[219]2969[209]2973[148]2976[302]2981[341]3027[168]3149[550]3297[259]3325[292]3726[248]3732[319]3853[411]3893[179]4021[55]4323[104]4627[560]5068[238]5106[476]5322[159]5328[222]6366[93]]
      <</Type/Font/Subtype/CIDFontType2/BaseFont/HTCSVQ+Amiri-Regular/FontDescriptor 18 0 R/CIDSystemInfo<</Registry(Adobe)/Ordering(Identity)/Supplement 0>>/DW 190/W 19 0 R>>
      <</Type/FontDescriptor/Ascent 1123/Descent -635/StemV 87/CapHeight 1123/AvgWidth 685/FontBBox[-581 -900 11467 1815]/ItalicAngle 0/Flags 6/Style<</Panose<000000000500000000000000>>>/FontName/HTCSVQ+Amiri-Regular/FontFile2 20 0 R/CIDSet 21 0 R>>
      <</Type/Font/Subtype/Type0/BaseFont/GAKHDJ+#28AH)#20Manal#20Black/Encoding/Identity-H/DescendantFonts[4 0 R]/ToUnicode 12 0 R>>
      <</Type/Font/Subtype/Type0/BaseFont/HTCSVQ+Amiri-Regular/Encoding/Identity-H/DescendantFonts[6 0 R]/ToUnicode 13 0 R>>
    
      endstream
    
    pdftocairo corruptedinfile.pdf -pdf outfile.pdf