Forum: Ruby-Gnome 2 Using Ruby/Poppler without an X display

Posted by Martin Lucina (Guest)
on 2009-03-31 00:27
(Received via mailing list)
Hello.

I'd like to use Ruby/Poppler in a server context (Rails application) to
render PDF documents.  However, it appears that the Poppler bindings
require the presence of an X display.  For example, the following code 
will
not run outside of an X session:

----
#!/usr/bin/ruby
require 'poppler'
require 'cairo'

filename = ARGV[0]

document = Poppler::Document.new(filename)
page = document.get_page(0)
(width, height) = page.size
surface = Cairo::ImageSurface.new(width_pt, height_pt)
context = Cairo::Context.new(surface)
page.render(context)
surface.write_to_png('test.png')
----

The code fails with:

/usr/lib/ruby/1.8/gtk2.rb:12:in `init': Cannot open display: 
(Gtk::InitError)
  from /usr/lib/ruby/1.8/gtk2.rb:12
  from /usr/lib/ruby/1.8/poppler.rb:6:in `require'
  from /usr/lib/ruby/1.8/poppler.rb:6
  from ./test_pdf.rb:2:in `require'
  from ./test_pdf.rb:2

(This is with ruby-gnome2 0.17, poppler 0.8.7 on Debian lenny)

AFAICT native Poppler itself can be used independently of an X session, 
so
it'd be nice to have a way to achieve this using the Ruby bindings.

What would need to be done to make this work?

Any suggestions on how to proceed would be much appreciated.

Regards,

-mato
Posted by Pascal Terjan (Guest)
on 2009-03-31 01:13
(Received via mailing list)
Le mardi 31 mars 2009 à 00:25 +0200, Martin Lucina a écrit :
> What would need to be done to make this work?

Currently I think poppler will work fine without DISPLAY if you don't
have gtk2 binding installed but not if you have it :

begin
  require "gtk2"
rescue LoadError
end
begin
  require "cairo"
rescue LoadError
end

If you just remove LoadError to catch other exceptions while loading
gtk2 it may work
Posted by Martin Lucina (Guest)
on 2009-03-31 01:28
(Received via mailing list)
pterjan@linuxfr.org said:
> Currently I think poppler will work fine without DISPLAY if you don't
> have gtk2 binding installed but not if you have it :

I tried that, it didn't work:

/usr/lib/ruby/1.8/x86_64-linux/poppler.so: 
/usr/lib/ruby/1.8/x86_64-linux/poppler.so: undefined symbol: mGdk - 
/usr/lib/ruby/1.8/x86_64-linux/poppler.so (LoadError)
  from /usr/lib/ruby/1.8/poppler.rb:14
  from ./test_pdf.rb:2:in `require'
  from ./test_pdf.rb:2

There's some kind of dependency in there I don't understand...

-mato
Posted by Kouhei Sutou (Guest)
on 2009-04-01 16:46
(Received via mailing list)
Hi,

In <20090330232723.GG20372@nodbug.moloch.sk>
  "Re: [ruby-gnome2-devel-en] Using Ruby/Poppler without an X display" 
on Tue, 31 Mar 2009 01:27:24 +0200,
  Martin Lucina <mato@kotelna.sk> wrote:

> 
> There's some kind of dependency in there I don't understand...

Add the following code before "require 'poppler'":

  begin
    require 'gtk2'
  rescue Gtk::InitError
  end

Thanks,
--
kou
Posted by Martin Lucina (Guest)
on 2009-04-01 17:54
(Received via mailing list)
kou@cozmixng.org said:
> Add the following code before "require 'poppler'":
> 
>   begin
>     require 'gtk2'
>   rescue Gtk::InitError
>   end

Thanks Kou, that works.

Looks like I'll be using a different approach in any case, will write 
some
minimal Ruby bindings for MuPDF[1], since Poppler pulls in far too many
dependencies I don't really want installed on a server.

Thanks anyway!

-mato

[1] http://ccxvii.net/fitz/
Posted by Todd Fisher (taf2)
on 2010-06-11 04:47
A better approach might be to require 'gtk2/base' instead?  maybe 
poppler.rb should do that anyway...

Martin Lucina wrote:
> kou@cozmixng.org said:
>> Add the following code before "require 'poppler'":
>> 
>>   begin
>>     require 'gtk2'
>>   rescue Gtk::InitError
>>   end
> 
> Thanks Kou, that works.
> 
> Looks like I'll be using a different approach in any case, will write 
> some
> minimal Ruby bindings for MuPDF[1], since Poppler pulls in far too many
> dependencies I don't really want installed on a server.
> 
> Thanks anyway!
> 
> -mato
> 
> [1] http://ccxvii.net/fitz/
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.