简体   繁体   中英

System.Drawing.Printing in .NET Core 3.1 on Custom Label Size (Brother QL-570)

I'm currently trying to print from a .NET Core 3.1 Console App on a Brother QL-570 Label Printer on Linux.

My Code is the following:

    class Program
    {
        static void Main(string[] args)
        {
            var printer = args.Length > 0 ? args[0] : "Brother QL-570";

            using (PrintDocument pd = new PrintDocument())
            {
                var s = new PaperSize("Custom", 244, 300);
                s.RawKind = 256;
                pd.PrinterSettings.PrinterName = printer;
                pd.DefaultPageSettings.PaperSize = s;
                pd.DefaultPageSettings.Margins = new Margins(12, 12, 12, 12);
                pd.DefaultPageSettings.Landscape = false;
                pd.OriginAtMargins = true;
                pd.DocumentName = "Label";
                pd.OriginAtMargins = true;


                Console.WriteLine(pd.DefaultPageSettings.PaperSize);
                Console.ReadLine();

                pd.PrintPage += Pd_PrintPage;


                if (!pd.PrinterSettings.IsValid)
                {
                    Console.WriteLine("Invalid printer settings.");
                    return;
                }

                pd.Print();

            }

            Console.ReadLine();
        }

        private static void Pd_PrintPage(object sender, PrintPageEventArgs e)
        {
            Console.WriteLine($"Res: {e.PageSettings.PrinterResolution}");
            Console.WriteLine($"pa: {e.PageSettings.PrintableArea}");
            Console.WriteLine($"bounds: {e.PageBounds}");
            Console.WriteLine($"mb: {e.MarginBounds}");
            Console.WriteLine(e.PageSettings.PaperSize);

            e.HasMorePages = false;
            var b = e.MarginBounds;

            e.Graphics.PageUnit = GraphicsUnit.Display;
            
            e.Graphics.DrawRectangle(new Pen(Brushes.Black, 12), b.X, b.Y, b.Width, b.Height);
        }
    }

(When trying to run, pass your printer name as first command line argument)

The aim is to print a 62mm wide and 3 inches (76,2mm) long label on a 62mm endless roll. On this label, the outer print margins shall be drawn with a rectangle in black.

Situation:

  1. When executed on Windows, everything works fine. Even when connecting Windows to a Label Printer which is running on CUPS on my Raspberry PI, the label comes out fine, the output is as expected.
  2. When on Linux, things just do not work. The Printer blinks red, no label is printed. Interestingly, in the PrintPage-Event, the PrintMargins are empty on Linux (X=Y=Width=Height=0), whereas on Windows the correct values are filled in. The same applies for the hard margin properties.

I'm using the printer-drivers-ptouch package on my Raspberry PI as driver for the brother P-Touch QL-570. In .NET Core I use the System.Drawing.Common package to use the PrintDocument APIs.

Can you provide me with more information or details on the following?

  • Is this probably more a driver issue or a C# issue?
  • Is my way of specifying the custom paper size valid for Linux, too?

I had the same issue turns out that you need to open /etc/cups/ppd/Brother-(YOUR VERSION).ppd then add the custom feed you want to use to that file.. I wanted to use 29x90 and edited the file to read.. *PageSize 29x90/29mmx90(1.1"x3.5): after that I went to the printer settings and selected the correct page setting then application worked..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM