Search code examples
javapdfibm-midrangerpglerpg

RPG iSeries Take PDF in byte[] from Java function and write it to IFS


I am preparing a program in RPGLE that will use Java function returning fancy PDF file. I want to take that PDF in RPGLE and create a PDF file on IFS.

For now the Java program returns the PDF in byte[] format. Than I take the byte [] as char in RPG and I try to create stream file out of it. But the PDF is damaged and I can't open it via Adobe Reader...

Below is the prototype of Java procedure and code creating stream file:

D GetPDF    PR    A ExtProc(*Java:'PDF':'get') Len(16773100) CCSID(*HEX)

   Dcl-S wkPDFTxt Char(16773100) CCSID(*HEX);

   wkPDFTxt = GetPDF();
   
   fd = open(filePath
            :O_CREAT + O_EXCL + O_WRONLY + O_CCSID + O_INHERITMODE
            :S_IRUSR
            :65535);
   write(fd: %addr(wkPDFTxt): %Len(%Trim(wkPDFTxt));
   close(fd)
  

Anyone is familiar with PDF creation and could help me how can I achieve this? I thought that taking byte [] as char() and writing it to stream file in UTF8 would work but it doesn't.


Solution

  • I found the open/write/close functions. They are UNIX-type integrated file system APIs.

    The CCSID when the file already exists is the file CCSID. The CCSID specified in the parameters is simply the CCSID that the file is created with. It should be either 1208 (UTF-8) or 819 (ASCII), even though PDF is a binary format. Maybe you should use 819 because that appears to be what you are getting from the Java method.

    NO CONVERSION IS PERFORMED. according to the documentation

    If O_TEXTDATA is not specified, the data is processed as binary. The data is read from the file and written to the file without any conversion. The application is responsible for handling the data.

    You must make sure the data is in the required format. The docs are a bit confusing because that fourth parameter is called conversion ID and the docs say

    The specified or derived CCSID is assumed to be the CCSID of the data in the file, when a new file is created. This CCSID is associated with the file during file creation.

    The punctuation could be wrong. To line up with the rest of the document, it should be

    The specified or derived CCSID is assumed to be the CCSID of the data in the file. When a new file is created, this CCSID is associated with the file during file creation.

    In any case, you do not have O_TEXTDATA (and you shouldn't) so since the source CCSID looks like it is 819 (this is normal for a windows file in the US) you should make the CCSID 819 and just write the data the way you got it.