0

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, '<?xml version="1.0" standalone="yes"?><ReportParameters name="test" id="50505555"><Options><Field name="test" /><Field name="testdate">2022-07-20</Field></Options><DataItems><DataItem name="test2">VERSION(1) SORTING(Field1) WHERE(Field1=1(A100))</DataItem><DataItem name="test3">VERSION(1) SORTING(Field1,Field2)</DataItem><DataItem name="test4">VERSION(1) SORTING(Field1,Field5,Field2)</DataItem><DataItem name="test5">VERSION(1) SORTING(Field1,Field3,Field4,Field5,Field6)</DataItem><DataItem name="test6">VERSION(1) SORTING(Field2,Field48,Field3,Field4,Field5,Field1)</DataItem><DataItem name="test7">VERSION(1) SORTING(Field2,Field48,Field3,Field4,Field5,Field1)</DataItem><DataItem name="test8">VERSION(1) SORTING(Field2)</DataItem></DataItems></ReportParameters>', 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?

1 Answers1

0

Have you checked your textencoding? The base64 codeunit has an overload where you can specify the text encoding.

Also, you can use a stream directly with your base64 encoding. Saves you some hassle.

jObject.Add('base64', Base64Convert.ToBase64(InStr), TextEncoding::UTF8);
René
  • 56
  • 3