unit Delivery.Jobs.Reporter.Pdf;

interface

uses
  JsonDataObjects;

type

  TPdfWriter = class
  public
    class procedure WriteOrderTo(const APath: string; AOrder: TJsonObject);
  end;

implementation

uses
  System.Classes,
  System.SysUtils,
  System.Types,
  System.UITypes,
  Skia;

class procedure TPdfWriter.WriteOrderTo(const APath: string; AOrder: TJSONObject);
const
  StrFontName = 'Calibri';
  StrYesNo: array [Boolean] of string = ('No!', 'Sė');
begin
  var LTitleTypeface := TSkTypeface.MakeFromName(StrFontName, TSkFontStyle.Bold);
  var LFieldTypeface := TSkTypeface.MakeFromName(StrFontName, TSkFontStyle.BoldItalic);
  var LValueTypeface := TSkTypeface.MakeFromName(StrFontName, TSkFontStyle.Normal);

  var LTitleFont := TSkFont.Create(LTitleTypeface, 28);
  var LFieldFont := TSkFont.Create(LFieldTypeface, 24);
  var LValueFont := TSkFont.Create(LValueTypeface, 24);

  var LPaint := ISkPaint(TSkPaint.Create);
  LPaint.AntiAlias := True;

  var LSize := TSizeF.Create(600, 600);

  var LDocumentStream := TFileStream.Create(APath, fmCreate);
  try
    var LDocument := TSkDocument.MakePDF(LDocumentStream);
    try
      var LCanvas := LDocument.BeginPage(LSize.Width, LSize.Height);
      try
        LPaint.Color := TAlphaColors.Blue;
        LCanvas.DrawSimpleText('Ordine', 100, 50, LTitleFont, LPaint);

        LPaint.Color := TAlphaColors.Darkblue;
        LCanvas.DrawSimpleText(AOrder['orderCode'], 100, 80, LTitleFont, LPaint);

        LPaint.Color := TAlphaColors.Black;
        LCanvas.DrawSimpleText('Tipo', 100, 150, LFieldFont, LPaint);
        LCanvas.DrawSimpleText(AOrder['serviceType'],
          250, 150, LValueFont, LPaint);

        LCanvas.DrawSimpleText('Prodotto', 100, 180, LFieldFont, LPaint);
        LCanvas.DrawSimpleText(AOrder['productType'], 250, 180, LValueFont, LPaint);

        LCanvas.DrawSimpleText('Persone', 100, 210, LFieldFont, LPaint);
        LCanvas.DrawSimpleText(IntToStr(AOrder['peopleCount']), 250, 210, LValueFont, LPaint);

        LCanvas.DrawSimpleText('Ritiro', 100, 240, LFieldFont, LPaint);
        LCanvas.DrawSimpleText(AOrder['deliveryAt'], 250, 240, LValueFont, LPaint);

        LCanvas.DrawSimpleText('Cliente', 100, 270, LFieldFont, LPaint);
        LCanvas.DrawSimpleText(AOrder['customerName'], 250, 270, LValueFont, LPaint);

        LCanvas.DrawSimpleText('Telefono', 100, 300, LFieldFont, LPaint);
        LCanvas.DrawSimpleText(AOrder['customerPhone'], 250, 300, LValueFont, LPaint);

        LCanvas.DrawSimpleText('E-Mail', 100, 330, LFieldFont, LPaint);
        LCanvas.DrawSimpleText(AOrder['customerMail'], 250, 330, LValueFont, LPaint);

        LCanvas.DrawSimpleText('Privacy', 100, 370, LFieldFont, LPaint);
        LCanvas.DrawSimpleText(StrYesNo[Boolean(AOrder['privacyCheck'])], 250, 370, LValueFont, LPaint);

        LCanvas.DrawSimpleText('Contatto', 100, 400, LFieldFont, LPaint);
        LCanvas.DrawSimpleText(StrYesNo[Boolean(AOrder['contactCheck'])], 250, 400, LValueFont, LPaint);

        LCanvas.DrawSimpleText('Note', 100, 450, LFieldFont, LPaint);
      finally
        LDocument.EndPage;
      end;
    finally
      LDocument.Close;
    end;
  finally
    LDocumentStream.Free;
  end;
end;
end.
