Subject:
[ruby-ffi] FFI check & questions
From:
Jeffrey Jones
Date:
2/26/12 7:26 PM
To:
ruby-ffi@googlegroups.com

Hello all.

I am in the process of looking at using the following library from ruby (http://www.datalogics.com/products/pdf2img/) using FFI but have run into a few issues mainly due to my lack of FFI experience and general lack of C++.

This library handles conversion from PDFs to image files. I know about things like ImageMagick and Ghostscript but they do not serve our purposes for various reasons.

Basically I wish to to the following:

1. Read a PDF file in ruby (From a file, database etc.)
2. Convert the PDF to a jpg image using the above library and get the image data back
3. Write the image in ruby (Or send_file in an web-app etc.)

There is a C++ example for the above library which does all of these things so what I want to do is replace the file reading / writing with ruby and then call the library functions using FFI.

The following Gist has the example C++ code (Posted with permission) and my Ruby code along side some example output. https://gist.github.com/1920005

C++ code overview
Lines 28 - 52 deal with opening a file
Lines 52 - 70 deal with conversion of the PDF to JPG
Lines 72 - 85 deal with writing the file (With line 79 actually getting the image data info a memory buffer)

The ruby code also contains the c++ function signatures above their ruby FFI definitions.

On line 22 of the ruby file I have defined the second argument as an :int, this is probably wrong but it suffices for the moment I think. I assume the real value is an Enumeration, struct or something along those lines. It is not documented so I will ask the original developers.

Questions:

1. On line 40 of the ruby file the required memory is apparently 4Gb, this is obviously wrong, does anyone know why? (I assume I have messed something up somewhere)

2. Can anyone see any other obvious mistakes on my part?

Currently the ruby program is accessing the Datalogics library functions directly.  Another option would be to create my own c library with a very simple API which calls the datalogics functions.

However while I seem to be able to create linux .so files without trouble I cannot create .dll files (Well, I can but FFI cannot open them, I assume I am missing some windows specific code). If anyone has a super simple windows dll template that I can go from that would be really helpful.

Any advice would be much appreciated.

Regards

Jeff