decode the base 64 pdf file

Homepage Clovertech Forums Read Only Archives Cloverleaf General decode the base 64 pdf file

  • Creator
    Topic
  • #50862
    Wilson Wat
    Participant

    Does anyone have any experiences to decode base 64 pdf file in the window environment? My goal is to decode the pdf file and write it to a designated folder across the network and build a link to it…Thx

Viewing 6 reply threads
  • Author
    Replies
    • #67808
      Charlie Bursell
      Participant

      Wilso:

      I think I araedy mentioned this but you can decode it with the Tcl decode command.  It should be a simple matter to build a script that accepts the base-64 data, decodes it and writes it to a .*pdf file.

      Make sure your file reads and writes are done in binary mode

    • #67809
      James Mestack
      Participant

      Something appears to be lost in the decode process from base-64 to pdf, can someone determine if I am doing this correctly?

      Win 2003 Server SP2

      CL 5.7 R2

      See attachment for inbound message in .msg file.

      See attachments for what pdf should look like and what it does look like?

      proc code:

      package require base64

      foreach segment $segments {

      if {[cequal [crange $segment 0 2] “OBX”]} {

         set obx_fields [split $segment $field_delim]

         set list_size [llength $obx_fields]

         while { $list_size < 6 } {

      lappend obx_fields “”

      set list_size [llength $obx_fields]

         }

         set obx_5_fields [split [lindex $obx_fields 5] $comp_delim]

         set list_size [llength $obx_5_fields]

         while { $list_size < 5 } {

      lappend obx_5_fields “”

      set list_size [llength $obx_5_fields]

         }

         set pdf_data [base64::decode [lindex $obx_5_fields 4]]

         break

      }

         }

         set PDFFileName [cconcat $HciProcessesDir “/” $myprocessName “/que_pdf_data/CentralLogic_Test.pdf”]

         if { [catch {set outfile [open $PDFFileName w]} cerr] } {

      puts [cconcat “–> WARNING: Error Creating ” $PDFFileName ” File – ” $cerr “r”]

         } else {

      puts $outfile $pdf_data

      close $outfile

         }

    • #67810
      Jim Kosloskey
      Participant

      James,

      First are you positive the inbound message has that fiel base-64 encoded?

      If this is done following the HL/7 standard OBX-2 should have the proper data type (I think that is ED) and then OBX-5 should have the proper structure which includes a component that tells you the encoding technique deployed.

      But even if the standard is not literally followed (not surprising), you need to make sure the data is actually base-64 encoded. Otherwise it most certainly could contain one or moer of the characters specified in MSH-1 and MSH-2 causing your parsing to fail and possibly not giving you all of the data you want.

      The Base64 package will attempt to decode whatever you give it even if it is not Base64 encoded.

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

    • #67811
      James Mestack
      Participant

      I included the smat files with the test records received from vendor, you’ll have to take off the “.txt” extension to utilize as I had to rename so I could upload.

      The vendor states it is base-64 from pdf.

    • #67812
      Charlie Bursell
      Participant

      I have a hard time with the code but if you are writing it to a file and it is indeed a PDF document you have to change translation type to binary

      set PDFFileName [cconcat $HciProcessesDir “/” $myprocessName “/que_pdf_data/CentralLogic_Test.pdf”]

      Better stated as [file join  $HciProcessesDir

                           que_pdf_data CentralLogic_Test.pdf]

      # w+ will reate file if it does not exist

      if { [catch {open $PDFFileName w+} outfile]} {

             echo –> WARNING: Error Creating  $PDFFileNamenFile – $outfilen

             —- May need an error return here


        } else {

                 fconfigure $outfile -translation binary

                 puts $outfile $pdf_data

                 close $outfile

      }

      The looping to get segment data is not very ergonomic.  Take a look at the lsearch command

    • #67813
      James Mestack
      Participant

      Right on! I added the “w+” and “fconfigure” and now receive readable output.  Also, thank you for the code tidbits.

      Being my first time doing this, on the PDF view, I see there is an “X” in the top box, supposedly our logo.  Is that not supposed to be embedded or do we need a seperate file?  Probably need to discuss with vendor?

      See attached new output…

    • #67814
      Christopher Wells
      Participant

      Charlie,

      I know this is an old post, but the file writing snippet really helped me out.  Just starting to work with decoded PDF files myself.

      Thanks,

      Christopher Wells

Viewing 6 reply threads
  • The forum ‘General’ is closed to new topics and replies.

Forum Statistics

Registered Users
5,117
Forums
28
Topics
9,293
Replies
34,435
Topic Tags
286
Empty Topic Tags
10