Search code examples
c#pdfdevexpressskiasharpimage-conversion

Convert a PDF to an Image in Azure Web App


Hi I have been trying to convert a pdf to an image, and it works on my local host, but it is not working when published to the cloud.

I have been trying to use Devexpress's PDFDocument Processor, but this only works for local host. below is some code that is working on localhost but not Azure web app.

public static byte[] streamResult(PdfDocumentProcessor processor, MemoryStream ms)
        {
            MemoryStream stream = new MemoryStream();
            processor.RenderingEngine = PdfRenderingEngine.Skia;
            processor.LoadDocument(ms);
            for (int i = 1; i <= processor.Document.Pages.Count; i++)
            {
                //Bitmap image = processor.CreateBitmap(i, 1000);

                PdfRectangle cropBox = processor.Document.Pages[0].CropBox;


                int actualPageHeight = DevExpress.Office.Utils.Units.PointsToPixels(11 * 72, 96);
                int actualPageWidth = DevExpress.Office.Utils.Units.PointsToPixels((Convert.ToInt32(8.5 * 72)), 96);
                int cropBoxPageHeight = DevExpress.Office.Utils.Units.PointsToPixels((int)cropBox.Height, 96);
                int cropBoxPageWidth = DevExpress.Office.Utils.Units.PointsToPixels((int)cropBox.Width, 96);
                //Image image = new System.Drawing.Bitmap(processor.CreateBitmap(i, cropBoxPageHeight));
                //image.Save(stream, System.Drawing.Imaging.ImageFormat.Png);

                new System.Drawing.Bitmap(processor.CreateBitmap(i, cropBoxPageHeight), actualPageWidth, 1030).Save(stream, System.Drawing.Imaging.ImageFormat.Png);
            }

               
            stream.Position = 0;

            //return new FileStreamResult(stream, "Png");
            return stream.ToArray();
        }

According to Devexpress if I set the PDFRenderingEngine to Skia this should work, but I have gone back and forth for about a month, and tried everything I can think of. I have installed the nugget packages necesarry such as the Devexpress.PDF.SkiaRenderer and the SkiaSharp library.

When I try to convert the PDF to an image on Azure I get a bunch of tiny little black boxes. and the Byte Array is completely different then what it was on localhost.

I have also tried to use the PDFium nugget package but I could not get this working. I was wondering if anyone might be able to find something I am doing wrong in my code, or if there is an alternative solution.

I understand that the BITMap defaults to GDI+ which I don't believe works with azure, but from what Devexpress says, the If I change the Render Engine to Skia and utilize their CreateBitmap then this should work.

Open to any ideas.

Thanks,

Here is the code with the ImageCodecInfo Image Encoders, and the EncoderParameters

var encoder = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
                    List<System.Drawing.Imaging.ImageCodecInfo> list = new List<System.Drawing.Imaging.ImageCodecInfo>();
                    
                    Guid variable = new Guid();
                    foreach (System.Drawing.Imaging.ImageCodecInfo code in encoder)
                    {
                        if (code.FormatID == System.Drawing.Imaging.ImageFormat.Png.Guid)
                        {
                            variable = code.FormatID;
                            list.Add(code);
                        }
                    }
                  
                    var encParams1 = new System.Drawing.Imaging.EncoderParameters() { Param = new[] { new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 90L) } };
                   
                    new System.Drawing.Bitmap(processor.CreateBitmap(i, cropBoxPageHeight), actualPageWidth, 1030).Save(stream, list.Find(x => x.FormatID == variable), encParams1);


Solution

  • After more than a month I went a different route. I moved this code inside an HTTP trigger azure function and it began to work properly. I think this might just be an issue with web apps in DevExpress, and with it being so new, I don't believe they know about this bug.

    I will continue to work with them to find out if I can provide information to where this doesn't need an azure function to make it work.