【发布时间】: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