Friday, May 13, 2022

API Base Cloud Printing in Business Central SaaS Version

Hello Friends,

It was very demanding in Business Central Cloud version to allow user to print directly from BC / Mobile App / Tablet App.

This can only be possible using Cloud API for printing.

We have develop very advance cloud printing API utility for print any Report by just single click from Business Central - any version.









procedure GetBase64_DS(ReportID: Integer; RecRef: RecordRef)
    var
        APISetup_lRec: Record "Cloud Print API Setup";
        ReportPrinterSetup: Record "Report Printer Setup";
        Base64Convert: Codeunit "Base64 Convert";
        Rpt_TempBlob: Codeunit "Temp Blob";
        gHttpClient: HttpClient;
        gcontent: HttpContent;
        lheaders: HttpHeaders;
        greqMsg: HttpRequestMessage;
        gResponseMsg: HttpResponseMessage;
        Rpt_InStream: InStream;
        DuplexPrintOption_lInt: Integer;
        ReqJObject: JsonObject;
        Rpt_OutStream: OutStream;
        Base64String: Text;
        JSONResponse: Text;
        ltext: Text;
        lurl: Text;
        PDFCMDBase64_lTxt: Text;
        PrintCmdText: Text;
        PrinterName_iTxt: Text;
        ReportFileName_lTxt: Text;
    begin
        APISetup_lRec.GET;
        APISetup_lRec.Testfield("Exe Path");
        APISetup_lRec.Testfield("Folder Path");
        APISetup_lRec.Testfield("Print API");

        ReportPrinterSetup.Reset();
        ReportPrinterSetup.SetRange("User ID", UserId);
        ReportPrinterSetup.SetRange("Report ID", ReportID);
        IF ReportPrinterSetup.FindFirst() THEN begin
            DuplexPrintOption_lInt := ReportPrinterSetup."Print Duplex Mode" + 1;
            PrinterName_iTxt := ReportPrinterSetup."Printer Name"
        End Else begin
            ReportPrinterSetup.SetRange("Report ID", 0);
            IF ReportPrinterSetup.FindFirst() THEN begin
                DuplexPrintOption_lInt := ReportPrinterSetup."Print Duplex Mode" + 1;
                PrinterName_iTxt := ReportPrinterSetup."Printer Name";
            End;
        end;


        Rpt_TempBlob.CreateOutStream(Rpt_OutStream);
        Report.SaveAs(ReportID, RecRef.GetFilters(), ReportFormat::Pdf,
Rpt_OutStream, RecRef);
        Rpt_TempBlob.CreateInStream(Rpt_InStream);
        Base64String := Base64Convert.ToBase64(Rpt_InStream);

        ReportFileName_lTxt := 'Inv_' + FORMAT(CURRENTDATETIME, 0,
'<Day,2>_<Month,2>_<Year4>_<Hours24>_<Minutes,2>_<Seconds,2>') + '_' +
Format(Random(100)) + '.pdf';

        PrintCmdText := StrSubstNo('%1 -duplex %2 -printer "%3" %4%5',
APISetup_lRec."Exe Path", DuplexPrintOption_lInt, PrinterName_iTxt,
APISetup_lRec."Folder Path", ReportFileName_lTxt);

        PDFCMDBase64_lTxt := Base64Convert.ToBase64(PrintCmdText);

        ReqJObject.Add('ReportBase64', Base64String);
        ReqJObject.Add('PrintCMDCommand', PDFCMDBase64_lTxt);
        ReqJObject.Add('FileName', ReportFileName_lTxt);
        ReqJObject.Add('FolderPath', Base64Convert.ToBase64(APISetup_lRec."Folder Path"));
        ReqJObject.Add('Exepath', Base64Convert.ToBase64(APISetup_lRec."Exe Path"));
        ReqJObject.WriteTo(ltext);

        lurl := APISetup_lRec."Print API";

        gcontent.WriteFrom(ltext);
        greqMsg.SetRequestUri(lurl);
        lheaders.Clear();
        gcontent.GetHeaders(lheaders);

        lheaders.Remove('Content-Type');
        lheaders.Add('Content-Type', 'application/json');
        gcontent.GetHeaders(lheaders);
        greqMsg.Content(gcontent);
        greqMsg.GetHeaders(lheaders);
        greqMsg.Method := 'POST';
        if not gHttpClient.Send(greqMsg, gResponseMsg) then
            Error('API Authorization token request failed...');

        JSONResponse := '';
        gResponseMsg.Content().ReadAs(JSONResponse);


        IF STRPOS(JSONResponse, 'An error has occurred') <> 0 then
            Error('Error %1 \\ PDF Command (in Base64) %2', JSONResponse, PDFCMDBase64_lTxt);

        IF STRPOS(JSONResponse, 'Time of printing is:') <> 0 then
            Message('Document Print Successfully')
        Else
            Message(JSONResponse);
    end;
}

Done...

This will print any report in just single click - where user can setup Default printer name in Business Central

Also this utility provide advance command of print like - Simple / Duplex Print and Paper Source Selection.

New Revaluation of Printing in cloud application !!!

If you intrested to purcahse this utility than contact to Intech System.

Thank you for reading....

Keep Sharing...Keep Growing...


No comments:

Post a Comment