Magick.NET icon indicating copy to clipboard operation
Magick.NET copied to clipboard

Magick.NET in .NET Core

Open Thilaksiva opened this issue 6 years ago • 1 comments

Prerequisites

I have developed a Web API application in .Net Core 3.1. The main purpose of that Web API is to convert the PDF to images. I have installed the Magick.NET-Q16-AnyCPU version 7.20.0.1 through nuget package .NET CLI. My development machine has Windows operating system. I can able to develop the code for converting PDF's to images and it is working in my local machine.

Description

The problem I am facing is, I am deploying this Web API in Kubernetes in Google Cloud Platform. The deployment machine is only linux. The code for PDF to image conversion does not works in the production environment. But the same is working locally. I have read the documentation and it says, "Ghostscript" is necessary for converting Pdf's to images. But without Ghostscript my code works correctly in local environment.

Kindly suggest me a solution to be able to deploy it in kubernetes (linux environment). For your information, my deployment machine is virtual and I cannot access it.

I have also attached the code I used below. Kindly make a note on images.Read method call. I don't even get exception here when I deploy my application. But still my PDF is not read correctly.

public IActionResult ConvertPdfToImageAsync(string InvoiceId, string ClientReference, string PdfUrl = null)
    {
        if (!string.IsNullOrEmpty(PdfUrl))
        {
            var settings = new MagickReadSettings();
            string ImageLinks = string.Empty;
            settings.Density = new Density(300, 300);
            settings.Format = MagickFormat.Pdf;
            try
            {
                using (var images = new MagickImageCollection())
                {
                    Guid guidObj = Guid.NewGuid();
                    var pdfContent = new MemoryStream();
                    StorageClientBuilder storageClientBuilder = new StorageClientBuilder
                    {
                        CredentialsPath = APICredentials
                    };
                    var storage = storageClientBuilder.Build();
                    Uri uri = new Uri(PdfUrl);
                    string filename = Path.GetFileName(uri.AbsolutePath);
                    storage.DownloadObject(bucketName, filename, pdfContent);
                    pdfContent.Position = 0;
                    
                    images.Read(pdfContent, settings);    //I don't even get exception in this line when it is in deployed state.

                    int count = 1;

                    string FolderName = guidObj.ToString();

                    foreach (var image in images)
                    {
                        var memstream = new MemoryStream();
                        image.Write(memstream, MagickFormat.Jpeg);
                        memstream.Position = 0;

                        string localPath = FolderName + "-page-" + count + ".jpg";

                        storage.UploadObject(bucketName, FolderName + "/" + localPath, null, memstream);

                        string Url = cloudStorageURL + bucketName + "/" + FolderName + "/" + localPath;
                        if (!string.IsNullOrEmpty(ImageLinks))
                        {
                            ImageLinks += " | " + Url;
                        }
                        else
                        {
                            ImageLinks = Url;
                        }
                        count++;
                    }
                    if (images.Count > 0)
                    {
                        bool status = TableInsertRows(InvoiceId, ClientReference, FolderName, ImageLinks);
                        if (status)
                        {
                            return Ok(new { status = true, message = "Image conversion success", result = ImageLinks });
                        }
                        else
                        {
                            return Ok(new { status = false, message = "Failed to store image conversion results in table" });
                        }
                    }
                    else
                    {
                        return Ok(new { status = false, message = "Cannot read images properly" });
                    }
                }
            }
            catch (Exception ex)
            {
                return Ok(new { status = false, message = ex.Message.ToString() });
            }
        }
        else
        {
            return Ok(new { status = false, message = "Pdf url is required as input" });
        }
    }

Kindly suggest me a solution and correct me if my development is wrong.

Thanks and Regards, S,Thilak

Thilaksiva avatar Jul 17 '20 13:07 Thilaksiva

You will need to install Ghostscript in your container. And I suspect that you have installed Ghostscript on your local machine and that is why it works on your development machine.

p.s. You need a commercial license for Ghostscript if you are planning to use this in a commercial product.

dlemstra avatar Jul 17 '20 13:07 dlemstra