web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

Downloading Blank PDFs as a ZIP

(1) ShareShare
ReportReport
Posted on by 34
Hi,
 
I want download multiple pdf files as zip. As per the below code it will download the file but with black pdfs.
 
I don't know what is the issue with the code.
 
does anyone know how to fix it?
 
Thanks in advance.
 
 

 
 
 procedure BulkDownloadInvoice(var SalesInvoiceHeader: Record "Sales Invoice Header")
 
    var
 
        TempBlob: Codeunit "Temp Blob";
 
        OutS: OutStream;
 
        InS: InStream;
 
        RecRef: RecordRef;
 
        FldRef: FieldRef;
 
        RecordCounter: Integer;
 
        DataCompression: Codeunit "Data Compression";
 
        FileManagement: Codeunit "File Management";
 
        ZipFileName: Text;
 
        PdfFileName: Text;
 
    begin
 
        ZipFileName := 'Sales Invoices_' + Format(CurrentDateTime) + '.zip';
 
        DataCompression.CreateZipArchive();
 
        if SalesInvoiceHeader.FindFirst() then begin
 
            repeat
 
                Commit();
 
                TempBlob.CreateOutStream(OutS);
 
                RecRef.GetTable(SalesInvoiceHeader);
 
                FldRef := RecRef.Field(SalesInvoiceHeader.FieldNo("No."));
 
                FldRef.SetRange(SalesInvoiceHeader."No.");
 
                if RecRef.FindFirst() then begin
 
                    Report.SaveAs(Report::"Posted Sales Invoice", '', ReportFormat::Pdf, OutS, RecRef);
 
                    TempBlob.CreateInStream(InS);
 
                    PdfFileName := 'Invoice No:' + Format(SalesInvoiceHeader."No.") + ' - ' + Format(SalesInvoiceHeader."Sell-to Customer Name") + '.pdf';
 
                    DataCompression.AddEntry(InS, PdfFileName);
 
                end;
 
                Sleep(1000);
 
            until SalesInvoiceHeader.Next() = 0;
 
            TempBlob.CreateOutStream(OutS);
 
            DataCompression.SaveZipArchive(OutS);
 
            TempBlob.CreateInStream(InS);
 
            DownloadFromStream(InS, '', '', '', ZipFileName);
 
        end;
 
    end;
I have the same question (0)
  • Suggested answer
    Khushbu Rajvi. Profile Picture
    21,048 Super User 2025 Season 2 on at
    I replicated the same process on my system, and I successfully downloaded the zip file containing all the invoices. The data is also visible in the PDF files.
     
     
    One thing I noticed is that you used the report "Posted Sales Invoice" code, while I used the standard report from BC.
     
    I don’t think "Posted Sales Invoice" is a standard report. If it is a custom report, you should check that the layout is correctly assigned to the report.
     
    I strongly believe that there is issue with report and layout only. 
     
    Hope it will help you out.
     
    Regards,
    Khushbu Rajvi
     
     
  • Angel M. Reynoso Gonzalez Profile Picture
    4 on at
    Hello
    Once I had to do the same thing, this was the link that helped me. I hope it can also help. 
     
  • Netjacker2097 Profile Picture
    286 on at
    Hi,
     
    try this:
     
    thanks,
    M
     
    procedure BulkDownloadInvoice(var SalesInvoiceHeader: Record "Sales Invoice Header")
    var
        TempBlob: Codeunit "Temp Blob";
        OutS: OutStream;
        InS: InStream;
        RecRef: RecordRef;
        FldRef: FieldRef;
        DataCompression: Codeunit "Data Compression";
        FileManagement: Codeunit "File Management";
        ZipFileName: Text;
        PdfFileName: Text;
    begin
        ZipFileName := 'Sales Invoices_' + Format(CurrentDateTime) + '.zip';
        DataCompression.CreateZipArchive();
       
        if SalesInvoiceHeader.FindSet() then begin
            repeat
                // Create a new OutStream for each PDF
                TempBlob.CreateOutStream(OutS);
                RecRef.GetTable(SalesInvoiceHeader);
               
                Report.SaveAs(Report::"Posted Sales Invoice", '', ReportFormat::Pdf, OutS, RecRef);
                OutS.Flush(); // Ensure the stream is flushed
               
                TempBlob.CreateInStream(InS);
                PdfFileName := 'Invoice No_' + SalesInvoiceHeader."No." + ' - ' + SalesInvoiceHeader."Sell-to Customer Name" + '.pdf';
               
                DataCompression.AddEntry(InS, PdfFileName);
            until SalesInvoiceHeader.Next() = 0;
           
            // Save the final zip file
            TempBlob.CreateOutStream(OutS);
            DataCompression.SaveZipArchive(OutS);
           
            TempBlob.CreateInStream(InS);
            DownloadFromStream(InS, '', '', '', ZipFileName);
        end;
    end;

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

News and Announcements

Season of Giving Solutions is Here!

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 1,585

#2
Khushbu Rajvi. Profile Picture

Khushbu Rajvi. 780 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 712 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans