Document does not import into Aspose.Words DOM using AWS Lambda Function

I have an api that takes in an IFormFile and converts it to a pdf, When running locally everything works but when hosed as a lambda function and API gateway the same file (.doc with 1 line of random text) returns a UnsupportedFileFormatException

   [Route("DocToPdf")]
[HttpPost]
public IActionResult DocToPdf(IFormFile file)
{
    if (file == null || file.Length == 0)
    {
        return BadRequest("Invalid File");
    }

    if (!file.ContentType.Contains(".document") &&
        file.ContentType != "application/msword")
    {
        return BadRequest("Unsupported File type");
    }

    byte[] pdf = _aspose.ConvertFileToPdf(file);

    return File(pdf, "application/pdf");
}

public byte[] ConvertFileToPdf(IFormFile file)
{
using (Stream stream = file.OpenReadStream())
{
return ConvertStreamToPdf(stream);
}
}

public byte[] ConvertStreamToPdf(Stream stream)
{
    var doc = new Document(stream);

    using (MemoryStream ms = new MemoryStream())
    {
        doc.Save(ms, SaveFormat.Pdf);

        return ms.ToArray();
    }
}

Aspose.Words.UnsupportedFileFormatException: Unsupported file format: Unknown at Aspose.Words.Document. (Stream , LoadOptions ) at Aspose.Words.Document. (Stream , LoadOptions ) at Aspose.Words.Document…ctor(Stream stream, LoadOptions loadOptions) at Aspose.Words.Document…ctor(Stream stream) at DocumentService.AsposeService.ConvertStreamToPdf(Stream stream) in C:\Repos\Codifi.Tools\DocumentService\DocumentService\DocumentService\AsposeService.cs:line 31 at DocumentService.AsposeService.ConvertFileToPdf(IFormFile file) in C:\Repos\Codifi.Tools\DocumentService\DocumentService\DocumentService\AsposeService.cs:line 25 at DocumentService.Controllers.ConvertController.DocToPdf(IFormFile file) in C:\Repos\Codifi.Tools\DocumentService\DocumentService\DocumentService\Controllers\ConvertController.cs:line 47 at lambda_method(Closure , Object , Object[] ) at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters) at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync() at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync() — End of stack trace from previous location where exception was thrown — at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync() — End of stack trace from previous location where exception was thrown — at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Logged|17_1(ResourceInvoker invoker) at Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger) at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context) HEADERS ======= Accept: / Accept-Encoding: gzip, deflate, br CloudFront-Forwarded-Proto: https CloudFront-Is-Desktop-Viewer: true CloudFront-Is-Mobile-Viewer: false CloudFront-Is-SmartTV-Viewer: false CloudFront-Is-Tablet-Viewer: false CloudFront-Viewer-Country: GB Content-Type: multipart/form-data; boundary=--------------------------…Host: …execute-api.eu-west-2.amazonaws.com Postman-Token: xxxx-f9e1-xxx-9298-xxxxxUser-Agent: PostmanRuntime/7.26.8 Via: 1.1 …cloudfront.net (CloudFront) X-Amz-Cf-Id: xs8c78-xxxxx== X-Amzn-Trace-Id: Root=1-5ff889da-42dd27ce5498ae817ee63b18 X-Forwarded-For: 31.49.171.96, 70.132.38.82 X-Forwarded-Port: 443 X-Forwarded-Proto: https Content-Length: 28658

@JamieMoff

In your case, Aspose.Words imports the document correctly at local system. So, the stream of document is incorrect when using lambda function. Please make sure that the file stream is correct. You can save the stream to disk using .NET API to check this issue.

If you still face problem, please attach the following resources here for testing:

  • Your input Word document.
  • Please create a simple Visual Studio project (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

Changing the api to take a byte[] rather than IFormFile worked. I was not able to work out why IFormFile was not working.

@JamieMoff

It is nice to hear from you that your problem has been solved. You need to use the correct stream into Document. You may use IFormFile.CopyTo(Stream) instead.

You check check the code example shared in the following link about usage of IFormFile.OpenReadStream.
How to read into memory the lines of a text file from an IFormFile in ASP.NET Core?