Search code examples
c#pdfimagickmagick.netimagemagick.net

Magick.net converting PDF to image "unable to create temporary file '': No such file or directory @ error/pdf.c/ReadPDFImage/476"


I'm trying to use Magick.net in a console application to render images from a PDFs and can't seem to get around this problem.

Upon calling "MagickImageCollection.Read(byte[], settings)" I always get a

"unable to create temporary file '': No such file or directory @ error/pdf.c/ReadPDFImage/476"

exception.

I have tried:

  • Placing both x86 and 64bit Ghostscript dlls in the bin folder.
  • Using combinations of AnyCPU, x86, 64 versions of Magick.net, with versions of GS
  • Setting MagickNET.SetGhostscriptDirectory to the program files GS bin folder
  • Setting MagickNET.SetTempDirectory to a folder in c:/temp and confirmed that my application can access by programatically moving a file in there.
  • Setting the MagickAnyCPU.CacheDirectory to a folder in c:/temp

I'm out of ideas of what I could be doing wrong

    using (MagickImageCollection images = new MagickImageCollection())
    {
        // Add all the pages of the pdf file to the collection
        images.Read(file, settings);

        switch (orientation)
        {
            case Orientation.Horizontal:
                using (MagickImage image = (MagickImage)images.AppendHorizontally())
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        image.Write(ms);

                        return ms.ToArray();
                    }

                }
            case Orientation.Vertical:
                using (MagickImage image = (MagickImage)images.AppendHorizontally())
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        image.Write(ms);

                        return ms.ToArray();
                    }
                }
        }
    }

Solution

  • I finally managed to overcome this problem, I was passing the wrong read settings to MagickImageCollection.Read(byte[], settings).

    I was telling Magick to read the PDF with the PNG format instead of writing the result to PNG...

    MagickReadSettings settings = new MagickReadSettings();
    settings.Format = MagickFormat.Png;
    

    I feel a bit silly but the error message completely through me off.