问题
We are using Magick.Net version 7.0 with Ghostscript 9.16. We are reading in a PDF and converting this to a tif or a jpg image. Everything is working fine when we run these through one at a time and our PDF gets converted.
This is an application that will be hit by many systems, so we put a small load test to ensure we could handle multiple requests. Everything runs great as long as we use different PDF files. If we try and run the same PDF file through multiple times (doing 5 requests at the same time with the same PDF), we encounter and error. The error we receive is PDFDelegateFailed. We are not sure why this error occurs and if we try other formats (such as tif to jpg), there are no issues.
ImageMagick.MagickDelegateErrorException:
ESBService.exe: PDFDelegateFailed [ghostscript library 9.16] -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pngalpha" -dTextAlphaBits=4 -dGraphicsAlphaBits=4 "-r96x96" "-sOutputFile=C:/Users/esbsvc/AppData/Local/Temp/magick-4668LPfdzdzRfLYF%d" "-fC:/Users/esbsvc/AppData/Local/Temp/magick-4668wanF98SE_8PK" "-fC:/Users/esbsvc/AppData/Local/Temp/magick-4668L3mJE6M2iUZV": (null)' @ error/pdf.c/ReadPDFImage/788 at ImageMagick.Wrapper.MagickImageCollection.HandleException(MagickException exception) at ImageMagick.Wrapper.MagickImageCollection.Read(Byte[] data, MagickReadSettings readSettings) at ImageMagick.MagickImageCollection.Read(Byte[] data, MagickReadSettings readSettings) at __DynamicCode.Typeaeb039071464a22ae6518eaa5ec46c.OnExecute(PipelineContext1 context) in c:\Users\esbsvc\AppData\Local\Temp\xp42eval.0.cs:line 112
Any help with this would be appreciated Mike H.
回答1:
There are two likely problems:
1) The C# code is using a single copy of the Ghostscript DLL and you haven't built it to be thread safe (I cannot recall what the default is currently on Windows). In effect you are running multiple threads rather than processes.
2) You have a collision on file access. In order to interpret a PDF file it is necessary to jump around the file a lot, I would guess that two processes tried to relocate the file pointer simultaneously and one failed.
ImageMagick can't handle PDF files directly, unlike image formats (PDF is not an image format, its much more complex), so it will not need to invoke Ghostscript. If you were to try the same with PostScript files you might encounter the same problem. However, since PostScript files are read linearly you may not have a problem with those.
If you capture the Ghostscript back channel output (and stop using -dQUIET) then you might get some more useful information.
Since you say this 'will be hit by many systems' please check the terms of the AGPL and ensure that your usage is consistent with the licence.
回答2:
The API documentation of Ghostscript (http://www.ghostscript.com/doc/current/API.htm) states the following:
The Win32 DLL gsdll32.dll can be used by multiple programs simultaneously, but only once within each process.
The version of Magick.NET that you are using does not handle this properly. I just pushed a patch to GIT repository of ImageMagick to make sure the DLL can be used only once. The first thread will use the library in memory and the second/third/etc. thread will forced to use the command line. Magick.NET 7.0.0.0022 has just been published and includes this change.
来源:https://stackoverflow.com/questions/33872598/error-with-imagemagick-and-ghostscript-converting-from-pdf