2012-04-19 60 views
0

我有一个rtf文件。
我想打开该文件使用Ruby来替换图像的一些占位符。
我环顾四周ruby​​-rtf。但是,它不适合用于分析RTF,只为打造全新的RTFRuby将图像插入到Rtf

任何人都可以帮助我吗?

回答

0

我找到了解决方案!

首先,你必须一个占位符添加到您的RTF文件。为此,你必须用MS Word打开rtf文件并将其放置在特定的位置。例如,您的占位符可能被命名为@@ placeholder @@

其次,打开保存在步骤1中的RTF文件用记事本,这样清理的占位符。女士有时可能会在您的占位符中添加“垃圾”标签(@@ pla rtlch \ fcs1 \ af39 \ afs20 \ ltrch \ fcs0 \ b \ f39 \ fs20 \ insrsid14101935 \ charrsi ceholder @@)。您应该纠正它,以便获得@@ placeholder @@

三,使用ruby-rtf。 最后,插入图片代码:

# using Document class of ruby-rtf plugin 
document = Document.new(nil) 
image = document.image("path_to_image/check.png") 

rtf_file = IO.read("path_to_rtf/my.rtf") 
rtf_file.gsub!('@@[email protected]@', image.to_rtf) 
File.open("#{Rails.root}/my_userdata/new_blank.rtf", "wb") {|f| f.write rtf_file } 

我希望这将是对别人有帮助的。