Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A ESC/POS Printer Commands Helper

NotificationsYou must be signed in to change notification settings

igorocampos/ESCPOS

Repository files navigation

SourceLinkBuild StatusGitHub code size in bytesNuGetGitHubFOSSA Status

ESCPOS

A ESC/POS Printer Commands Helper.

Installing via NuGet Package

The NuGet Package can be foundhere and you can install it with:

PM>Install-Package ESCPOS

Usage

All command methods will return abyte array that you should concatenate with the bytes of your data, and then send it all to your printer using thePrint extension method, which will send a byte array to the informed printer address. It can be something likeCOM3,LPT1,\\127.0.0.1\printer,192.168.0.100:9100, etc. or even a path to a text file like./print.txt.

There is also 2 extension methods,Add andToBytes, located in the namespaceESCPOS.Utils.The first one can be used in byte arrays, so you can concatenate 2 or more byte arrays just like this:

byte[]result=array1.Add(array2,array3, ...,arrayN);

In addition there's an overload to the mentionedAdd method that will accept string parameters instead of byte arrays. It appends all strings into one new string and then converts it to a byte array.And yet another overload to accept the mix of byte arrays and strings parameters, but since it's accepting an object type parameter, this will ignore any parameter that is notstring orbyte[] (e.g.int).

WithToBytes method you can convert a UTF-8 string to a byte array:

byte[]result="Some string".ToBytes();

Alternatively you can choose whatever Encoding you wish to use for that:

byte[]result="汉字".ToBytes(Encoding.GetEncoding("GBK"));

*Just make sure your Printer has a corresponding CodePage for that Encoding!

Printer Class

As an alternative, if you'd like to instantiate a Printer class that will handle all about bytes, encoding, and even have printer specific features like an horizontal line or print text aligned to the left AND to the right in the same line, which is only possible by knowing the number of columns your printer has, you can use thePrinter class and its methods.

//Instantiate printer with its details for encoding, column count and addressvarprinter=newPrinter{Encoding=Encoding.UTF8,Columns=32,Address="COM4"};//Add all bytes to the Cacheprinter.AddToCache(printer.HorizontalLine,LF,printer.SameLineLeftAndRightAlignedText("Sample Product","$10.99"),LF,printer.HorizontalLine);//Send everything that is currently in the cache to the printerprinter.Print();

Examples

All examples will assume the using statements below:

usingstaticESCPOS.Commands;usingESCPOS;usingESCPOS.Utils;

QRCode

byte[]qrCodeCommand=QRCode("Some data");qrCodeCommand.Print("COM2");

Or using the Extension Method

stringdata="Some data";data.ToQRCode().Print("COM2");

Barcode

byte[]barCodeCommand=Barcode(BarCodeType.EAN13,"9780201379624");barCodeCommand.Print("192.168.0.100:9100");

Or using the Extension Method

stringcode="9780201379624";code.ToBarcode(BarCodeType.EAN13).Print("192.168.0.100:9100");

Formatted Text

byte[]cmd=AlignToCenter.Add(CharSizeDoubleHeight,"Fancy Title");cmd.Print(@"\\127.0.0.1\printer");

Example using Printer class

varprinter=newPrinter{Encoding=Encoding.UTF8,Columns=32,Address="192.168.0.100:9100"};printer.AddToCache(printer.HorizontalDoubleLine,LF,printer.SameLineLeftAndRightAlignedText("Product Name","Price"),LF,printer.HorizontalLine,LF,printer.SameLineLeftAndRightAlignedText("Sample Product","$10.99"),LF,printer.SameLineLeftAndRightAlignedText("Sample Product with a very long description","$0.01"),LF,AlignToRight,"----------",LF,CharSizeDoubleHeight,"$11.00",CharSizeReset,LF,"----------",AlignToLeft,LF,printer.HorizontalDoubleLine);printer.Print();

Full CFe SAT Receipt without using Printer class

This example will assume that the variablecfe is a deserialized object from theCFe XML, and will print the receipt using its fields.Also this example will print a 32 columns receipt, which is ideal for 56mm paper roll.

varline="--------------------------------";byte[]array=LF;array=array.Add(CharSizeDoubleHeight,AlignToCenter);if(cfe.infCFe.emit.xFant!=null)array.Add(cfe.infCFe.emit.xFant);array.Add(LF,CharSizeReset,cfe.infCFe.emit.xNome,LF,$"{cfe.infCFe.emit.enderEmit.xLgr},{cfe.infCFe.emit.enderEmit.nro}{cfe.infCFe.emit.enderEmit.xBairro} -{cfe.infCFe.emit.enderEmit.xMun}{cfe.infCFe.emit.enderEmit.CEP}",LF,$"CNPJ:{cfe.infCFe.emit.CNPJ}",LF,$"IE:{cfe.infCFe.emit.IE}",LF,line,CharSizeDoubleHeight,$"Extrato No.{cfe.infCFe.ide.nCFe}",LF,"CUPOM FISCAL ELETRONICO - SAT",CharSizeReset,LF,LF);if(!string.IsNullOrEmpty(cfe.infCFe.dest?.Item))array.Add(line,"CPF/CNPJ do Consumidor:",cfe.infCFe.dest.Item,LF);array.Add(line,"#|COD|DESC|QTD|UN|VL UNIT R$|(VL TRIB R$)*|VL ITEM R$",LF,line,AlignLeft);inti=1;foreach(vardetincfe.infCFe.det){stringprod=$"{det.prod.cProd}{det.prod.xProd}{det.prod.qCom:0.0##}{det.prod.uCom} X{det.prod.vUnCom:0.00#}{((det.imposto?.vItem12741??0)==0?"":$"({det.imposto.vItem12741:f2})*")}";array.Add($"{i++:D3} ");while(prod.Length>20){varwrap=prod.Length>=20?prod.Substring(0,20):prod;array.Add(wrap),LF, "     ");prod=prod.Substring(20);}array.Add(prod.PadRight(20),det.prod.vProd.ToString("f2").PadLeft(6),LF);}array.Add(LF);if(cfe.infCFe.total.ICMSTot.vDesc>0)array.Add($" Desconto R${cfe.infCFe.total.ICMSTot.vDesc.ToString("f2").PadLeft(19)}",LF);if(cfe.infCFe.total.ICMSTot.vOutro>0)array.Add($" Acrescimo R${cfe.infCFe.total.ICMSTot.vOutro.ToString("f2").PadLeft(18)}",LF);array.Add(CharSizeDoubleHeight,$" TOTAL R${cfe.infCFe.total.vCFe.ToString("f2").PadLeft(22)}",LF,CharSizeReset,LF);foreach(varmpincfe.infCFe.pgto.MP){stringdescription;switch(Convert.ToInt32(mp.cMP??"1")){case2:description="Cheque";break;case3:description="Cartao de Credito";break;case4:description="Cartao de Debito";break;case5:description="Credito na Loja";break;case10:description="Vale Alimentacao";break;case11:description="Vale Refeicao";break;case12:description="Vale Presente";break;case13:description="Vale Combustivel";break;case14:description="Duplicata Mercantil";break;case90:description="Sem Pagamento";break;default:description="Dinheiro";break;}array.Add($"{description.PadRight(18)}{mp.vMP.ToString("f2").PadLeft(12)}",LF);}StringaccessKey=cfe.infCFe.Id.Substring(3,44);array.Add($" Troco{cfe.infCFe.pgto.vTroco.ToString("f2").PadLeft(25)}",LF);foreach(varobsincfe.infCFe.infAdic.obsFisco)array.Add($"{obs.xCampo}-{obs.xTexto}",LF);array.Add(line,"OBSERVACOES DO CONTRIBUINTE",LF);foreach(varitemincfe.infCFe.infAdic.infCpl.Split(';'))array.Add(item,LF);array.Add(LF,line,CharSizeDoubleHeight,AlignTOCenter,$"SAT No.{cfe.infCFe.ide.nserieSAT}",LF,CharSizeReset,DateTime.ParseExact($"{cfe.infCFe.ide.dEmi}{cfe.infCFe.ide.hEmi}","yyyyMMdd HHmmss",System.Globalization.CultureInfo.InvariantCulture).ToString("dd/MM/yyyy HH:mm:ss"),LF,LF,CharSizeDoubleHeight,accessKey,LF,LF,accessKey.Substring(0,22).ToBarcode(BarCodeType.CODE128,30),accessKey.Substring(22).ToBarcode(BarCodeType.CODE128,30),LF,LF,$"{accessKey}|{cfe.infCFe.ide.dEmi}{cfe.infCFe.ide.hEmi}|{cfe.infCFe.total.vCFe}|{cfe.infCFe.dest?.Item??""}|{cfe.infCFe.ide.assinaturaQRCODE}".ToQRCode(),CharSizeReset,LF,line,LF,LF,LF);array.Print(@"\\127.0.0.1\printer");

Considerations

When printing CODE128 barcodes, it will use automatically subset B, which supports numbers, upper and lower case letters and some additional characters.

You can see the changeloghere.

License

FOSSA Status


[8]ページ先頭

©2009-2025 Movatter.jp