Read PDF file and write to folder using TCL

Clovertech Forums Cloverleaf Read PDF file and write to folder using TCL

Tagged: 

  • Creator
    Topic
  • #119418
    Brian Sweetland
    Participant

      I’m using a thread to read a local folder where a customer drops files.
      PROTOCOL: fileset-local
      In the Inbound tab, I’ve added a TCL proc to: TPS Inbound Data
      Shortened code…

      run {
      keylget args MSGID mh
      set msg [msgget $mh]
      set pdfFileName “C:\\Temp\\myFile.pdf”
      set outputFile [open $pdfFileName a+]
      puts $outputFile $msg;flush $outputFile
      close $outputFile
      lappend dispList “KILL $mh”
      }

      If this is a text file, I need to make some changes and write to this folder.
      I’ve found that the customer sometimes drops PDF files in this folder as well.
      I’ve added logic to the TCL proc that if the extension is a PDF, ignore my custom modifications and just write the file as is, however, it’s not converting the data exactly as it reads it.
      It seems it’s adding \CR\LF throughout the msg which means I cannot open the PDF from the folder. Get an error that there is something wrong with the msg.

      Not sure why this converts the Text data with no issue, but not the PDF.
      Any help would be appreciated.

      On a side note, is there a way I can set up my inbound thread to only read *.TXT files and ignore PDF or any other files that may get placed there?

    Viewing 5 reply threads
    • Author
      Replies
      • #119419
        Jeff Dinsmore
        Participant

          Try to config the file write as binary.

          after opening the file:

          fconfigure $outputFile -translation binary

          You’ll probably also want the file opened in write mode. Otherwise, it will append if the file already exists.

          set outputFile [open $pdfFileName w]

          You also don’t need the flush since that’s automatically done when you close the file. It doesn’t hurt anything, but it’s redundant.

          Jeff Dinsmore
          Chesapeake Regional Healthcare

        • #119420
          Jim Kosloskey
          Participant

            On your Fileset Local protocol you can use the DirParse UPoC to control which files are picked up.

            On later releases I believe there are GUI settings @ DirParse which allow some control otherwise you will need some Tcl there.

            I have an argument driven DirParse proc set which provides most of the control you might need. If you would like that proc, email me.

            email: jim.kosloskey@jim-kosloskey.com 29+ years Cloverleaf, 59 years IT - old fart.

          • #119421
            Jim Kosloskey
            Participant

              Also if you are on a later release of Cloverleaf you can set the IB Fileset Local to a Style to read the entire file as one message and also binary (if needed).

              You could also route the IB to an OB Fileset protocol instead of using Tcl to write the file.

              In the above case if you want the OB File Name to match the IB File Name you can use a Tcl proc @ the OB Thread Outbound UPoC.

              I have a proc that does that as well. If you want that proc, email me.

              email: jim.kosloskey@jim-kosloskey.com 29+ years Cloverleaf, 59 years IT - old fart.

            • #119422
              Charlie Bursell
              Participant

                I agree with Jeff you need to write the PDF as binary. However with Tcl now there is no need for the fconfigure command to do that.

                set outputFile [open $pdfFileName wb]

                From the Tcl help file:
                r
                Open the file for reading only; the file must already exist. This is the default value if access is not specified.
                r+
                Open the file for both reading and writing; the file must already exist.
                w
                Open the file for writing only. Truncate it if it exists. If it does not exist, create a new file.
                w+
                Open the file for reading and writing. Truncate it if it exists. If it does not exist, create a new file.
                a
                Open the file for writing only. If the file does not exist, create a new empty file. Set the file pointer to the end of the file prior to each write.
                a+
                Open the file for reading and writing. If the file does not exist, create a new empty file. Set the initial access position to the end of the file.

                All of the legal access values above may have the character b added as the second or third character in the value to indicate that the opened channel should be configured as if with the fconfigure -translation binary option, making the channel suitable for reading or writing of binary data.

              • #119441
                Brian Sweetland
                Participant

                  Thanks for the feedback.
                  I tried the binary in the fconfigure and that did not work, but then I realized I had to set my Thread>Protocol>Encoding to binary (was ASCII).
                  This worked. I can now read all the files in the folder, I break it down based on file extension and then format/create/copy based on that.

                  IF PDF:
                  set outputFile [open $pdfFileName wb]
                  fconfigure $outputFile -translation binary -encoding binary
                  puts $outputFile $msg
                  close $outputFile

                  Jim pointed out that in my version of Cloverleaf, that in the protocol properties, there is a setting called “File Mask” where you can filter what extension types the CL will process.
                  Now knowing this, I may go back and rewrite my code to only process the TXT file, then use TCL to copy the PDF file where it won’t go through any sort of formatting. I think this approach may be cleaner in the end, less confusing code.

                  I was reading the Infor documents and it says you can add multiple wildcards separated by a comma, no spaces. But this doesn’t seem to work for me.
                  { IBFILEMASK *.txt,*.tmp }
                  Tried to encapsulate it with {} and this doesn’t work either. Also tried the single quotes.
                  { IBFILEMASK {*.txt,*.tmp} }
                  I really only need the TXT file, but I am reading the TXT file and if the PDF was not there I would write the file back to the import folder as *.tmp and wait for the next scan. Now when I read the tmp file, I know it’s the second attempt and if the PDF still isn’t there, I log the error and delete the msg.
                  If I can’t get the multiple files working here, I can always come up with a counter.

                • #119442
                  Charlie Bursell
                  Participant

                    Good for you Brian.
                    Just to let you know the fconfigure is redundant. You have already opened the file in binary mode with the “wb” in the open statement.

                Viewing 5 reply threads
                • You must be logged in to reply to this topic.