77 lines
2.6 KiB
C#
77 lines
2.6 KiB
C#
using Microsoft.Win32;
|
|
using NLog;
|
|
using System;
|
|
using System.IO;
|
|
using System.Windows.Forms;
|
|
|
|
namespace AIParkingApplication
|
|
{
|
|
public class Printer
|
|
{
|
|
private Logger appLogger;
|
|
|
|
public Printer(Logger appLogger)
|
|
{
|
|
this.appLogger = appLogger;
|
|
SetupPrinterPageSetting();
|
|
}
|
|
|
|
public void DoPrint(PrinterData printData)
|
|
{
|
|
WebBrowser webBrowser = new WebBrowser();
|
|
webBrowser.Left = 0;
|
|
string documentText = ProcessingString("PrinterForm.html", printData);
|
|
if (!string.IsNullOrEmpty(documentText))
|
|
{
|
|
webBrowser.DocumentText = documentText;
|
|
webBrowser.DocumentCompleted += WebBrowser_DocumentCompleted;
|
|
}
|
|
}
|
|
|
|
private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
|
|
{
|
|
((WebBrowser)sender).Print();
|
|
}
|
|
|
|
private string ProcessingString(string path, PrinterData printerField)
|
|
{
|
|
try
|
|
{
|
|
string htmlStr = File.ReadAllText(path);
|
|
htmlStr = htmlStr.Replace("{PLATE_STRING}", printerField.PlateString)
|
|
.Replace("{TIME_PARKING_IN}", printerField.TimeParkingIn)
|
|
.Replace("{TIME_PARKING_OUT}", printerField.TimeParkingOut)
|
|
.Replace("{MONEY_AMOUNT}", printerField.MoneyAmount);
|
|
return htmlStr;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"ProcessingString\texMessage:{ex.Message}");
|
|
appLogger.Log(LogLevel.Error, $"Lỗi khi In hóa đơn: {ex.Message}");
|
|
return string.Empty;
|
|
}
|
|
}
|
|
|
|
private void SetupPrinterPageSetting()
|
|
{
|
|
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer\PageSetup", RegistryKeyPermissionCheck.ReadWriteSubTree);
|
|
|
|
key.SetValue("footer", string.Empty, RegistryValueKind.String);
|
|
key.SetValue("header", string.Empty, RegistryValueKind.String);
|
|
key.SetValue("margin_top", 0);
|
|
key.SetValue("margin_right", 0.2);
|
|
key.SetValue("margin_bottom", 0.5);
|
|
key.SetValue("margin_left", 0.2);
|
|
key.Close();
|
|
}
|
|
}
|
|
|
|
public class PrinterData
|
|
{
|
|
public string PlateString { get; set; }
|
|
public string TimeParkingIn { get; set; }
|
|
public string TimeParkingOut { get; set; }
|
|
public string MoneyAmount { get; set; }
|
|
}
|
|
}
|