Skip to main content
Business Central forum
Suggested answer

Business Central Codeunit "Base64 Convert" is returning different base64 code than an online converter

editSubscribe (1) ShareShare
ReportReport
Posted on by 15

Hi,

I am trying to convert a report to base64 code so that I can send it to a different application via API calls.

To test, I used an online pdf to base64 converter, I copied the generated code and the application successfully received the document that I sent. Now, instead of hardcoding the base64 code, I want to be able to convert a report in Business Central to base64 and send it. However, my API call failed with an error message "PDF Validation failed". When I compare the outputs from the online converter and the AL procedure, I see that they are different. Below is the code that I used to convert the report to base 64.

local procedure BssiReport()
    var
        ReportOutstream: OutStream;
        ReportInstream: InStream;
        TempBlob: Codeunit "Temp Blob";
        res: Text;
        Txt: Text;
        Base64Convert: Codeunit "Base64 Convert";
    begin
        TempBlob.CreateOutStream(ReportOutstream);
        Report.SaveAs(Report::BssiPLMLeaseSummary, '2022-07-20VERSION(1) SORTING(Field1) WHERE(Field1=1(A100))VERSION(1) SORTING(Field1,Field2)VERSION(1) SORTING(Field1,Field5,Field2)VERSION(1) SORTING(Field1,Field3,Field4,Field5,Field6)VERSION(1) SORTING(Field2,Field48,Field3,Field4,Field5,Field1)VERSION(1) SORTING(Field2,Field48,Field3,Field4,Field5,Field1)VERSION(1) SORTING(Field2)', ReportFormat::Pdf, ReportOutstream);
        TempBlob.CreateInStream(ReportInstream);
        WHILE NOT (ReportInstream.EOS) DO BEGIN
            ReportInstream.READTEXT(Txt);
            res  = Txt;
        END;
        jsonObj.Add('documentBase64', Base64Convert.ToBase64(res));
        //...
    end;

I won't copy the entire code here but this is the first portion of the output that I got from using the online converter:

"JVBERi0xLjcNCiWhs8XXDQoxIDAgb2JqDQo8PC9QYWdlcyAyIDAgUiAvVHlwZS9DYXRhbG9nPj4NCmVuZG9iag0KMiAwIG9iag0KPDwvQ291bnQgMS9LaWRzWyA0IDAgUiBdL1R5cGUvUGFnZXM Pg0KZW5kb2JqDQozIDAgb2JqDQo8PC9DcmVhdGlvbkRhdGUoRDoyMDIyMDcwNDExMTYxOCkvQ3JlYXRvcihQREZpdW0pL1Byb2R1Y2VyKFBERml1bSk Pg0KZW5kb2JqDQo0IDAgb2JqDQo8PC9Db250ZW50cyA1IDAgUiAvTWVkaWFCb3..."

This is what I got from Business Central using CodeUnit "Base64 Convert":

"JVBERi0xLjcl77 977 977 977 977 977 977 9MSAwIG9ialsvUERGL1RleHQvSW1hZ2VCL0ltYWdlQy9JbWFnZUldZW5kb2JqMiAwIG9iajw8L1R5cGUvUGFnZS9QYXJlbnQgNiAwIFIvTWVkaWFCb3hbMCAwIDc5MiA2MTJdL0NvbnRlbnRzIDUgMCBSL1Jlc291cmNlczw8L1Byb2NTZXQgMSAwIFIvWE9iamVjdDw8Pj4vRm9udDw8L0YzIDMgMCBSL0Y0IDQgMCBSPj4 Pj4 ZW5kb2JqMyAwIG9iajw8L1R5cGUvRm9udC9TdWJ0eXBlL1R..."

Does anybody have any idea on why Business Central is not generating the same output?

Categories:
  • Suggested answer
    Binesh Profile Picture
    Binesh 7,885 on at
    RE: Business Central Codeunit "Base64 Convert" is returning different base64 code than an online converter

    Hello,

    Use the below code which will return the base64 string of your report in pdf format.

    Note: Use the return Base64 String to add in Jsonobject. 

        procedure SendReportToBase64()Base64: Text
        var
            BaseConvert: Codeunit "Base64 Convert";
            TempBlob: Codeunit "Temp Blob";
            NAvInstream: InStream;
            ParametersTok: Label '2022-07-20VERSION(1) SORTING(Field1) WHERE(Field1=1(A100))VERSION(1) SORTING(Field1,Field2)VERSION(1) SORTING(Field1,Field5,Field2)VERSION(1) SORTING(Field1,Field3,Field4,Field5,Field6)VERSION(1) SORTING(Field2,Field48,Field3,Field4,Field5,Field1)VERSION(1) SORTING(Field2,Field48,Field3,Field4,Field5,Field1)VERSION(1) SORTING(Field2)';
            Navoutstream: OutStream;
        begin
            Clear(Base64);
            ClearLastError();
            TempBlob.CreateOutStream(NAVOutStream);
            if Report.SaveAs(Report::BssiPLMLeaseSummary, ParametersTok, ReportFormat::Pdf, NAVOutStream) then begin
                TempBlob.CreateInStream(NAvInstream);
                Base64 := BaseConvert.ToBase64(NAvInstream);
                if Base64 = '' then Error('Unable to generate report');
            end else begin
                Clear(Base64);
                Error(GetLastErrorText);
            end;
        end;

  • Suggested answer
    Inge M. Bruvik Profile Picture
    Inge M. Bruvik 32,714 Super User on at
    RE: Business Central Codeunit "Base64 Convert" is returning different base64 code than an online converter

    How are you feeding the online converter. Do you feed it the finished report or the txt value from your code?

    Maybe there is a difference in the character set that is used ?

Helpful resources

Quick Links

What Motivates a Super User?

We know many of you visit the Dynamics 365 Community and Power Platform…

Demystifying Copilot with Georg Glantschnig…

Industry experts answer burning questions directly from our amazing Community…

Setting Up Knowledge Sources for Copilot…

Look at how configuring a comprehensive knowledge base is crucial…

Leaderboard

#1
Andre Arnaud de Calavon Profile Picture

Andre Arnaud de Cal... 283,045 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 222,570 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,138

Product updates

Dynamics 365 release plans