From 921871cab1a008dd6cb8e4643f97f4693ae4d50f Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Wed, 2 Aug 2023 20:31:02 +0200 Subject: [PATCH] Implement Pdf printing --- Elwig/Documents/Document.cshtml.cs | 4 ++-- Elwig/Documents/Pdf.cs | 10 ++++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Elwig/Documents/Document.cshtml.cs b/Elwig/Documents/Document.cshtml.cs index f9810bd..0c7bd6d 100644 --- a/Elwig/Documents/Document.cshtml.cs +++ b/Elwig/Documents/Document.cshtml.cs @@ -66,9 +66,9 @@ namespace Elwig.Documents { File.Copy(PdfFile.FilePath, pdfPath); } - public async Task Print() { + public async Task Print(int copies = 1) { if (PdfFile == null) throw new InvalidOperationException("Pdf file has not been generated yet"); - Pdf.Print(PdfFile.FilePath); + await Pdf.Print(PdfFile.FilePath, copies); } public void Show() { diff --git a/Elwig/Documents/Pdf.cs b/Elwig/Documents/Pdf.cs index 8fda954..c024be2 100644 --- a/Elwig/Documents/Pdf.cs +++ b/Elwig/Documents/Pdf.cs @@ -5,11 +5,13 @@ using PdfSharp.Pdf.IO; using PuppeteerSharp; using Elwig.Helpers; using Elwig.Windows; +using System.Diagnostics; namespace Elwig.Documents { public static class Pdf { private static readonly string Chromium = @"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"; + private static readonly string PdfToPrinter = App.ExePath + "PDFtoPrinter.exe"; private static IBrowser? Browser = null; public static bool IsReady => Browser != null; @@ -73,8 +75,12 @@ namespace Elwig.Documents { }); } - public static void Print(string path) { - // TODO print pdf + public static async Task Print(string path, int copies = 1) { + var p = new Process() { StartInfo = new() { FileName = PdfToPrinter } }; + p.StartInfo.ArgumentList.Add(path); + p.StartInfo.ArgumentList.Add($"copies={copies}"); + p.Start(); + await p.WaitForExitAsync(); } } }