我想查看是否有人这样做。在Ruby Search PDF中,突出显示找到的文本,导出页面的JPG
在ruby中,我想打开PDF并在那里搜索文本。我找到的任何文本都想用黄色突出显示,然后返回发现文本为jpg的页面。有没有人做过这个?
谢谢, 克雷格
我想查看是否有人这样做。在Ruby Search PDF中,突出显示找到的文本,导出页面的JPG
在ruby中,我想打开PDF并在那里搜索文本。我找到的任何文本都想用黄色突出显示,然后返回发现文本为jpg的页面。有没有人做过这个?
谢谢, 克雷格
如果你乐于使用C的扩展,你可以用红宝石实现这一目标-gnome2绑定。你需要poppler和gdk_pixbuf2宝石。
这些宝石的API文档是有点吝啬,但你可以找到什么有在http://ruby-gnome2.sourceforge.jp/
require 'poppler'
require 'gdk_pixbuf2'
SCALE = 2
filename = "source.pdf"
doc = Poppler::Document.new(filename)
page = doc.get_page(0)
# render the page to an in-memory buffer
width, height = *page.size
buf = Gdk::Pixbuf.new(Gdk::Pixbuf::COLORSPACE_RGB, true, 8, width*SCALE, height*SCALE)
page.render(0, 0, width*SCALE, height*SCALE, SCALE, 0, buf)
# copy the rendered buffer into an pixmap for further editing
map = Gdk::Pixmap.new(nil, width*SCALE, height*SCALE, 24)
map.draw_pixbuf(nil, buf, 0, 0, 0, 0, -1, -1, Gdk::RGB::DITHER_NONE, 0, 0)
# setup highlight color and blend function
gc = Gdk::GC.new(map) # graphics context
gc.rgb_fg_color = Gdk::Color.new(65535, 65535, 0)
gc.function = Gdk::GC::AND
# find each match and highlight it. The co-ordinate maths is ugly but
# necesary to convert from PDF co-ords to Pixmap co-ords
page.find_text("the").each do |match|
matchx = match.x1 * SCALE
matchy = (height - match.y2) * SCALE
matchw = (match.x2-match.x1) * SCALE
matchh = (match.y2-match.y1) * SCALE
map.draw_rectangle(gc, true, matchx, matchy, matchw, matchh)
end
# save the buffer to a JPG
newbuf = Gdk::Pixbuf.from_drawable(nil, map, 0, 0, width*SCALE, height*SCALE)
newbuf.save("foo.jpg", "jpeg")
是,JRuby或致电通过命令行选项的罐子?在这种情况下,你可以使用Java iText库和沿东西这些答案的线
我宁可不使用JRuby的,因为我从来没有使用过。从ruby on rails的命令行调用itext会有什么性能/缩放问题吗? – fregas
谢谢,我会试试看。看起来像我需要! – fregas
太棒了。如果解决不了,请不要忘记接受答案 –