在 Azure Web 应用程序中将 PDF 转换为图像

问题描述

嗨,我一直在尝试将 pdf 转换为图像,它可以在我的本地主机上运行,​​但在发布到云端时却无法运行。

我一直在尝试使用 Devexpress 的 PDFDocument Processor,但这仅适用于本地主机。下面是一些适用于 localhost 但不适用于 Azure Web 应用的代码

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();
        }

根据 Devexpress,如果我将 PDFRenderingEngine 设置为 Skia,这应该可以工作,但我已经来回走了大约一个月,并尝试了我能想到的一切。我已经安装了必要的金块包,例如 Devexpress.PDF.SkiaRenderer 和 SkiaSharp 库。

当我尝试将 PDF 转换为 Azure 上的图像时,我得到一堆小黑盒子。并且字节数组与本地主机上的完全不同。

我也尝试过使用 PDFium nugget 包,但我无法使其正常工作。我想知道是否有人能够在我的代码中找到我做错的地方,或者是否有替代解决方案。

我知道 BITMap 认为 GDI+,我认为它不适用于 azure,但从 Devexpress 所说,如果我将渲染引擎更改为 Skia 并使用他们的 CreateBitmap,那么这应该可以工作。

对任何想法持开放态度。

谢谢,

这是带有 ImageCodecInfo 图像编码器和编码器参数的代码

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,list.Find(x => x.FormatID == variable),encParams1);

解决方法

一个多月后,我走了一条不同的路。我将这段代码移到 HTTP 触发器 azure 函数中,它开始正常工作。我认为这可能只是 DevExpress 中的网络应用程序的一个问题,而且由于它是如此新,我认为他们不知道这个错误。

我将继续与他们合作,以了解是否可以向不需要 azure 函数的地方提供信息。