Skip to content

Commit

Permalink
add process print, use with foxit pdf reader.
Browse files Browse the repository at this point in the history
make printing process async
  • Loading branch information
Vlad Somai authored and Vlad Somai committed Jul 17, 2022
1 parent e028424 commit 4af235c
Showing 1 changed file with 9 additions and 20 deletions.
29 changes: 9 additions & 20 deletions Necta/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Windows.Threading;
using PuppeteerSharp;
using System.Diagnostics;
using System.Threading.Tasks;

namespace Necta
{
Expand Down Expand Up @@ -106,7 +107,7 @@ private void SaveAndValidateURI(bool isFirstPaint = false)
}
}

public static void PrintReceipt(Receipt receipt)
public static async void PrintReceipt(Receipt receipt)
{
NectaLogService.WriteLog("Setting default printer", LogLevels.INFO);
bool defaultPrinterResult = WinPrinter.SetDefaultPrinter(receipt.PrinterName);
Expand All @@ -119,7 +120,8 @@ public static void PrintReceipt(Receipt receipt)

try
{
ChromeConvertHtmlToPdf(receipt.HTML);
await ChromeConvertHtmlToPdf(receipt.HTML);
printingInProgress = false;
}
catch (Exception ex)
{
Expand Down Expand Up @@ -159,7 +161,7 @@ private void onResize(object sender, EventArgs e)
}
}

private static async void ChromeConvertHtmlToPdf(string html)
private static async Task<int> ChromeConvertHtmlToPdf(string html)
{
BrowserFetcher browserFetcher = new BrowserFetcher();
await browserFetcher.DownloadAsync(BrowserFetcher.DefaultChromiumRevision);
Expand All @@ -186,12 +188,13 @@ private static async void ChromeConvertHtmlToPdf(string html)
await browserForConverting.CloseAsync();
}

AdobePrint();
printingInProgress = false;
ProcessPrint();
NectaLogService.WriteLog("Printing done!", LogLevels.INFO);

return 0;
}

private static void AdobePrint()
private static void ProcessPrint()
{
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "print";
Expand All @@ -204,20 +207,6 @@ private static void AdobePrint()
p.Start();

p.WaitForInputIdle();

var currentTime = DateTime.Now;
var elapsedTime = DateTime.Now;
elapsedTime.AddSeconds(5);

while(true)
{
if (false == p.CloseMainWindow())
p.Kill();

currentTime = DateTime.Now;
var el = DateTime.Compare(currentTime, elapsedTime);
if (el>0) break;
}
}
}
}

0 comments on commit 4af235c

Please sign in to comment.