Magick.net: Exception when instantiating new MagickImageCollection by passing in PDF as a byte[]

Created on 30 Oct 2018  路  4Comments  路  Source: dlemstra/Magick.NET

Prerequisites

  • [x] I have written a descriptive issue title
  • [x] I have verified that I am using the latest version of Magick.NET
  • [x] I have searched open and closed issues to ensure it has not already been reported

Description

I'm trying to instantiate a new MagickImageCollection using a byte array, which should be an option from looking at the overloaded constructors. My goal is to convert a multi-page PDF into a collection of MagickImage objects, but I get the following error:
FailedToExecuteCommand `\"gswin32c.exe\" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 \"-sDEVICE=pngalpha\" -dTextAlphaBits=4 -dGraphicsAlphaBits=4 \"-r300x300\" \"-sOutputFile=C:/Users/KYLERJ~1.CIG/AppData/Local/Temp/magick-20604AaUhIVsEcWg%d\" \"-fC:/Users/KYLERJ~1.CIG/AppData/Local/Temp/magick-2060qx27_qu8fxBC\" \"-fC:/Users/KYLERJ~1.CIG/AppData/Local/Temp/magick-2060bh7xT1NnEmmm\"' (The system cannot find the file specified.\r\n) @ error/delegate.c/ExternalDelegateCommand/475

Here's my code:

public void ConvertPDFToImages(byte[] pdfBytes, string saveImgToPath, string saveThumbnailToPath = null)
        {
            MagickReadSettings readSettings = new MagickReadSettings();
            readSettings.Density = new Density(300, 300); // make this configurable
            readSettings.Format = MagickFormat.Pdf;
            using (MagickImageCollection images = new MagickImageCollection(pdfBytes, readSettings))
            {
                //images.Read(pdfBytes, settings);

                for (int i = 0; i > images.Count; i++)
                {
                    int docNum = i + 1;
                    string fullImgPath = FormatPath(saveImgToPath, docNum);
                    string fullThumbPath = FormatPath(saveThumbnailToPath, docNum);

                    _imageResizingService.OptimizeImage(images[0].ToByteArray(), fullImgPath, fullThumbPath);
                }
            }
        }

I have tried passing the pdfBytes along with the readSettings into the constructor in the using statement as you see above as well as passing them into the MagickImageCollection.Read() method just inside of the using statement. I get the same error I included above.

Steps to Reproduce

  1. Create a sample PDF, go to a base64 encoder online and convert the sample PDF.
  2. Get the byte[] from the base64 string by using the Convert.FromBase64String() method
  3. Set MagickReadSettings.Format = MagickFormat.Pdf;
  4. Try instantiating a new MagickImageCollection in a using statement as I did in the code snippet above.

System Configuration

OS: Windows 10 Enterprise
OS Version: 10.0.17134 Build 17134
Framework: C# .Net Core 2.1.3

  • Magick.NET version: v7.9.0.1
  • Magick.NET-Q16-AnyCPU

Most helpful comment

A PDF file needs to have Ghostscript installed and you probably need to pay for it when you use it commercially. For most other image formats you don't need to install something.

All 4 comments

Did you install Ghostscript on your server/machine?

@dlemstra No, I just installed the Nuget package. I must have overlooked that part of the docs. I've been using it to convert and optimize images by passing in the images as byte[] to the MagickImage constructor without any issues, though. Doesn't it use similar commands under the hood?

A PDF file needs to have Ghostscript installed and you probably need to pay for it when you use it commercially. For most other image formats you don't need to install something.

@dlemstra That solved the problem! My apologies and I appreciate the help!

Was this page helpful?
0 / 5 - 0 ratings