Wednesday, August 11, 2021

Download Multiple PDF Files as Compress Zip File in Business Central SaaS Version

Hello Expert,

The goal of this pattern is to enable the users to download multiple files as a zip file instead of downloading one by one.  On the Web Client this is preferred way of delivering multiple files since it is one of the web patterns and we cannot use File Management code unit to place files silently on the machine.

To Enable download of multiple files of report at result single zip archive file we can following sample code.

report 65001 "Download Zip File Sample"
{
    UsageCategory = Administration;
    ApplicationArea = All;
    ProcessingOnly = true;
    Caption = 'Download Zip File';

    trigger OnPreReport()
    begin
        //Init Zip Archive
        Clear(DataCompression);
        DataCompression.CreateZipArchive();

        //Create  PDF File 1 and add in Archive >>
        SIH_lRec.RESET;
        SIH_lRec.SetCurrentKey("Sell-to Customer No.");
        SIH_lRec.SetFilter("No.", '103086');

        Clear(ReportRunTempBlob);
        Clear(Rpt_Out);
        Clear(Rpt_Instr);
        ReportRunTempBlob.CreateOutStream(Rpt_Out);
        SIH_RecRef.GetTable(SIH_lRec);
        REPORT.SaveAs(1306''REPORTFORMAT::Pdf, Rpt_Out, SIH_RecRef);
        ReportRunTempBlob.CREATEINSTREAM(Rpt_Instr);
        DataCompression.AddEntry(Rpt_Instr, 'test 1.pdf');


        //Create  PDF File 2 and add in Archive >>
        SIH_lRec.RESET;
        SIH_lRec.SetCurrentKey("Sell-to Customer No.");
        SIH_lRec.SetFilter("No.", '103087');

        Clear(ReportRunTempBlob);
        Clear(Rpt_Out);
        Clear(Rpt_Instr);
        ReportRunTempBlob.CreateOutStream(Rpt_Out);
        SIH_RecRef.GetTable(SIH_lRec);
        REPORT.SaveAs(1306''REPORTFORMAT::Pdf, Rpt_Out, SIH_RecRef);
        ReportRunTempBlob.CREATEINSTREAM(Rpt_Instr);
        DataCompression.AddEntry(Rpt_Instr, 'test 2.pdf');

        //Create  PDF File 3 and add in Archive >>
        SIH_lRec.RESET;
        SIH_lRec.SetCurrentKey("Sell-to Customer No.");
        SIH_lRec.SetFilter("No.", '103088');

        Clear(ReportRunTempBlob);
        Clear(Rpt_Out);
        Clear(Rpt_Instr);
        ReportRunTempBlob.CreateOutStream(Rpt_Out);
        SIH_RecRef.GetTable(SIH_lRec);
        REPORT.SaveAs(1306''REPORTFORMAT::Pdf, Rpt_Out, SIH_RecRef);
        ReportRunTempBlob.CREATEINSTREAM(Rpt_Instr);
        DataCompression.AddEntry(Rpt_Instr, 'test 3.pdf');

        //Save Zip Archive and Convert in InStream Data
        ZipTempBlob.CreateOutStream(ZipFileOutStream);
        DataCompression.SaveZipArchive(ZipFileOutStream);
        ZipTempBlob.CreateInStream(ZipFileInStream);

        DataCompression.CloseZipArchive();

        ZipFileName := 'Test.Zip';
        DOWNLOADFROMSTREAM(ZipFileInStream, '''''', ZipFileName);
    end;

 var
        DataCompression: Codeunit "Data Compression";
        ReportRunTempBlob: Codeunit "Temp Blob";

        Rpt_Out: OutStream;
        Rpt_Instr: InStream;
        SIH_lRec: Record "Sales Invoice Header";
        SIH_RecRef: RecordRef;
        ZipTempBlob: Codeunit "Temp Blob";
        ZipFileOutStream: OutStream;
        ZipFileInStream: InStream;
        ZipFileName: Text;
}

The above code create 3 pdf files of invoice and at the result we get the single zip file.









I hope it will help someone in future.

Thank you reading this blog.

Keep Sharing....Keep Growing...

No comments:

Post a Comment