mardi 26 janvier 2016

Rails caracal embedded image cannot be displayed

I have an issue with displaying images. This works:

docx.img 'http://ift.tt/1Pi8QDM', width: 200, height: 60

however this doesn't:

docx.img 'app/assets/images/logo.gif', width: 200, height: 60

The path is recognised, and the doc is generated, but the image is displayed in the doc with a cross and "This image cannot currently be displayed."

I asked the caracal owner on Github, who says:

Word documents embed images inside them and keep a sort of index of those assets.

When you provide an external url, Caracal uses Net::HTTP to read the data, save its data to a file, and index the embedded file via the supplied URL.

If you want to use a local file, you can do that, too, but the options signature is a bit different. Since Caracal can't use Net::HTTP to read the local file data, you'll need to do that yourself and pass Caracal the base64 data explicitly. You can use ruby's built-in File library to read the image content into a string and then use the Base64 library to encode the raw string into format that can be written into Caracal's ZIP file. As the README notes, you still want to provide the local path to the file in the img call, as this gives Caracal something to use when it indexes the resulting embedded file.

Without testing this pseudo-code at all, you'll want to do something like the following:

img_data = Base64.encode64( File.read('public/logo.png') )
docx.img 'public/logo.png' do
  data    img_data
  width   200
  height  60
end

However I still get the same issue when I do this. Anyone got any ideas?

Thanks!

Omar

Aucun commentaire:

Enregistrer un commentaire