Business Central Cloud renaming a PDF report using FileManagement.BLOBExport

Business Central Cloud renaming a PDF report using FileManagement.BLOBExport

On the Business Central Cloud, to save a report in PDF format and change its name while saving, you can use a STREAM BLOB through the “Temp blob” codeunit.

Once this is done, use the function then the BLOBExport function present in the “File Management” codeunit to export the report created with the “REPORT.SAVEAS” command, changing its name during saving.



pageextension 50001 SalesInvoiceext extends "Sales Invoice"


        addafter("Test Report")
            action("Download PDF Report")
                ApplicationArea = All;
                Image = ExportFile;
                trigger OnAction()

                    TempBlob_lRec: Codeunit "Temp Blob";
                    Out: OutStream;
                    RecRef: RecordRef;
                    FileManagement_lCdu: Codeunit "File Management";
                    SalesHeader_lRec: Record "Sales Header";
TempBlob_lRec.CreateOutStream(Out, TEXTENCODING::UTF8);  // Create Outstream
                    // Record filter
         SalesHeader_lRec.Setrange(“Document Type”,  “Document Type”::Order);                
                    SalesHeader_lRec.SetRange("No.", Rec."No.");
	         // REPORT “SAVEAS” and BLOBExport
                    REPORT.SAVEAS(5001, '', REPORTFORMAT::Pdf, Out, RecRef);    // save report in TempBlob di recRef
                    FileManagement_lCdu.BLOBExport(TempBlob_lRec, STRSUBSTNO('Proforma_%1.Pdf', "No."), TRUE);   // export report in PDF format






2 thoughts on “Business Central Cloud renaming a PDF report using FileManagement.BLOBExport

  • 10 March 2021 at 1:59 PM

    Hi Roberto, thank you for your article, it is very useful!

    We have to do the same but downloading more than one report (in our case we have invoices).
    We are doing the same as you in a proccesing only report, we write your code in the OnAfterGetRecord trigger in “Sales Invoice Header” dataitem. In the request page we set an interval of Document No. (f.e: 7033..7035) and when we do the process we only received the last document in the interval (7035).
    Could you help us?, Do you know what we could do to receive a pdf of each document?

    Thank you for your time.

    Alba Rey

  • 16 February 2023 at 10:15 AM

    Hi Roberto,
    Thank you for this. I had a similar request from a client. From BC21 there is a event subscriber that allows you to change the Filename from base app Report Selection per report.

    In Code unit “Custom Layout Reporting” we can use the eventsubscriber “OnGenerateFileNameOnAfterAssignFileName” to change the File name. The Code will look something like this. In my case I wanted to change the customer Statement File Name.

    [EventSubscriber(ObjectType::Codeunit, Codeunit::”Custom Layout Reporting”, ‘OnGenerateFileNameOnAfterAssignFileName’, ”, true, true)]
    local procedure OnGenerateFileNameOnAfterAssignFileName(var FileName: Text; ReportID: Integer; Extension: Text; DataRecRef: RecordRef)
    Customer: Record Customer;
    FileNameDate: Text[20];
    dotpdfPos: Integer;
    PreFileNameLbl: Label ‘Statement for %1 %2 %3’;
    Pos: Integer;
    LengthText: Integer;
    if ReportID = 1316 then begin
    Pos := Text.StrPos(FileName, ‘as’);
    dotpdfPos := Text.StrPos(FileName, ‘.pdf’);
    FileNameDate := Text.CopyStr(FileName, Pos, dotpdfPos);
    FileName := StrSubstNo(PreFileNameLbl, Customer.”No.”, Customer.Name, FileNameDate);

    This Returns a Filename “Statement for as of .”

Comments are closed.