Aspose.HTML to PDF does not work on linux

Hello,
We are evaluating the application migration to Linux and are facing issue with the HTML to PDF functionality not working there.
To be more explicit, we are running code like below in Azure App Service for Linux:

        private Stream GenerateBodyStream(string html, string title)
        {
            var stream = new MemoryStream();

            using (var streamProvider = new StreamProvider())
            {
                using (var document = new HTMLDocument(html, "."))
                {
                    var options = new PdfSaveOptions();

                    options.PageSetup.AnyPage = new Page(
                        new Size(612, 792),
                        new Margin(0, 100, 0, 85));

                    options.DocumentInfo.Title = title;

                    Converter.ConvertHTML(document, options, streamProvider);

                    var memory = streamProvider.Streams.First();
                    memory.Seek(0, SeekOrigin.Begin);

                    memory.CopyTo(stream);
                }
            }

            stream.Seek(0, SeekOrigin.Begin);
            return stream;
        }

And get the exception:

System.TypeInitializationException: The type initializer for 'Gdip' threw an exception.
 ---> System.PlatformNotSupportedException: System.Drawing.Common is not supported on non-Windows platforms. See https://aka.ms/systemdrawingnonwindows for more information.
   at System.Drawing.SafeNativeMethods.Gdip.<>c.<.cctor>b__2_0(String _, Assembly _, Nullable`1 _)
   at System.Runtime.InteropServices.NativeLibrary.LoadLibraryCallbackStub(String libraryName, Assembly assembly, Boolean hasDllImportSearchPathFlags, UInt32 dllImportSearchPathFlags)
   at System.Drawing.SafeNativeMethods.Gdip.<GdiplusStartup>g____PInvoke|32_0(IntPtr* __token_native, StartupInputEx* __input_native, StartupOutput* __output_native)
   at System.Drawing.SafeNativeMethods.Gdip.GdiplusStartup(IntPtr& token, StartupInputEx& input, StartupOutput& output)
   at System.Drawing.SafeNativeMethods.Gdip..cctor()
   --- End of inner exception stack trace ---
   at System.Drawing.SafeNativeMethods.Gdip.GdipCreateMatrix2(Single m11, Single m12, Single m21, Single m22, Single dx, Single dy, IntPtr& matrix)
   at System.Drawing.Drawing2D.Matrix..ctor(Single m11, Single m12, Single m21, Single m22, Single dx, Single dy)
   at    ..ctor(Single , Single , Single , Single , Single , Single )
   at    .CreateMatrix(Single , Single , Single , Single , Single , Single )
   at    .BeginPage(SizeF )
   at Aspose.Html.Rendering.Device`2.BeginPage(SizeF size)
   at    .       (IDevice , CancellationToken )
   at ​  .       (Renderer ,    [] , IDevice , CancellationToken )
   at Aspose.Html.Rendering.HtmlRenderer.(IDevice , CancellationToken , Element[] , Document[] )
   at Aspose.Html.Rendering.HtmlRenderer.Render(IDevice device, TimeSpan timeout, HTMLDocument[] sources)
   at Aspose.Html.Rendering.Renderer`1.Render(IDevice device, TSource source, TimeSpan timeout)
   at Aspose.Html.Rendering.Renderer`1.Render(IDevice device, TSource source)
   at   .(   , IDevice )
   at   .      (   , PdfSaveOptions , ICreateStreamProvider )
   at Aspose.Html.Converters.Converter.ConvertHTML(HTMLDocument document, PdfSaveOptions options, ICreateStreamProvider provider)
   at WebApplication_forLinux.Controllers.TestController.GenerateBodyStream(String html, String title) in C:\src\WebApplication_forLinux\Controllers\TestController.cs:line 87
   at WebApplication_forLinux.Controllers.TestController.GetPdf() in C:\src\WebApplication_forLinux\Controllers\TestController.cs:line 46

Full sample solution attached below. Can you please assist?
aspose_linux.zip (3.3 КБ)

@shtaff

It seems that you are encountering a System.PlatformNotSupportedException when trying to use the HTML to PDF conversion functionality in Aspose.HTML on a Linux environment. This issue arises because the System.Drawing.Common library, which is used for graphics operations, is not supported on non-Windows platforms.

To resolve this issue, you can consider the following options:

  1. Use a Different Rendering Engine: If your application relies on features from System.Drawing.Common, you might need to switch to a different rendering engine that is compatible with Linux. Aspose.HTML does not require System.Drawing.Common for basic HTML to PDF conversion, so ensure that your code does not inadvertently use any features that depend on it.

  2. Check for Dependencies: Review your project dependencies to ensure that none of them are pulling in System.Drawing.Common. If they are, you may need to find alternatives or modify your code to avoid using those dependencies.

  3. Use Docker or a Windows Environment: If you must use features that require System.Drawing.Common, consider running your application in a Windows-based Docker container or on a Windows server.

  4. Update Your Code: Ensure that your code is using the latest version of Aspose.HTML, as updates may include fixes or improvements for cross-platform compatibility.

Here is a simplified example of how to convert HTML to PDF without relying on System.Drawing.Common:

using Aspose.Html;
using Aspose.Html.Saving;

public void ConvertHtmlToPdf(string htmlContent, string outputPath)
{
    using (var document = new HTMLDocument(htmlContent, "."))
    {
        var options = new PdfSaveOptions();
        Converter.ConvertHTML(document, options, outputPath);
    }
}

Make sure to replace htmlContent with your actual HTML string and outputPath with the desired output file path.

If you continue to face issues, please provide more details about your environment and any additional error messages you may encounter.

We are not explicitly using anything that would point to System.Drawing. Will need more context on “Changing the rendering engine” or any additional help with updating the sample we provided to properly work in Linux environment. Thanks.

@shtaff

We have released Aspose.HTML.Drawing for .NET which is not dependent on System.Drawing. It can be downloaded here NuGet Gallery | Aspose.HTML.Drawing 25.5.0. Please download its latest available version and use it. In case you face any issues, please let us know.

1 Like

Hello @asad.ali
We tried to install it alongside the Aspose.Html similar to how SkiaSharp dependencies are used for other Aspose products on Linux, but that did not help.
Do you mean that we need to use Aspose.Html.Drawing instead of Aspose.Html, and not alongside?
We’ll try that out tomorrow, thank you.

@asad.ali thanks a lot, from a first glance replacing Aspose.Html with Aspose.Html.Drawing works well.

@shtaff

Its 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 further assistance.