Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Conversion of documents with images to fixed page formats using Aspose.Words in Azure Functions does not work properly at deployment to Azure, having images not retained. However, such a conversion works properly on a local machine. The reason behind this problem is that SkiaSharp native assets are not properly published. The issue is reported to Azure in Github.
In order to resolve the problem, you can add the following section in the .csproj file that makes native assets copied properly:
<Target Name="CopyRequiredNativeAssets" AfterTargets="_FunctionsPostPublish">
<ItemGroup>
<NativeAssetToCopy Include="$(PublishDir)runtimes\win-x86\native\libSkiaSharp.dll" />
</ItemGroup>
<Copy SourceFiles="@(NativeAssetToCopy)" DestinationFolder="$(PublishDir)bin" />
</Target>
The following example shows how to use Aspose.Words in Azure Function with the details on how to add the code above.
You need to use Visual Studio to create an Azure Functions Application. The created application already has a simple “Hello World” function code.
In this example, you create a simple “Hello World” document and return it to the user’s browser as a PDF file. To accomplish this:
NuGet
reference to the latest version of Aspose.Words. Then modify the code as follows:using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Aspose.Words;
namespace AsposeWordsAzureTestApp
{
public static class CreateTestDocument
{
[FunctionName("CreateTestDocument")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
// Create a simple document using DocumentBuilder.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Write some text in the document.
builder.Writeln("Hello Aspose.Words!");
// Write OS we are running on.
builder.Writeln("You are running on " + System.Environment.OSVersion.VersionString);
// Insert some image into the document.
builder.InsertImage(@"http://cms.admin.containerize.com/templates/aspose/App_Themes/V3/images/aspose-logo.png");
// Now save the created document to PDF and return as a FileContentResult.
using (MemoryStream ms = new MemoryStream())
{
doc.Save(ms, SaveFormat.Pdf);
return new FileContentResult(ms.ToArray(), "application/pdf")
{
FileDownloadName = "out.pdf"
};
}
}
}
}
URL
from the console output to your favorite browser to get the output document, which should look as follows:<Target Name="CopyRequiredNativeAssets" AfterTargets="_FunctionsPostPublish"> <br> <ItemGroup> <br> <NativeAssetToCopy<br> Include="$(PublishDir)runtimes\win-x86\native\libSkiaSharp.dll" /> <br> </ItemGroup> <br> <Copy SourceFiles="@(NativeAssetToCopy)" <br> DestinationFolder="$(PublishDir)bin" /> <br> </Target>
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.