Saturday, May 14, 2022

Merge Multiple PDF fiels in Single PDF file in Business Central

 Hello Team,

Merge multiple pdf files is single pdf file is demanding requirement in Business Central SaaS Version

We have develop very simple API Utility that can take N number on input Base64 data and combine in single PDF and return sigle Base 64 file for merge PDF.






Key Feature:

- Merge N Numbers of different file in Single PDF

- Merge Standard Attached PDF Documents on Master with Primary Document File

- Send Merge PDF Files on Email

- Save Merge PDF File in Document Attachment

Code Snapshot:


 procedure MergeInvoices(var SIH_vRec: Record "Sales Invoice Header")
    var
        ReportSelect_lRec: Record "Report Selections";
        CurrentSIH_lRec: Record "Sales Invoice Header";
        Base64Convert: Codeunit "Base64 Convert";

        RecRef: RecordRef;

        Base64String: Text;
        Win_gDlg: Dialog;
        Curr_gInt: Integer;
    begin
        Win_gDlg.Open('Total #1#############\Current #2##############');
        Win_gDlg.Update(1, SIH_vRec.Count);

        ReportSelect_lRec.Reset();
        ReportSelect_lRec.Setrange(Usage, ReportSelect_lRec.Usage::"S.Invoice");
        ReportSelect_lRec.FindFirst();
        ReportSelect_lRec.Testfield("Report ID");

        IF SIH_vRec.FindSet() THen
            repeat
                Curr_gInt += 1;
                Win_gDlg.Update(2, Curr_gInt);


                CurrentSIH_lRec.get(SIH_vRec."No.");
                CurrentSIH_lRec.SetFilter("No.", SIH_vRec."No.");
                RecRef.GetTable(CurrentSIH_lRec);
                RecRef.SetView(CurrentSIH_lRec.GetView());

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

                //FileBase64 Object
                Clear(JObject);
                JObject.Add('FileBase64', Base64String);
                ObjectJArray.Add(JObject);
            until SIH_vRec.NEXT() = 0;
        Win_gDlg.Close();

        CallPDFMergeAPI();
    end;

 local procedure CallPDFMergeAPI()
    var
        Base64Convert: Codeunit "Base64 Convert";
        DoubleQuote_Lbl: Label '""""';
        lText: Text;
        ShipmentResponse: Text;
        MergeFileName_lTxt: Text;
        gHttpClient: HttpClient;
        OrderDetailContent: HttpContent;
        lheaders: HttpHeaders;
        greqMsg: HttpRequestMessage;
        gResponseMsg: HttpResponseMessage;

    begin
        OrderObject.Add('Files', ObjectJArray);
        OrderObject.WriteTo(lText);
        // Message(lText);

        OrderDetailContent.WriteFrom(lText);

        Clear(greqMsg);
        Clear(gHttpClient);
        greqMsg.SetRequestUri(API Link here....);
        lheaders.Clear();
        OrderDetailContent.GetHeaders(lheaders);
        lheaders.Remove('Content-Type');
        lheaders.Add('Content-Type', 'application/json');
        OrderDetailContent.GetHeaders(lheaders);
        greqMsg.Content(OrderDetailContent);
        greqMsg.Method := 'post';
        gHttpClient.Send(greqMsg, gResponseMsg);
        ShipmentResponse := '';

        gResponseMsg.Content().ReadAs(ShipmentResponse);
        ShipmentResponse := DelChr(ShipmentResponse, '<>', DoubleQuote_Lbl);

        //  Message(ShipmentResponse);

        Clear(Rpt_TempBlob);
        Rpt_TempBlob.CreateOutStream(Rpt_OutStream);
        Base64Convert.FromBase64(ShipmentResponse, Rpt_OutStream);
        Rpt_TempBlob.CreateInStream(Rpt_InStream);
        MergeFileName_lTxt := 'MergeFile_' + FORMAT(CURRENTDATETIME, 0,
'<Day,2>_<Month,2>_<Year4>_<Hours24>_<Minutes,2>_<Seconds,2>') + '_' +
Format(Random(100)) + '.pdf';
        DownloadFromStream(Rpt_InStream, '', '', '', MergeFileName_lTxt);
    end;

    var
        Rpt_TempBlob: Codeunit "Temp Blob";
        OrderObject: JsonObject;
        ObjectJArray: JsonArray;
        JObject: JsonObject;
        Rpt_InStream: InStream;
        Rpt_OutStream: OutStream;

Done...

Very simple and most useful solution.

If you need this utility than contact to Intech Systems

Thank you for reading...

Keep Sharing...Keep Growing...

No comments:

Post a Comment