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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Convert PDF stream to Base64String in D365FO

(1) ShareShare
ReportReport
Posted on by 86
Hi Folks,
 
We are trying to convert PDF stream to Base64String, in order to share the encoded Base64 string with other 3PL system.
 
Note: We don't have file as input, we are getting this PDF Stream from class (ERFileDestinationAttachment) while system execute ER (BDM) report.
 
It wasn't working when this feature is enabled /Utilize application resources to perform CBD documents conversion from Word to PDF format/ under feature management.

Requirement scope: Convert PDF stream to Base64String  
 
Logic 1:
System.Byte[]               bytes;
    System.IO.BinaryReader      binaryReader;
    System.IO.Stream            baseStream;
    System.IO.StreamReader      streamReader;
    System.Object               streamObject;
    System.Object               bytesObject;
    int                         streamLength;
    int                         bytesLength;
baseStream      = _pdfStream;
            streamObject    = baseStream.get_Length();
            streamLength    = streamObject;
            binaryReader    = new System.IO.BinaryReader(baseStream);
            bytes           = binaryReader.ReadBytes(streamLength);  // Here system return as bytes[0]
 
            baseStream.Close();
            bytesObject     = bytes.get_Length();
            bytesLength     = bytesObject;
            base64data      = System.Convert::ToBase64String(bytes, 0, bytesLength);
 
Logic 2:
using (System.IO.Stream fileStream = _pdfStream)
            {
                using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
                {
                    fileStream.CopyTo(memoryStream);
                    base64data = ERConversionUtils::StreamToBase64(memoryStream);
                }
            }
 
For conversion from PDF Stream to BASE64String
If Feature is enabled, 
           Both logic 1 & 2 - Not working
if Feature is disabled,
           Both logic 1 & 2 - Working
 
Note: We had to enable this feature, since we are trying to print/save document which has more than 300+ lines. As per suggestion from MS.
 
Kindly advise with some input on this.
 
Thanks in advance.
 
Thanks,
Sangeeth
 
I have the same question (0)
  • Martin Dráb Profile Picture
    237,801 Most Valuable Professional on at
    What do you need from us? Can't you simply use the working solution, i.e. enabling the feature?
     
    I don't know what you mean by "not working", bit it's clear that the problem isn't related to the code you showed us above. It seems to be about how you create the PDF. I'm assuming that you use Configurable Business Document (CBD), which the feature is about. Then enabling it makes a good sense.
     
    It's difficult to guess if all you told us was "not working", but it's quite possible that you'll see the problem if you forget your code and just download the file by File::SendFileToUser(baseStream, 'test.pdf'). If so, it's a definite proof that problem has nothing to do with your conversion to a Base64 string.
  • Sangeeth R Profile Picture
    86 on at
    Hi Martin,
     
    Thanks for reply. Sorry I might not give clear requirement scope.
     
    Requirement scope: Convert PDF stream to Base64String 
    Technical scope: Receiving PDF stream from D365FO with use of CBD and then sharing BASE64String conversion value to another system.
     
    We use custom service API to get request from another system, then we respond them as Base64String.
     
    So, Logic which using for this requirement was working only when that feature is DISABLED.
     
    Thanks,
    Sangeeth
  • Martin Dráb Profile Picture
    237,801 Most Valuable Professional on at
    Ah, sorry, I overlooked that it works if the feature is disabled, not enabled.
     
    But the rest of my reply still stands, such as that the problem doesn't seem to be anywhere in the code you showed us.
  • Verified answer
    Sangeeth R Profile Picture
    86 on at
    Hi All,
     
    At last, it got worked for me.
     
    Logic:

    str     fileDownloadURL;
                System.IO.Stream        tempPdfStream;
                fileDownloadURL = File::SendFileToTempStore(<Input stream(
    _pdfStream)>, this.parmSaveFilePath()); 

                tempPdfStream = File::UseFileFromURL(fileDownloadURL);

     

    baseStream      = tempPdfStream ;
                streamObject    = baseStream.get_Length();
                streamLength    = streamObject;
                binaryReader    = new System.IO.BinaryReader(baseStream);
                bytes           = binaryReader.ReadBytes(streamLength);  
     
                baseStream.Close();
                bytesObject     = bytes.get_Length();
                bytesLength     = bytesObject;
                base64data      = System.Convert::ToBase64String(bytes, 0, bytesLength);
     
     
    Thanks,
    Sangeeth

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

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 > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 660 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 549 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 307 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans