HTML -> PDF Converter Fails In Linux Environment

Package is running in Azure and works fine except for the HTML converter which reports:

System.Drawing.Common is not supported on non-Windows platforms. See Breaking change: System.Drawing.Common only supported on Windows - .NET | Microsoft Learn for more information.

Is there some setting or configuration that we are not using properly.

@srmorley

The error you’re encountering indicates that System.Drawing.Common is not supported on non-Windows platforms, which is a common issue when running applications in Linux environments, such as Azure.

To resolve this, consider the following options:

  1. Use a Different Rendering Engine: Instead of relying on System.Drawing, utilize a rendering engine that is compatible with Linux. Aspose.PDF for .NET does not require System.Drawing for HTML to PDF conversion, so ensure you are using the latest version of Aspose.PDF.

  2. Check Your Code: Review your code to ensure it does not explicitly use any System.Drawing classes or methods. If it does, you will need to refactor it to eliminate those dependencies.

  3. Use Docker: If you are deploying in Azure, consider using a Docker container based on a Windows image if your application heavily relies on System.Drawing. This allows you to run your application in a Windows environment while still utilizing Azure services.

  4. Explore Alternative Libraries: If the issue persists, consider exploring other libraries or services that can handle HTML to PDF conversion without relying on System.Drawing.

For further guidance on converting documents in Azure using Aspose.PDF, please consult the official documentation.

If that is the case then why is it doing so in this application.

We are using
Aspose.PDF 25.9.0
Aspose.HTML 25.8.0
Aspose.Cells 25.9.0

namespace ConversionLibrary.Converters
{
internal class ConvertHtmlDocument : IConvertDocument
{
public async Task Convert(Stream inputStream, Stream outputStream, ILogger logger, string fileName)
{
try
{
//using var streamProvider = new OutputStreamProvider(outputStream);
using var streamProvider = new MemoryStreamProvider();
var htmlDoc = new HTMLDocument(inputStream, “.” );

            var x = new Aspose.Html.Saving.PdfSaveOptions();
            x.DocumentInfo.CreationDate = DateTime.UtcNow;
            x.DocumentInfo.Producer = "CMG Document Converter";
            x.DocumentInfo.Title = fileName;
            x.DocumentInfo.Author = "";
            x.DocumentInfo.Subject = "";

            Aspose.Html.Converters.Converter.ConvertHTML(htmlDoc, x, streamProvider);

            var memory = streamProvider.Streams.First();
            memory.Seek(0, SeekOrigin.Begin);
            await memory.CopyToAsync(outputStream);
            await outputStream.FlushAsync();

            return null;
        }
        catch( Exception ex )
        {
            logger.LogError(ex, "Exception During HTML Document Conversion");
            return "Exception";
        }
    }
}

}

Looking around I found another approach suggested by your website which avoid the user of Aspose.HTML. This threw the same exception.

    public async Task<string> Convert(Stream inputStream, Stream outputStream, ILogger logger, string fileName)
    {
        try
        {
            var options = new HtmlLoadOptions
            {
                // Set Print or Screen mode
                HtmlMediaType = Aspose.Pdf.HtmlMediaType.Print
            };

            var document = new Aspose.Pdf.Document(inputStream, options);
            document.Metadata["Producer"] = "CMG Document Converter";
            document.Save(outputStream);

            return null;
        }
        catch( Exception ex )
        {
            logger.LogError(ex, "Exception During HTML Document Conversion");
            return "Exception";
        }
    }

@srmorley

Please note that we have published Aspose.PDF.Drawing for .NET and Aspose.HTML.Drawing for .NET APIs separately to work in non-Windows environments. We request you please uninstall existing packages and install above mentioned packages from NuGet Package Manager. In case you still notice any errors while using these packages, please let us know.

I added Aspose.PDF.Drawing which did not resolve the issue. Added Aspose.Drawing which also failed to fix things.

<PackageReference Include="Aspose.Cells" Version="25.9.0" />
<PackageReference Include="Aspose.Drawing" Version="25.9.0" />
<PackageReference Include="Aspose.PDF" Version="25.9.0" />
<PackageReference Include="Aspose.PDF.Drawing" Version="25.9.0" />
<PackageReference Include="Aspose.Words" Version="25.9.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="2.88.8" />

Exception details are in the attached image.
image.png (22.2 KB)

Got it!

Aspose.PDF.Drawing instead of Aspose.PDF

Working now. Yeah. Thanks.

@srmorley

It is nice to know that things have started working at your end. Please keep using the API and feel free to create a new topic in case you need any kind of assistance.