1. John Vanleeuwe
  2. PowerBuilder
  3. Wednesday, 23 January 2019 11:06 AM UTC

HI all,


I am consuming a rest webservice api which gives me a back an XML reply message , with one of the XML elements a base64 encoded string.


This base64 encoded string is a PDF file , which i need to decode and then write as a PDF file in a certain folder.


The webservice call works fine , i receive the base64 encoded string perfectly and i when i code to encode the decoded string , i end up having the same string as my initial retrieve. So i know this part works fine.


I am stuck at the simple function to write this blob to a PDF file , the PDF file is created with 219 KB , but gives an adobe error when opening.


I tried filewrite , filewriteex , with blobs and strings , encodingutf8! , encodingansi! and so on... Not sure what i am missing here ...


string ls_historyreport

//ls_historyreport contains the base64 encoded string we receive from the webservice)


blob blob_a

CoderObject lnv_CoderObject

lnv_CoderObject = Create CoderObject

blob_a = lnv_coderobject.Base64Decode(ls_historyreport)
fnr = FileOpen("d:\abc.pdf", StreamMode!, Write!,Shared!,Replace!,EncodingUTF8!)
FileWriteEx(fnr, string( blob_a, EncodingUTF8!))

            fnr = FileOpen("d:\abcs.pdf", StreamMode!, Write!,Shared!,Replace!,EncodingANSI!)
            FileWriteEx(fnr, string(blob_a,EncodingAnsi!))
            fnr = FileOpen("d:\abc.pdf", StreamMode!, Write!,Shared!,Replace!,EncodingANSI!)
            FileWriteEx(fnr, STRING(blob_a))
            fnr = FileOpen("d:\abc1.pdf", StreamMode!, Write!,Shared!,Replace!,EncodingUTF8!)
            FileWrite(fnr, string( blob_a, EncodingUTF8!))
            fnr = FileOpen("d:\abcs2.pdf", StreamMode!, Write!,Shared!,Replace!,EncodingANSI!)
            FileWrite(fnr, string(blob_a,EncodingAnsi!))
            fnr = FileOpen("d:\abc3.pdf", StreamMode!, Write!,Shared!,Replace!,EncodingANSI!)
            FileWrite(fnr, STRING(blob_a))


Not 1 of above filewrites works, ok the 3 filewrites in the end are just tests


What am i missing here please ?


I need more sleep ... I need more coffee....




Accepted Answer
Roland Smith Accepted Answer Pending Moderation
  1. Wednesday, 23 January 2019 14:43 PM UTC
  2. PowerBuilder
  3. # Permalink

A PDF file is ANSI text. The first few characters should look like:


There are no comments made yet.
John Vanleeuwe Accepted Answer Pending Moderation
  1. Wednesday, 23 January 2019 14:52 PM UTC
  2. PowerBuilder
  3. # 1

Hi Roland,


OK , let me check this tomorrow. You mean the first characters of the base64 decoded blob , right ?




  1. Roland Smith
  2. Wednesday, 23 January 2019 21:26 PM UTC
If you open a PDF file on disk in Notepad, those are the first few characters. You have to decode it before writing it to disk.
  1. Helpful
There are no comments made yet.
John Vanleeuwe Accepted Answer Pending Moderation
  1. Wednesday, 23 January 2019 12:00 PM UTC
  2. PowerBuilder
  3. # 2

Thanks Andres.


Just tried this also , corrupt PDF file :(




There are no comments made yet.
Andres Slachevsky Accepted Answer Pending Moderation
  1. Wednesday, 23 January 2019 11:54 AM UTC
  2. PowerBuilder
  3. # 3

Hello John

I have a similar WS i solve it using this code:

First i take the PDF part from the return 

ll_pos_ini = pos(ls_data,"")
if ll_pos_ini < 1 then
 messagebox("ERROR Llamando a WS ",ls_data)
 return -1
end if
ll_pos_fin = pos(ls_data,"")
ls_pdf = mid(ls_data,ll_pos_ini +  7 , ll_pos_fin - (ll_pos_ini +  7 ))
then i decode and write the file
lnv_coderobject = create CoderObject
lbl_pdf = lnv_coderobject.Base64Decode(ls_pdf)
is_archivo_pdf  = is_carpeta + string(par_persona) + ".pdf"
li_file = fileopen(is_archivo_pdf,StreamMode! ,write!, Shared! ,replace!)
There are no comments made yet.
  • Page :
  • 1

There are no replies made for this question yet.
However, you are not allowed to reply to this question.