Using Ruby/Poppler without an X display


#1

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:inrequire’
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


#2

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


#3

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


#4

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


#5

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/


#6

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/