base64_encode of a pdf file

Other open source tools published on ScottKlement.com
Post Reply
pargent70
Posts: 6
Joined: Tue Aug 30, 2022 7:54 am

base64_encode of a pdf file

Post by pargent70 »

Hi,

I'm using base64_encode to work with a pdf file.
I read the pdf content with get_clob_from_file,
Then I put the encoded data in a json ( variable name : request)
and send it by http_req

But the pdf received is different than the original,
(It shows just the template model without the customized data)
Where could be the mistake ?
Thanks

This is an extract of the code:

dcl-s MioFile sqltype(clob:16773100);
dcl-s UTF8Data varchar(16773100) ccsid(1208);
dcl-s encoded char(16773100);
dcl-s request varchar(16773100);

exec sql
set :MioFile = get_clob_from_file(trim(:path));

UTF8Data = %Trim(MioFile);

outputlen = base64_encode(%addr(UTF8Data:*data)
: %Len(UTF8Data)
: %addr(encoded)
: %Len(encoded));
Scott Klement
Site Admin
Posts: 856
Joined: Sun Jul 04, 2021 5:12 am

Re: base64_encode of a pdf file

Post by Scott Klement »

pargent70 wrote: Wed Jan 29, 2025 8:16 am (It shows just the template model without the customized data)
Where could be the mistake ?
I don't know what you mean by "template model without the customized data". I'm guessing its something specific to your particular application where you are, perhaps, using some sort of template combined with custom data, and it happens to be the template part that's working -- but, as I'm completely unfamiliar, this is a wild guess.

Looking at your code, I'm surprised any of it works at all.
pargent70 wrote: Wed Jan 29, 2025 8:16 am

Code: Select all

dcl-s MioFile sqltype(clob:16773100);
dcl-s UTF8Data varchar(16773100) ccsid(1208);
dcl-s encoded char(16773100);
dcl-s request varchar(16773100);

exec sql 
set :MioFile = get_clob_from_file(trim(:path));

UTF8Data = %Trim(MioFile);
I'm not familiar with get_clob_from_file() the CLOB that it outputs contains data in EBCDIC?

The reason I'm asking is the next line "UTF8Data = %Trim(MioFile)" seems designed to convert from EBCDIC to UTF-8. Why would you do that with a PDF? PDFs are binary documents, they aren't either EBCDIC or UTF-8, they are in PDF format. Running it through a translation table will corrupt the data.

Also, MioFile is the name of a data structure (from RPG's perspective) that contains multiple fields, including the length of the text... why would you want to trim, copy and convert all of those fields instead of just the data field? This makes no sense at all.

in fact, I'd strongly recommend NEVER using %TRIM with this sort of program. It is VERY harmful.

pargent70 wrote: Wed Jan 29, 2025 8:16 am

Code: Select all

outputlen = base64_encode(%addr(UTF8Data:*data)
                          : %Len(UTF8Data)
                          : %addr(encoded)
                          : %Len(encoded));
This part looks fine. Just be careful when you use 'encoded' that you handle the length properly using outputlen (and NOT %trim!!)
Post Reply