Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics NAV (Archived)

Convert PDF to Base64 using OutStream

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Hi Experts, 

I have a requirement wherein I have to convert the saved PDF to Base64. I have written the following code but that doesn't seem to work and there are hardly any blogs regarding it.

Filename := 'C:\NAV\Service Invoices\PSI0000081.pdf';
MyFile.OPEN(Filename);
MyFile.CREATEINSTREAM(IStream);
MemoryStream := MemoryStream.MemoryStream();
COPYSTREAM(MemoryStream,IStream);
Byte := MemoryStream.GetBuffer();
locOutFile.ADDTEXT(Convert.ToBase64String(Byte));
MESSAGE(FORMAT(locOutFile));
MyFile.CLOSE; 

Can anybody suggest me how than can be done or where did I go wrong? Your suggestions are welcomed. 

Thanks in advance. 

*This post is locked for comments

  • Suggested answer
    I Gusti Made Ari Profile Picture
    I Gusti Made Ari 3,594 on at
    RE: Convert PDF to Base64 using OutStream

    You can use File Management codeunit and TempBlob record

    FileName :=  'C:\NAV\Service Invoices\PSI0000081.pdf';

    FileManagement.BLOBImportFromServerFile(TempBlob,FileName);

    MESSAGE('%1',TempBlob.ToBase64String);

    Since you are using MESSAGE prompt , try to convert PDF file with size smaller than 20 KB since the converted base64 text can be too long and cannot show in message prompt.

    Alternatively save the base64 text in blob field or text file instead.

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

Announcing Our 2025 Season 1 Super Users!

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

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,489 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,305 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans