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

Announcements

No record found.

News and Announcements icon
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
 
  • Martin Dráb Profile Picture
    240,827 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
    240,827 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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the August Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 495 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 432 Super User 2026 Season 2

#3
CU10121822-0 Profile Picture

CU10121822-0 366

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans