Skip to main content

Notifications

Announcements

No record found.

Small and medium business | Business Central, N...
Suggested answer

How to run a report and convert it to Base64 string.

(2) ShareShare
ReportReport
Posted on by 149
Hi  All,

We are working on on-prem Business Central , i want to create pdf of sales order using AL code. Actually i want to run my sales order report in al code then i want to convert it on base64 so after that i can send this base64 string to API.

Does anyone have any idea about this, how we can do this.
 
Thanks for Help.
  • VaHiX Profile Picture
    VaHiX 3,702 on at
    How to run a report and convert it to Base64 string.
     

    ** Please don't forget to close up the thread here by Like and verify it as an answer if it is helpful **

  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 75,848 Super User 2024 Season 2 on at
    How to run a report and convert it to Base64 string.
    Update: Dynamics 365 Business Central: How to save a report as encoded text (Base64 String) via AL
     
    Thanks.
    ZHU
  • Suggested answer
    Nyakinyua Profile Picture
    Nyakinyua 55 on at
    How to run a report and convert it to Base64 string.
    Var
    VarTempBlob: Codeunit "Temp Blob";
    IStream: InStream;
    B64: Codeunit Base64; 
    FileManagement: Codeunit "File Management";
    EncryptedFileName: Text;
                        
    if not FileManagement.ServerFileExists(EncryptedFileName) then
        Error('The file %1 does not exist.', EncryptedFileName);
     
    // Create an InStream from the file
    FileManagement.BLOBImportFromServerFile(VarTempBlob, EncryptedFileName);
     
    // Get the InStream from the TempBlob
    VarTempBlob.CreateInStream(IStream);
     
    // Convert the InStream to Base64
    Base64 := B64.StreamToBase64String(IStream);
  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 75,848 Super User 2024 Season 2 on at
    How to run a report and convert it to Base64 string.
    Hi, hope the following helps as well.
    Dynamics 365 Business Central: How to import/create item picture from encoded text (Base64 String)
    Dynamics 365 Business Central: How to convert Image (item picture) to encoded text (Base64 String) via AL
     
    Thanks.
    ZHU
  • Suggested answer
    Sami Ullah Profile Picture
    Sami Ullah 283 on at
    How to run a report and convert it to Base64 string.
    Hi,
    Please try this code.
     
    var
        ReportSelections: Record "Report Selections"; 
        TempReportSelections: Record "Report Selections" temporary; 
        TempBlob: Codeunit "Temp Blob"; 
        Base64Convert: Codeunit "Base64 Convert"; 
        outStreamReport: OutStream; 
        inStreamReport: InStream; 
        ReportUsage: Enum "Report Selection Usage"; 
        DocumentPrint: Codeunit "Document-Print"; 
        SourceRecordRef: RecordRef; 
        SalesHeader: Record "Sales Header"; 
        DocumentBase64: Text; 
    begin
        // If you already have a RecordRef, you can skip the next four lines.
        if not SalesHeader.FindFirst() then 
            // For testing purposes, retrieving the first record of the Sales Header.
            // In your real scenario, replace this with the necessary conditions.
            exit; 
    
        SalesHeader.SetRecFilter(); 
        // Set the filter on SalesHeader to be used for the RecordRef.
    
        SourceRecordRef.GetTable(SalesHeader); 
        // Assign the SalesHeader record to the SourceRecordRef.
    
        ReportUsage := DocumentPrint.GetSalesDocTypeUsage(SalesHeader); 
        // Determine the report usage based on the document type of the Sales Header.
    
        ReportSelections.FindReportUsageForCust(ReportUsage, SalesHeader."No.", TempReportSelections); 
        // Find the appropriate report selection for the customer based on the report usage and Sales Header number.
        // Store the result in a temporary Report Selections record (TempReportSelections).
    
        TempBlob.CreateOutStream(outStreamReport); 
        // Create an output stream in the TempBlob to store the generated report.
    
        ReportSelections.SaveReportAsPDFInTempBlob(
            TempBlob, TempReportSelections."Report ID", 
            SourceRecordRef, TempReportSelections."Custom Report Layout Code", ReportUsage
        ); 
        // Save the selected report as a PDF into TempBlob using the specified report ID, 
        // custom report layout, and report usage.
    
        TempBlob.CreateInStream(inStreamReport); 
        // Create an input stream from the TempBlob to read the PDF report data.
    
        DocumentBase64 := Base64Convert.ToBase64(inStreamReport, false);
        // Convert the report data from the input stream to a Base64-encoded string 
        // and assign it to the DocumentBase64 variable.
    end;
    
     
  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 21,081 Super User 2024 Season 1 on at
    How to run a report and convert it to Base64 string.
    Hi,
    You can try as below
     
    var
            TempBlob: Codeunit "Temp Blob";
            ReportInStream: InStream;
            ReportOutStream: OutStream;
    Base64Convert: Codeunit "Base64 Convert"; TempBlob.CreateOutStream(ReportOutStream); Report.SaveAs(ReportID, '', ReportFormat::Html, ReportOutStream, RecordRef); TempBlob.CreateInStream(ReportInStream); ReportInStream.ReadText(BodyText);
    ​​​​​​​Base64Convert.ToBase64(BodyText);


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

Quick Links

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,711 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,458 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans