Re: Decoding PDF data from a web service

Daniel Gross <[email protected]> Fri, 13 Mar 2026 17:38:41 +0100
Newsgroups gmane.comp.lang.as400.rpg
Message-ID <[email protected]>
Hi Greg,

I know I sound like a broken record here on the mailing list - but have you tried it with SQL? If the string gets decoded by the online decoder, it might be possible, that there is a problem with the decoding procedure.

As the strings are quite large, you will average to use CLOBs, as SQL only supports VARCHAR up to a length of 32739 bytes.

So it might be an an idea to try:

   dcl-s encoded_clob sqltype(clob:1000000) ccsid(*utf8) inz;
   dcl-s decoded_clob sqltype(clob:1000000) ccsid(*utf8) inz;

   encoded_clob_data = json.data.node.packingSlipPdfData;
   encoded_clob_len = %len(json.data.node.packingSlipPdfData);

   exec sql set :decoded_clob = base64_decode(:encoded_clob);

   base64decoded = %subst(decoded_clob_data:1:decoded_clob_len);

Or you can even use SQL to write the decoded data directly to your IFS file.

HTH
Daniel


> Am 13.03.2026 um 17:19 schrieb Greg Wilburn <[email protected]>:
> 
> I have done this quite a few times before, so I'm not sure why I'm struggling so much with this...
> 
> I'm getting a json document from a server that contains a base64 encoded string representing a packing slip... I'm able to copy the data from the webservice response file (JSON) on the IFS, and paste that into an online decoder.  That works.
> 
> But when I try to do he same in my program, I'm getting an error:
> " Unable to decode character at position 1. (Char=x'4A') "
> 
> Using the online decoder, indicating UTF8, it will decode.  Using the same decoder, selecting autodetect, it decodes differently indicating windows 1252?
> 
> Just wondering if anyone can spot any glaring errors in my code.  I've been looking at it all morning.  UGH.
> 
> // definitions
> 
>  dcl-s  base64encoded  varchar(1000000) ccsid(*utf8) inz;
>  dcl-s  base64decoded  char(1000000) inz;
>  dcl-s  fd             int(10);
> 
>  dcl-s  pdfFile        varchar(128) inz;
>  dcl-s  pdfData        varchar(1000000) ccsid(*utf8) inz;
> 
> 
> // Decode Packslip Data
>  base64Encoded = json.data.node.packingSlipPdfData;
>  len = base64_decode( %addr(base64Encoded:*data)
>                     : %len(%trim(base64Encoded))
>                     : %addr(base64Decoded)
>                     : %size(base64Decoded)
>                     );
>  pdfData = %subst(base64decoded:1:len);
>  pdfFile = fldr + '/packslips/' + inCom + '_' + inOrd + '.pdf';
> 
>  flag = o_wronly + o_creat + o_ccsid + o_excl;
>  mode = s_irusr + s_iwusr + s_iroth + s_iwoth;
>  fd = open(pdfFile:flag:mode:1208:0);           // PDF is UTF8
>  rc = write( fd
>            : %addr(pdfData:*data)
>            : %len(pdfData));
> 
> [Logo]<https://www.totalbizfulfillment.com/>    Greg Wilburn
> Director of IT
> 301.895.3792 ext. 1231
> 301.895.3895 direct
> [email protected]<mailto:[email protected]>
> 1 Corporate Dr
> Grantsville, MD 21536
> www.totalbizfulfillment.com<http://www.totalbizfulfillment.com>
> --
> This is the RPG programming on IBM i (RPG400-L) mailing list
> To post a message email: [email protected]
> To subscribe, unsubscribe, or change list options,
> visit: https://lists.midrange.com/mailman/listinfo/rpg400-l
> or email: [email protected]
> Before posting, please take a moment to review the archives
> at https://archive.midrange.com/rpg400-l.
> 
> Please contact [email protected] for any subscription related questions.
> 
-- 
This is the RPG programming on IBM i (RPG400-L) mailing list
To post a message email: [email protected]
To subscribe, unsubscribe, or change list options,
visit: https://lists.midrange.com/mailman/listinfo/rpg400-l
or email: [email protected]
Before posting, please take a moment to review the archives
at https://archive.midrange.com/rpg400-l.

Please contact [email protected] for any subscription related questions.