Skip to content

Instantly share code, notes, and snippets.

@ebot
Created February 9, 2012 17:02
Show Gist options
  • Save ebot/1781135 to your computer and use it in GitHub Desktop.
Save ebot/1781135 to your computer and use it in GitHub Desktop.
Sample encoding and decoding hl7 messages from EDM ADR
#!/usr/bin/env ruby -w
# encoding: UTF-8
require 'base64'
input = ''
unless ARGV[0] == 'hl7'
input = File.read 'doc_base64.txt'
else
input = File.read( 'hl7_samples/working_hl7_sample.txt' ).split( '^Base64^' )[1]
end
decoded = Base64.decode64 input
pdf_base64 = ''
lines_read = 0
decoded.each_line do |line|
lines_read += 1
pdf_base64 << line if lines_read > 8
end
pdf = File.new 'doc.pdf', 'wb'
pdf << Base64.decode64( pdf_base64 )
pdf.close
#!/usr/bin/env ruby -w
# encoding: UTF-8
require 'base64'
mime_header =<<EOF
Mime Header (Add this to Base64 encoded PDF):
------=_Part_0_12717604.1285180108138
Content-Type: multipart/mixed
------=_Part_1_22540508.1285180108139
Content-Type: application/pdf
Content-Transfer-Encoding: BASE64
Content-Disposition: attachment; filename=AD.pdf
EOF
# read in the EDM ADR HL7 message and get the PDF Base64 String
edm_input = File.new 'hl7_samples/02-edm_adr_hl7.txt', 'r'
document = edm_input.read.split( '^Base 64^' )[1]
edm_input.close
# Add the mime type and re-encode in Base64
document = Base64.encode64( mime_header + document )
doc_encode = File.new 'doc_base64.txt', 'w'
doc_encode << document
doc_encode.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment