【问题标题】:Convert a PDF to an Image in Azure Web App在 Azure Web App 中将 PDF 转换为图像
【发布时间】:2021-05-26 20:20:56
【问题描述】:

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

我一直在尝试使用 Devexpress 的 PDF 文档处理器,但这仅适用于本地主机。下面是一些适用于 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,这应该可以工作,但我已经来回走了大约一个月,并尝试了我能想到的一切。我已经安装了必要的 nugget 包,例如 Devexpress.PDF.SkiaRenderer 和 SkiaSharp 库。

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

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

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

欢迎任何想法。

谢谢,

这是带有 ImageCodecInfo 图像编码器和 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);

【问题讨论】:

  • 也许您可以尝试使用 docker,然后部署到 Azure Web App Containers。您可以选择 Windows 或 Linux 容器。
  • 我明天将尝试他们的免费试用似乎接近我需要的东西,谢谢您指出这一点。我以前从未听说过它们!
  • 不客气。我用过几次,效果很好。
  • 出于好奇,如果从 Bitmap.Save 方法中删除 System.Drawing.Imaging.ImageFormat.Png 并将其保存为标准位图会发生什么?
  • 让我试试,我会告诉你结果

标签: c# pdf devexpress skiasharp image-conversion


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2017-06-15
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-01
    • 1970-01-01
    • 2022-06-13
    • 1970-01-01
    相关资源
    最近更新 更多