Imageresizer:如何修复GhostScript异常时转换PDF到PNG

本文关键字:转换 PDF PNG 异常 何修复 GhostScript Imageresizer | 更新日期: 2023-09-27 17:50:59

当尝试使用ImageResizer(3.4.2)将pdf转换为PNG缩略图时,如下所示:

https://somehost.com/file.pdf?format=png&宽度= 1024,身高= 1024,自转= true

一些pdf文件工作良好。其他的给出类似于下面的异常:

ImageResizer.Plugins.PdfRenderer.Ghostscript.GhostscriptException: <pdf> <fileNameData><value>67</value><value>58</value><value>92</value><value>87</value><value>105</value><value>110</value><value>100</value><value>111</value><value>119</value><value>115</value><value>92</value><value>84</value><value>69</value><value>77</value><value>80</value><value>92</value><value>116</value><value>109</value><value>112</value><value>54</value><value>49</value><value>53</value><value>56</value><value>46</value><value>116</value><value>109</value><value>112</value></fileNameData> <pageCount>1</pageCount> <pages> <page number="1"> <mediaBox height="792" width="612" top="0" left="0"/> Error: /stackunderflow in --dup-- Operand stack: Execution stack: %interp_exit .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push 1932 1 3 %oparray_pop 1931 1 3 %oparray_pop 1915 1 3 %oparray_pop 1803 1 3 %oparray_pop --nostringval-- %errorexec_pop .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- 2 1 1 --nostringval-- %for_pos_int_continue --nostringval-- Dictionary stack: --dict:1172/1684(ro)(G)-- --dict:1/20(G)-- --dict:84/200(L)-- --dict:84/200(L)-- --dict:109/127(ro)(G)-- --dict:292/300(ro)(G)-- --dict:26/32(L)-- Current allocation mode is local Current file position is 3951
Generated: Fri, 07 Mar 2014 17:14:21 GMT
ImageResizer.Plugins.PdfRenderer.Ghostscript.GhostscriptException: <pdf>
<fileNameData><value>67</value><value>58</value><value>92</value><value>87</value><value>105</value><value>110</value><value>100</value><value>111</value><value>119</value><value>115</value><value>92</value><value>84</value><value>69</value><value>77</value><value>80</value><value>92</value><value>116</value><value>109</value><value>112</value><value>54</value><value>49</value><value>53</value><value>56</value><value>46</value><value>116</value><value>109</value><value>112</value></fileNameData>
<pageCount>1</pageCount>
<pages>
<page number="1">
<mediaBox height="792" width="612" top="0" left="0"/>
Error: /stackunderflow in --dup--
Operand stack:
Execution stack:
   %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--   --nostringval--   false   1   %stopped_push   1932   1   3   %oparray_pop   1931   1   3   %oparray_pop   1915   1   3   %oparray_pop   1803   1   3   %oparray_pop   --nostringval--   %errorexec_pop   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   2   1   1   --nostringval--   %for_pos_int_continue   --nostringval--
Dictionary stack:
   --dict:1172/1684(ro)(G)--   --dict:1/20(G)--   --dict:84/200(L)--   --dict:84/200(L)--   --dict:109/127(ro)(G)--   --dict:292/300(ro)(G)--   --dict:26/32(L)--
Current allocation mode is local
Current file position is 3951
   at ImageResizer.Plugins.PdfRenderer.Ghostscript.GhostscriptEngine.Execute(GhostscriptSettings settings)
   at ImageResizer.Plugins.PdfRenderer.PdfRendererPlugin.GetPdfInfo(String path)
   at ImageResizer.Plugins.PdfRenderer.PdfRendererPlugin.DecodeStream(Stream s, ResizeSettings settings, String optionalPath)
   at ImageResizer.Resizing.AbstractImageProcessor.DecodeStream(Stream s, ResizeSettings settings, String optionalPath)
   at ImageResizer.ImageBuilder.DecodeStream(Stream s, ResizeSettings settings, String optionalPath)
   at ImageResizer.ImageBuilder.LoadImage(Object source, ResizeSettings settings, Boolean restoreStreamPos)
   at ImageResizer.ImageBuilder.BuildJob(ImageJob job)
   at ImageResizer.ImageBuilder.Build(ImageJob job)
   at ImageResizer.ImageBuilder.Build(Object source, Object dest, ResizeSettings settings, Boolean disposeSource, Boolean addFileExtension)
   at ImageResizer.ImageBuilder.Build(Object source, Object dest, ResizeSettings settings, Boolean disposeSource)
   at ImageResizer.ImageBuilder.Build(Object source, Object dest, ResizeSettings settings)
   at ImageResizer.InterceptModule.<>c__DisplayClass3.<HandleRequest>b__2(Stream stream)
   at ImageResizer.Plugins.DiskCache.CustomDiskCache.<>c__DisplayClasse.<TryWriteFile>b__d()
   at ImageResizer.Plugins.DiskCache.LockProvider.TryExecute(String key, Int32 timeoutMs, LockCallback success)
   at ImageResizer.Plugins.DiskCache.CustomDiskCache.TryWriteFile(CacheResult result, String physicalPath, String relativePath, ResizeImageDelegate writeCallback, DateTime sourceModifiedUtc, Int32 timeoutMs, Boolean recheckFS)
   at ImageResizer.Plugins.DiskCache.CustomDiskCache.GetCachedFile(String keyBasis, String extension, ResizeImageDelegate writeCallback, DateTime sourceModifiedUtc, Int32 timeoutMs, Boolean asynchronous)
   at ImageResizer.Plugins.DiskCache.DiskCache.Process(IResponseArgs e)
   at ImageResizer.Plugins.DiskCache.DiskCache.Process(HttpContext context, IResponseArgs e)
   at ImageResizer.InterceptModule.HandleRequest(HttpContext context, String virtualPath, NameValueCollection queryString, IVirtualFile vf)
   at ImageResizer.InterceptModule.CheckRequest_PostAuthorizeRequest(Object sender, EventArgs e)
   at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

我已经尝试了几个不同版本的Ghostscript,包括Imageresizer自动下载的版本以及sourceforge(9.10)的最新版本。

Imageresizer:如何修复GhostScript异常时转换PDF到PNG

stackunderflow基本上告诉你PostScript是无效的。在这种情况下,程序试图复制('dup')堆栈上的对象,显然堆栈上没有对象,因此有stackunderflow。

为了调查,我需要一份你正在使用的PDF文件的副本,以及传递给Ghostscript的确切命令行。如果您可以提供这两个,那么您应该通过在shell中执行上述命令行来快速测试问题,如果仍然出错,那么您可以在bugs.ghostscript.com

上打开错误报告。

我遇到了同样的错误,我下载了PdfRenderer插件的源代码。你会在Ghostscript/Resources/目录下的ImageResizer.Plugins.PdfRenderer项目中找到PostScript (pdfInfo.ps)文件。

您可以修改此PostScript并保留以下部分:

% CropBox
dup /CropBox pget {
  aload pop   % NOTE: Elements are reversed when popping from stack 
  (<cropBox) print 
  ( height=") print =print (") print
  ( width=") print =print (") print
  ( top=") print =print (") print
  ( left=") print =print (") print
  (/>'n) print flush
} if

当然,您必须构建项目并使用输出dll。