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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Martin L. (Guest)
on 2009-03-31 02: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
Pascal T. (Guest)
on 2009-03-31 03:13
(Received via mailing list)
Le mardi 31 mars 2009 à 00:25 +0200, Martin L. 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
Martin L. (Guest)
on 2009-03-31 03:28
(Received via mailing list)
removed_email_address@domain.invalid 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
Kouhei S. (Guest)
on 2009-04-01 18:46
(Received via mailing list)
Hi,

In <removed_email_address@domain.invalid>
  "Re: [ruby-gnome2-devel-en] Using Ruby/Poppler without an X display"
on Tue, 31 Mar 2009 01:27:24 +0200,
  Martin L. <removed_email_address@domain.invalid> 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
Martin L. (Guest)
on 2009-04-01 19:54
(Received via mailing list)
removed_email_address@domain.invalid 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/
Todd F. (Guest)
on 2010-06-11 06:47
A better approach might be to require 'gtk2/base' instead?  maybe
poppler.rb should do that anyway...

Martin L. wrote:
> removed_email_address@domain.invalid 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/
This topic is locked and can not be replied to.