Page 1 of 2

Convert PDF to Base64

Posted: Sun Apr 18, 2021 11:09 am
by PedroAlex
Hey guys.
Does anyone have an example of how to convert a PDF file to Base64 String.

Many Thanks.

Re: Convert PDF to Base64

Posted: Sun Apr 18, 2021 10:22 pm
by Tom
Load the File (Memoread, FileStr) as a binary string, apply a conversion function (xbBase64Encode from Xb2.Net, Xbase++ has a function aswell) to that string, that's it.

Re: Convert PDF to Base64

Posted: Tue Apr 20, 2021 1:39 am
by PedroAlex
Tom, I did that, too, but it's not that simple.
This sample does not work.
xBase 2.0.1176.
Can you check if it works with your version?

Code: Select all


//=====================================================================
Function PDF2Base64()
Local cFile := Dc_popFile()

MsgBox(cFile,'The  PDF' File)

cStream := FileStr(cFile)

MsgBox( cStream,'FileStr() do PDF' )

cB64 := Bin2Base64( cStream )

MsgBox( 'CB64 ' + cB64 ,'PDF to Base64')

Return NIL

Something doesn't work well

Re: Convert PDF to Base64

Posted: Tue Apr 20, 2021 3:09 am
by gradosic
Hi!

I do it thisway:


cFile = PDF file...
Base64Coded:=ToBase64(MEMOREAD(cFile))

then you can use this "Base64Coded" how you want (in XML, SOAP, etc...)

Goran

Re: Convert PDF to Base64

Posted: Tue Apr 20, 2021 3:47 am
by PedroAlex
Hi,
Tobase64() ?? It is Alaska xbase ??


I think the problem is in the way of reading the Pdf.
Memoread () and FileStr () don't seem to be the right way ...

Re: Convert PDF to Base64

Posted: Tue Apr 20, 2021 4:12 am
by gradosic
It's ot4xb library, you can find it on https://www.dll-files.com/ot4xb.dll.html..

this is working 100%, we send thousands of PDF files thisway.

br

Re: Convert PDF to Base64

Posted: Tue Apr 20, 2021 4:44 am
by Tom
Or use the functions from Xb2.Net.

Code: Select all

cBase64 := xbBase64Encode(FileStr(cFileName))

Re: Convert PDF to Base64

Posted: Tue Apr 20, 2021 6:22 am
by PedroAlex
Goran,

Can you post (in a Txt file) the result of ToBase64() the anexed pdf?

Re: Convert PDF to Base64

Posted: Tue Apr 20, 2021 7:34 am
by Tom
Hi, Pedro.

See Attachment. Result of xbBase64Encode(FileStr("FATURA_FT_1_768.pdf")).

Re: Convert PDF to Base64

Posted: Thu Apr 22, 2021 10:20 am
by PedroAlex
Hi, Tom.

Many Thanks .

The result of your sample is correct. But I have not Xb2net.

I cannot get this result with the Alaska Function Bin2Base64().

Have you tried this feature in your Alaska version?

Just for confirm..

cB64 := Bin2Base64(FileStr("FATURA_FT_1_768.pdf"))

Thanks..