.NET Core System.Drawing.Common PrintDocument doesn't work on Linux

3.4k Views Asked by At

I have a problem with PrintDocument from System.Drawing.Common when I use this on Linux, to be more specific, the problem comes from PrintingServices.LoadPrinterSettings, which is using in PrinterSettings.Unix

here the content of the error:

Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
An unhandled exception has occurred while executing the request. System.NullReferenceException: Object reference not set to an instance of an object. at System.Drawing.Printing.PrintingServices.LoadPrinterSettings(String printer, PrinterSettings settings)

On Windows everything working correctly, because PrintDocument uses solution prepare for Windows.

Code:

PrintDocument pd = new PrintDocument();
pd.PrinterSettings.PrinterName = printerName;
pd.OriginAtMargins = true;
pd.DefaultPageSettings.Landscape = true;
pd.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
pd.PrintPage += new PrintPageEventHandler(Pd_PrintPage);
pd.Print();

I'm using:

System.Drawing.common (4.5.1)

.NET Core 2.1

libc6-dev and libgdiplus have been installed on Linux

Linux - Debian 9

Anyone can help?

1

There are 1 best solutions below

2
On

This is most likely an issue in System.Drawing.Common. The printing code on Linux has been ported from Mono and hasn't received a lot of attention.

If you can create a 'minimal repo', I think the best option is to file an issue at https://github.com/dotnet/corefx (or even better, send a PR).