Basit ama etkili bir yazdırma sunucusu.
Mantık olarak unigui üzerinden lokal ip yi kontrol ettiriyorum daha sonra yazıcı ismini alıyorum daha sonra rest sunucusuna fastreportdan aldığım pdf verisine gönderiyorum.
unit PrinterServerMethodsUnit;
interface
uses
System.SysUtils, System.Classes, Datasnap.DSServer, Datasnap.DSAuth, Printers,
IdBaseComponent, IdComponent, IdIPWatch, WinSpool, ShellAPI, gtPDFDoc,
gtPDFPrinter, Types;
type
{$METHODINFO ON}
PrinterServerMethods = class(TComponent)
private
{ Private declarations }
function GetLocalIp: string;
public
{ Public declarations }
function Connect(ip: string): Boolean;
function GetPrinterName: string;
function Print(data: TStream): Boolean;
end;
{$METHODINFO OFF}
implementation
uses
System.StrUtils;
function PrinterServerMethods.Connect(ip: string): Boolean;
begin
if ip = GetLocalIp then
Result := True
else
Result := False;
end;
function PrinterServerMethods.GetLocalIp: string;
var
IPW: TIdIPWatch;
begin
IPW := TIdIPWatch.Create(nil);
try
if IPW.LocalIP <> '' then
Result := IPW.LocalIP;
finally
IPW.Free;
end;
end;
function PrinterServerMethods.GetPrinterName: string;
begin
if (Printer.Printers.Count > 0) then
begin
Printer.PrinterIndex := -1;
Result := Printer.Printers[Printer.PrinterIndex];
end
else
begin
Result := '';
end;
end;
function PrinterServerMethods.Print(data: TStream): Boolean;
var
pdf: TgtPDFDocument;
pdf_printer: TgtPDFPrinter;
begin
try
try
pdf := TgtPDFDocument.Create(Self);
pdf_printer := TgtPDFPrinter.Create(Self);
pdf.LoadFromStream(data);
pdf_printer.PDFDocument := pdf;
pdf_printer.SelectPrinterByName(GetPrinterName);
pdf_printer.ShowSetupDialog := False;
pdf_printer.PrintDoc;
Result := True;
finally
pdf.Free;
pdf_printer.Free;
end;
except
on e: exception do
begin
Result := False;
end;
end;
end;
end.