Skip to content

Instantly share code, notes, and snippets.

@bastien
Created September 6, 2011 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bastien/1197679 to your computer and use it in GitHub Desktop.
Save bastien/1197679 to your computer and use it in GitHub Desktop.
Trying to write out files in UTF-8 that TextEdit, Pages, Word and open
Danish: æåø
Chinese: 我真累
# encoding: utf-8
require 'rubygems'
require 'rtf'
include RTF
module RTF
# https://github.com/lfarcy/rtf-extensions
# Modifies the TextNode class to manage utf8 character strings
class TextNode < Node
# This method generates the RTF equivalent for a TextNode object. This
# method escapes any special sequences that appear in the text.
def to_rtf
_text = @text || ''
_text = _text.gsub("\\", "\\\\\\").gsub("{", "\\{").gsub("}", "\\}")
_text.unpack('U*').map { |n| n < 128 ? n.chr : n < 256 ? "\\'#{n.to_s(16)}" : "\\u#{n}\\'3f" }.join('')
end
end
end
content = ""
f = File.open("input.txt", 'r')
content = f.read
f.close
document = Document.new(Font.new(Font::ROMAN, 'Times New Roman'))
document.paragraph << content
temp_file = File.open("output.rtf", 'w') do |f|
f.puts document.to_rtf
end
# encoding: utf-8
puts `file input.txt -I`
content = ""
f = File.open("input.txt", 'r', encoding:'UTF-8')
content = f.read
f.close
temp_file = File.open("output.txt", 'w') do |f|
f.puts content
end
f = File.open("output.txt", 'r', encoding:'UTF-8')
puts f.read
puts f.read.encoding
f.close
puts `file output.txt -I`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment