Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 Community / Forums / Finance forum / Convert PDF stream to ...
Finance forum
Answered

Convert PDF stream to Base64String in D365FO

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
 
  • Verified answer
    Sangeeth R Profile Picture
    Sangeeth R 86 on at
    Convert PDF stream to Base64String in D365FO
    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
  • Martin Dráb Profile Picture
    Martin Dráb 228,177 Most Valuable Professional on at
    Convert PDF stream to Base64String in D365FO
    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.
  • Sangeeth R Profile Picture
    Sangeeth R 86 on at
    Convert PDF stream to Base64String in D365FO
    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
    Martin Dráb 228,177 Most Valuable Professional on at
    Convert PDF stream to Base64String in D365FO
    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.

Helpful resources

Quick Links

Dynamics 365 Community Update – Sep 16th

Welcome to the next edition of the Community Platform Update. This is a weekly…

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,339 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,177 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans