PDFtoImage处理Azure Blob流时抛出SEHException问题排查
- 直接将Azure Blob存储返回的PDF流传入
Conversion.ToImages方法执行转图片操作时,会抛出异常导致转换失败;但先将该流复制到本地文件系统,再使用FileStream作为输入则转换成功 - 此问题仅在处理特定PDF文件时出现
- 测试了基于pdfium引擎的两个库:PDFtoImage和Syncfusion.PdfToImageConverter.Net,均出现相同错误
示例代码(使用PDFtoImage库)
public async Task<List<SKBitmap>> TestConvertPdfToImagesAsync() { var blobServiceClient = new BlobServiceClient("?"); BlobClient blobClient = blobServiceClient .GetBlobContainerClient("test") .GetBlobClient("map.pdf"); var bitmaps = new List<SKBitmap>(); using (Stream pdfStream = await blobClient.OpenReadAsync()) { foreach (SKBitmap bitmap in Conversion.ToImages(pdfStream, true, null, new RenderOptions { Dpi = 220 })) { bitmaps.Add(bitmap); } } return bitmaps; }
抛出的异常信息
System.Runtime.InteropServices.SEHException (0x80004005): 外部组件抛出了异常。
at PDFtoImage.Internals.NativeMethods.Imports.FPDF_LoadPage(IntPtr document, Int32 page_index)
at PDFtoImage.Internals.NativeMethods.FPDF_LoadPage(IntPtr document, Int32 page_index)
at PDFtoImage.Internals.PdfFile.PageData..ctor(IntPtr document, IntPtr form, Int32 pageNumber)
at PDFtoImage.Internals.PdfFile.RenderPDFPageToBitmap(Int32 pageNumber, IntPtr bitmapHandle, Int32 boundsOriginX, Int32 boundsOriginY, Int32 boundsWidth, Int32 boundsHeight, Int32 rotate, FPDF flags, Boolean renderFormFill)
at PDFtoImage.Internals.PdfDocument.RenderSubset(PdfFile file, Int32 page, Single width, Single height, PdfRotation rotate, FPDF flags, Boolean renderFormFill, SKColor backgroundColor, Nullable1 bounds, Single originalWidth, Single originalHeight, CancellationToken cancellationToken) at PDFtoImage.Internals.PdfDocument.Render(Int32 page, Nullable1 requestedWidth, Nullable1 requestedHeight, Single dpiX, Single dpiY, PdfRotation rotate, FPDF flags, Boolean renderFormFill, SKColor backgroundColor, Nullable1 bounds, Boolean useTiling, Boolean withAspectRatio, Boolean dpiRelativeToBounds, CancellationToken cancellationToken)
at PDFtoImage.Conversion.RenderImpl(PdfDocument pdfDocument, Int32 page, FPDF renderFlags, RenderOptions options)
at PDFtoImage.Conversion.ToImagesImpl(PdfDocument pdfDocument, RenderOptions options, IEnumerable1 pages)+MoveNext() at PDFtoImage.Conversion.ToImagesImpl(Stream pdfStream, Boolean leaveOpen, String password, RenderOptions options, IEnumerable1 pages)+MoveNext()
at PDFtoImage.Conversion.ToImages(Stream pdfStream, IEnumerable`1 pages, Boolean leaveOpen, String password, RenderOptions options)+MoveNext()
at Biometric.Tests.Tests.PdfToImageCovert()
内容的提问来源于stack exchange,提问作者Linas

