using System.Drawing.Printing;
using PdfiumViewer;

namespace OrderPROPrint.Services;

public class PrintService
{
    public bool PrintPdf(byte[] pdfBytes, string printerName)
    {
        using var stream = new MemoryStream(pdfBytes);
        using var pdfDocument = PdfDocument.Load(stream);

        using var printDocument = pdfDocument.CreatePrintDocument();
        printDocument.PrinterSettings.PrinterName = printerName;
        printDocument.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);

        // A6 = 105x148mm = 413x583 hundredths of inch
        printDocument.DefaultPageSettings.PaperSize = new PaperSize("A6", 413, 583);

        printDocument.PrintController = new StandardPrintController();

        try
        {
            printDocument.Print();
            return true;
        }
        catch
        {
            return false;
        }
    }
}
