|
@@ -1,10 +1,19 @@
|
|
|
-using System.Drawing.Printing;
|
|
|
|
|
-using System.Net.Sockets;
|
|
|
|
|
-using System.Net;
|
|
|
|
|
-using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
+using Microsoft.AspNetCore.Mvc;
|
|
|
using Spire.Pdf;
|
|
using Spire.Pdf;
|
|
|
|
|
+using System.Drawing.Printing;
|
|
|
|
|
+using System.Net;
|
|
|
|
|
+using System.Net.Sockets;
|
|
|
|
|
+using System.Text;
|
|
|
namespace printservice.Controllers
|
|
namespace printservice.Controllers
|
|
|
{
|
|
{
|
|
|
|
|
+
|
|
|
|
|
+ public class PrintTextRequest
|
|
|
|
|
+ {
|
|
|
|
|
+ public required string PrinterIp { get; set; }
|
|
|
|
|
+ public required string Text { get; set; }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
[ApiController]
|
|
[ApiController]
|
|
|
[Route("api/printer")]
|
|
[Route("api/printer")]
|
|
|
public class PrinterController : ControllerBase
|
|
public class PrinterController : ControllerBase
|
|
@@ -51,6 +60,46 @@ namespace printservice.Controllers
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ [HttpPost("open-drawer")]
|
|
|
|
|
+ public IActionResult OpenDrawer([FromBody] string printerIp)
|
|
|
|
|
+ {
|
|
|
|
|
+ try
|
|
|
|
|
+ {
|
|
|
|
|
+ using (var client = new TcpClient(printerIp, 9100)) // Puerto común para impresoras ESC/POS
|
|
|
|
|
+ using (var stream = client.GetStream())
|
|
|
|
|
+ {
|
|
|
|
|
+ // Comando para abrir la gaveta
|
|
|
|
|
+ byte[] command = new byte[] { 0x1B, 0x70, 0x00, 0x19, 0xFA };
|
|
|
|
|
+ stream.Write(command, 0, command.Length);
|
|
|
|
|
+ }
|
|
|
|
|
+ return Ok("Gaveta abierta correctamente.");
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (Exception ex)
|
|
|
|
|
+ {
|
|
|
|
|
+ _logger.LogError(ex, "Error al abrir la gaveta: {Message}", ex.Message);
|
|
|
|
|
+ return StatusCode(500, "Error interno del servidor al abrir la gaveta.");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ [HttpPost("print-text")]
|
|
|
|
|
+ public IActionResult PrintText([FromBody] PrintTextRequest request)
|
|
|
|
|
+ {
|
|
|
|
|
+ try
|
|
|
|
|
+ {
|
|
|
|
|
+ using (var client = new TcpClient(request.PrinterIp, 9100)) // Puerto común para impresoras ESC/POS
|
|
|
|
|
+ using (var stream = client.GetStream())
|
|
|
|
|
+ {
|
|
|
|
|
+ byte[] textBytes = Encoding.ASCII.GetBytes(request.Text + "\n");
|
|
|
|
|
+ stream.Write(textBytes, 0, textBytes.Length);
|
|
|
|
|
+ }
|
|
|
|
|
+ return Ok("Texto enviado a imprimir correctamente.");
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (Exception ex)
|
|
|
|
|
+ {
|
|
|
|
|
+ _logger.LogError(ex, "Error al imprimir texto.");
|
|
|
|
|
+ return StatusCode(500, "Error interno del servidor al imprimir texto.");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
[HttpPost("print-ticket")]
|
|
[HttpPost("print-ticket")]
|
|
|
public IActionResult PrintTicket(IFormFile file, [FromForm] string printerName)
|
|
public IActionResult PrintTicket(IFormFile file, [FromForm] string printerName)
|
|
@@ -94,6 +143,26 @@ namespace printservice.Controllers
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ [HttpPost("cut-paper")]
|
|
|
|
|
+ public IActionResult CutPaper([FromBody] string printerIp)
|
|
|
|
|
+ {
|
|
|
|
|
+ try
|
|
|
|
|
+ {
|
|
|
|
|
+ using (var client = new TcpClient(printerIp, 9100)) // Puerto común para impresoras ESC/POS
|
|
|
|
|
+ using (var stream = client.GetStream())
|
|
|
|
|
+ {
|
|
|
|
|
+ // Comando para cortar papel
|
|
|
|
|
+ byte[] cutCommand = new byte[] { 0x1D, 0x69 }; // Comando ESC/POS para cortar papel
|
|
|
|
|
+ stream.Write(cutCommand, 0, cutCommand.Length);
|
|
|
|
|
+ }
|
|
|
|
|
+ return Ok("Papel cortado correctamente.");
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (Exception ex)
|
|
|
|
|
+ {
|
|
|
|
|
+ _logger.LogError(ex, "Error al cortar papel.");
|
|
|
|
|
+ return StatusCode(500, "Error interno del servidor al cortar papel.");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|