ESCPOS 1.1.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package ESCPOS --version 1.1.2                
NuGet\Install-Package ESCPOS -Version 1.1.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ESCPOS" Version="1.1.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ESCPOS --version 1.1.2                
#r "nuget: ESCPOS, 1.1.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ESCPOS as a Cake Addin
#addin nuget:?package=ESCPOS&version=1.1.2

// Install ESCPOS as a Cake Tool
#tool nuget:?package=ESCPOS&version=1.1.2                

Build Status GitHub code size in bytes NuGet GitHub FOSSA Status

ESCPOS

A ESC/POS Printer Commands Helper.

alternate text is missing from this package README image

Installing via NuGet Package

The NuGet Package can be found here and you can install it with:

PM> Install-Package ESCPOS

Usage

All command methods will return a byte array that you should concatenate with the bytes of your data, and then send it all to your printer.

The Print extension method will send a byte array to the informed printer address, which can be something like COM3, 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 an Add and ToBytes extension methods located in the namespace ESCPOS.Utils. The first one for byte arrays, you can use it to concatenate 2 or more byte arrays just like this:

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

The second for strings, you can use it to convert a UTF-8 string to a byte array:

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

Examples

All examples will assume the using statements below:

using static ESCPOS.Commands;
using ESCPOS;
using ESCPOS.Utils;

QRCode

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

Barcode

byte[] barCodeCommand = PrintBarCode(BarCodeType.EAN13, "9780201379624");
barCodeCommand.Print("LPT1");

Full CFe SAT Receipt

This example will assume that the variable cfe is a deserialized object from the CFe 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.

var line = "--------------------------------".ToBytes();
byte[] array = null;
array.Add(LF, SelectCharSizeHeight(CharSizeHeight.Double), SelectJustification(Justification.Center));
if (cfe.infCFe.emit.xFant != null)
    array.Add(cfe.infCFe.emit.xFant.ToBytes());

array.Add(LF, SelectCharSizeHeight(CharSizeHeight.Normal), cfe.infCFe.emit.xNome.ToBytes(),
          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}".ToBytes(),
          LF, $"CNPJ: {cfe.infCFe.emit.CNPJ}".ToBytes(),
          LF, $"IE: {cfe.infCFe.emit.IE}".ToBytes(),
          LF, line, SelectCharSizeHeight(CharSizeHeight.Double), $"Extrato No. {cfe.infCFe.ide.nCFe}".ToBytes(),
          LF, "CUPOM FISCAL ELETRONICO - SAT".ToBytes(), SelectCharSizeHeight(CharSizeHeight.Normal),
          LF, LF);

if (!string.IsNullOrEmpty(cfe.infCFe.dest?.Item))
    array.Add(line, "CPF/CNPJ do Consumidor:".ToBytes(), cfe.infCFe.dest.Item.ToBytes(), LF);

array.Add(line, "#|COD|DESC|QTD|UN|VL UNIT R$|(VL TRIB R$)*|VL ITEM R$".ToBytes(),
          LF, line, SelectJustification(Justification.Left));

int i = 1;
foreach (var det in cfe.infCFe.det)
{
    string prod = $"{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} ".ToBytes());
    while (prod.Length > 20)
    {
        var wrap = prod.Length >= 20 ? prod.Substring(0,20) : prod;
        array.Add(wrap).ToBytes(), LF, "     ".ToBytes());
        prod = prod.Substring(20);
    }
    array.Add(prod.PadRight(20).ToBytes(), det.prod.vProd.ToString("f2").PadLeft(6).ToBytes(), LF);
}

array.Add(LF);
if (cfe.infCFe.total.ICMSTot.vDesc > 0)
    array.Add($" Desconto R${cfe.infCFe.total.ICMSTot.vDesc.ToString("f2").PadLeft(19)}".ToBytes(), LF);

if (cfe.infCFe.total.ICMSTot.vOutro > 0)
    array.Add($" Acrescimo R${cfe.infCFe.total.ICMSTot.vOutro.ToString("f2").PadLeft(18)}".ToBytes(), LF);

array.Add(SelectCharSizeHeight(CharSizeHeight.Double), $" TOTAL R${cfe.infCFe.total.vCFe.ToString("f2").PadLeft(22)}".ToBytes(), LF,
          SelectCharSizeHeight(CharSizeHeight.Normal), LF);

foreach (var mp in cfe.infCFe.pgto.MP)
{
    string description;
    switch (Convert.ToInt32(mp.cMP ?? "1"))
    {
        case 2:
            description = "Cheque";
            break;
        case 3:
            description = "Cartao de Credito";
            break;
        case 4:
            description = "Cartao de Debito";
            break;
        case 5:
            description = "Credito na Loja";
            break;
        case 10:
            description = "Vale Alimentacao";
            break;
        case 11:
            description = "Vale Refeicao";
            break;
        case 12:
            description = "Vale Presente";
            break;
        case 13:
            description = "Vale Combustivel";
            break;
        case 14:
            description = "Duplicata Mercantil";
            break;
        case 90:
            description = "Sem Pagamento";
            break;
        default:
            description = "Dinheiro";
            break;
    }

    array.Add($" {description.PadRight(18)}{mp.vMP.ToString("f2").PadLeft(12)}".ToBytes(), LF);
}

String accessKey = cfe.infCFe.Id.Substring(3, 44);
array.Add($" Troco{cfe.infCFe.pgto.vTroco.ToString("f2").PadLeft(25)}".ToBytes(), LF);

foreach (var obs in cfe.infCFe.infAdic.obsFisco)
    array.Add($" {obs.xCampo}-{obs.xTexto}".ToBytes(), LF);

array.Add(line, "OBSERVACOES DO CONTRIBUINTE".ToBytes(), LF);
foreach (var item in cfe.infCFe.infAdic.infCpl.Split(';'))
    array.Add(item.ToBytes(), LF);

array.Add(LF, line, SelectCharSizeHeight(CharSizeHeight.Double), SelectJustification(Justification.Center), $"SAT No. {cfe.infCFe.ide.nserieSAT}".ToBytes(),
          LF, SelectCharSizeHeight(CharSizeHeight.Normal), DateTime.ParseExact($"{cfe.infCFe.ide.dEmi} {cfe.infCFe.ide.hEmi}", "yyyyMMdd HHmmss", System.Globalization.CultureInfo.InvariantCulture).ToString("dd/MM/yyyy HH:mm:ss").ToBytes(),
          LF, LF, SelectCharSizeHeight(CharSizeHeight.Double), accessKey.ToBytes(),
          LF, LF, PrintBarCode(BarCodeType.CODE128, accessKey.Substring(0, 22), 30), PrintBarCode(BarCodeType.CODE128, accessKey.Substring(22), 30),
          LF, LF,
          PrintQRCode($"{accessKey}|{cfe.infCFe.ide.dEmi}{cfe.infCFe.ide.hEmi}|{cfe.infCFe.total.vCFe}|{cfe.infCFe.dest?.Item ?? ""}|{cfe.infCFe.ide.assinaturaQRCODE}"),
          SelectCharSizeHeight(CharSizeHeight.Normal),
          LF, line, LF, LF, LF);

array.Print(@"\\127.0.0.1\printer");

Considerations

This library will only accept UTF8 Encoding for Barcodes and QRCodes data.

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 changelog here.

License

FOSSA Status

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on ESCPOS:

Package Downloads
DemoPOS

Lightweight and easy to use fluent-style usb thermal printer wrapper based on ESCPOS and RawPrint

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.4 870 1/23/2024
1.2.3 2,417 11/24/2022
1.2.2 1,621 4/28/2021
1.2.1 945 9/15/2020
1.2.0 766 3/18/2020
1.1.2 658 1/7/2020
1.1.1 588 12/19/2019
1.1.0 854 8/28/2019
1.0.3 534 8/27/2019
1.0.2 528 8/27/2019
1.0.1 525 8/27/2019
1.0.0 549 8/26/2019