我有一个 .NET Core 6 web api,它使用 Office interop 打开 ppt 文档并将其转换为 pdf。 当我调试应用程序或将其作为独立应用程序发布并手动运行 .exe 时,该应用程序没有问题。 我需要在 Windows 服务器上的 IIS 上发布 Web API,但是当我尝试调用 API 端点时出现以下错误:“System.OutOfMemoryException:内存不足,无法继续执行程序”。
这是进行转换的方法,“pptApplication.Presentations.Open(originalPptPath, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse)”抛出异常
public static bool PowerPointToPdf(string originalPptPath, string pdfPath)
{
PowerPoint.Application pptApplication = null;
PowerPoint.Presentation pptPresentation = null;
object unknownType = Type.Missing;
var result = false;
//start power point
pptApplication = new PowerPoint.Application();
//open powerpoint document
pptPresentation = pptApplication.Presentations.Open(originalPptPath, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse);
if (pptPresentation == null)
return false;
pptPresentation.ExportAsFixedFormat(pdfPath, PowerPoint.PpFixedFormatType.ppFixedFormatTypePDF, PowerPoint.PpFixedFormatIntent.ppFixedFormatIntentPrint);
result = true;
// Close and release the Document object.
if (pptPresentation != null)
{
pptPresentation.Close();
ReleaseObject(pptPresentation);
pptPresentation = null;
}
pptApplication.Quit();
ReleaseObject(pptApplication);
pptApplication = null;
return result;
}
我已经尝试用管理员用户更改应用程序池身份,但没有任何改变。我知道微软不鼓励使用服务器端办公自动化,我已经评估了几个不使用办公自动化进行转换的库,但最终结果质量较差。