**free ctl-opt dftactgrp(*no) bnddir('BASE64') option(*srcstmt:*noshowcpy); /copy ifsio_h /copy base64_h dcl-s pdfPath varchar(5000); dcl-s data varchar(7000000); dcl-s b64data varchar(9333336); dcl-s fd int(10); dcl-s templen int(10); pdfPath = '/home/sklement/test.pdf'; // Read entire PDF file into memory in binary mode // NOTE: To use 'data' with the read() API, we need // to treat it as a buffer. Trouble is, RPG wipes // out data when you increase the length of a varchar // so before we use it, set the length to its maxmimum. // After reading, we can set it back to the proper length fd = open(pdfPath: O_RDONLY); if fd = -1; // handle error endif; %len(data) = %len(data:*MAX); templen = read(fd: %addr(data:*data): %len(data)); %len(data) = templen; callp close(fd); // base64 encode data // NOTE: Same issue with using a varchar as a buffer as described // above. // // NOTE: Here all of the data is written as a single string. // If the goal is to MIME encode it for e-mail, it'd be better // to read/write in a loop and write 70-character lines. %len(b64data) = %len(b64data:*max); templen = base64_encode( %addr(data: *data) : %len(data) : %addr(b64data: *data) : %len(b64data) ); %len(b64data) = templen; fd = open('/home/sklement/test.b64' : O_WRONLY + O_CREAT + O_INHERITMODE + O_TRUNC + O_TEXT_CREAT + O_CCSID + O_TEXTDATA : 0 : 1208 : 0 ); if fd = -1; // handle error endif; callp write(fd: %addr(b64data:*data): %len(b64data)); callp close(fd); *inlr = *on;