PrinterController.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System.Drawing.Printing;
  2. using System.Net.Sockets;
  3. using System.Net;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Spire.Pdf;
  6. namespace printservice.Controllers
  7. {
  8. [ApiController]
  9. [Route("api/printer")]
  10. public class PrinterController : ControllerBase
  11. {
  12. private readonly ILogger<PrinterController> _logger;
  13. public PrinterController(ILogger<PrinterController> logger)
  14. {
  15. _logger = logger;
  16. }
  17. [HttpGet("list")]
  18. public IActionResult GetPrinters()
  19. {
  20. try
  21. {
  22. // Obtener la lista de impresoras instaladas
  23. var printers = PrinterSettings.InstalledPrinters.Cast<string>().ToList();
  24. if (printers.Count == 0)
  25. {
  26. return NotFound("No se encontraron impresoras instaladas.");
  27. }
  28. return Ok(printers);
  29. }
  30. catch (Exception ex)
  31. {
  32. _logger.LogError(ex, "Error al obtener la lista de impresoras.");
  33. return StatusCode(500, "Error interno del servidor al listar impresoras.");
  34. }
  35. }
  36. [HttpGet("get-local-ip")]
  37. public IActionResult GetLocalIP()
  38. {
  39. var hostName = Dns.GetHostName();// Nombre de la PC
  40. var localIP = Dns.GetHostAddresses(hostName)
  41. .Where(ip => ip.AddressFamily == AddressFamily.InterNetwork)//Obtener solo IPv4
  42. .Select(ip => ip.ToString());
  43. return Ok(new
  44. {
  45. machineName = hostName,
  46. localIPs = localIP
  47. });
  48. }
  49. [HttpPost("print-ticket")]
  50. public IActionResult PrintTicket(IFormFile file, [FromForm] string printerName)
  51. {
  52. try
  53. {
  54. if (file == null || file.Length == 0)
  55. {
  56. return BadRequest("No se envió ningún archivo.");
  57. }
  58. if (string.IsNullOrEmpty(printerName))
  59. {
  60. return BadRequest("No se ingreso nombre de impresora");
  61. }
  62. // Guardar temporalmente el archivo
  63. var tempFilePath = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}.pdf ");
  64. using (var stream = new FileStream(tempFilePath, FileMode.Create))
  65. {
  66. file.CopyTo(stream);
  67. }
  68. // Cargar el PDF
  69. var pdfDocument = new PdfDocument();
  70. pdfDocument.LoadFromFile(tempFilePath);
  71. // Configurar la impresora
  72. pdfDocument.PrintSettings.PrinterName = printerName;
  73. pdfDocument.PrintSettings.SelectPageRange(1, pdfDocument.Pages.Count);
  74. pdfDocument.PrintSettings.SelectSinglePageLayout(Spire.Pdf.Print.PdfSinglePageScalingMode.FitSize);
  75. // Enviar a imprimir
  76. pdfDocument.Print();
  77. // Liberar recursos
  78. pdfDocument.Close();
  79. // Eliminar el archivo temporal
  80. System.IO.File.Delete(tempFilePath);
  81. return Ok("PDF enviado a imprimir correctamente.");
  82. }
  83. catch (Exception ex)
  84. {
  85. return StatusCode(500, $"Error al imprimir el PDF: {ex.Message}");
  86. }
  87. }
  88. }
  89. }