ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Webservice return stream file

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Webservice return stream file

    Hello,

    is there a way to return a stream file (PDF) from a rpg programm. I want to build a webservice that can be called and this webservice returns a pdf. Is there a way to get this to work?

  • #2
    Simply use the OVRPRTF command to redirect your spool output to a PDF file. Alternatively (if you need a "printed" copy of the file) then produce the spool file as usual and then use CPYSPLF to produce the desired PTF.

    If for some reason you cannot use a regular RPG print file then you'll have to use a language like PHP, Python, Java, node.js or whatever for which there are PDF packages available. Then simply invoke the relevant script from your RPG code.

    Comment


    • #3
      Thank you for your reply. Im trying it with qtmhwrstout to get the pdf on the browser of the client but maybe im doing something wrong.

      Code:
      Headers = 'Content-Type: application/pdf' + newline +
                                    'Content-Disposition: inline; filename=' + %trimr(filepath)
                                    + newline + newline;
      qtmhwrstout(headers:  %len(headers):  err);​

      Comment


      • #4
        ** Post deleted - couldn't find another way of doing it

        Last edited by JonBoy; August 15, 2023, 03:03 PM.

        Comment


        • #5
          The header type you are using is normally only aimed at a browser i.e. where you want to show the user the pdf as page content. But you say you are providing a web service.

          If you really want to send the pdf as a web service response it would normally be wrapped in (say) json _and_ base64 encoded. Reason for this being that a pdf is a binary format and you'll almost certainly get character conversion problems just sending it.

          It would be helpful if you could be a little clearer about the service you are providing.

          You can find some ideas on how and why base64 is needed in this thread on Midrange.com https://archive.midrange.com/web400/.../msg00047.html

          Comment


          • #6
            I'm reasonably sure that the IBMi webserver (at least the one we use, which I think is Apache) does not character set convert non-text content types. So if you specify application/pdf and then write the raw bytes to STDOUT using qtmhwrstout, it should work.

            I write a proof-of-contept program in 2019 to do exactly this - read IFS files and return them as the response body of a HTTP GET request. It worked for html files and PDFs, as long as you specified the correct content type in the header.

            Plain text type file:
            Code:
                     Dcl-C CGI_LINE_FEED       const(x'15');
            
                     headers = 'content-type: text/html' + CGI_LINE_FEED;
                     ifsPath = 'somepath/SomeHTMLPage.html';
                     IFSOUT(ifsPath:headers:*off);​
            Binary file:
            Code:
                     Dcl-C CGI_LINE_FEED       const(x'15');
            
                     headers = 'content-type: application/pdf' + CGI_LINE_FEED;
                     ifsPath = 'somepath/SomeBinaryFile.pdf';
                     IFSOUT(ifsPath:headers:*on);​


            IFSOUT would
            • Write the header to STDOUT
              • (append a CGI_LINE_FEED if one not already present)
            • Write another CGI_LINE_FEED to close off the header
            • Read the IFS file using SQL, using a SQLTYPE (CLOB_FILE) variable, in 32k chunks
              • If IFSOUT parm 3 (binary) is *on, then the CLOB_FILE is CCCSID(*hex) so no character set conversion is done
                • Because content-type is a binary type, the webserver will not character set convert
              • Else the CLOB_FILE is ccsid(37) (job CCSID) so the IFS file is converted to CCSID 37 as it is read into the program
                • Because content-type is a text type, so the webserver will character set convert from 37
              • Call qtmhwrstout to write this block to STDOUT
            We have an old application and job CCSID defaults to 65535 instead of 37, which is why I need to explicitly declare the SQLTYPE as CCSID 37. If your IBMi is set up with a proper job CCSID then you may not need to specify a CCSID for the text-type processing

            Comment


            • #7
              Vectorspace solution is more up-to-date, but here is an oldie, but goodie using CGIDEV2 to write standard output.
              Attached Files
              Your friends list is empty!

              Comment

              Working...
              X