'Adding inline image with Caracal in Ruby on Rails

I'm trying to get text to show up in-line with an image using the Caracal ruby gem.

It currently does this:

enter image description here

But I need it to instead do this instead: (accomplished in Word by going to Format Picture -> Wrap Text -> In Front of Text)

enter image description here

The first example was generated with the following:

docx.img image.path do
  width   250
  height  200
  align   :left
end
docx.p "This is a cool picture", align: 'right'

I edited the document in Word in order to create the second example.



Solution 1:[1]

The difference between your two versions of the image is that the first one is an inline object (i.e. it is aligned like any other text in the paragraph) and the second one is a floating object anchored to the current paragraph.

I'm not too familiar with the Caracal library, but from a first glance at the sources it seems that this library only supports inline images.

To still get what you want you should be able to do the following:

  • Create the document as you do it already now (this will do all the complicated work of adding the image file at the correct place inside the package and adding relationships in the .rels package part and content types and the like)
  • Then open the generated docx package file using a standard Ruby zip library
  • Adapt the XML in the document.xml part of the docx package to contain a floating representation of your image.

The last step probably is the most complicated because you need to have some basic understanding of the OpenXML. I suggest you have a look at the OpenXML of the second document which has been generated by Word and try to adjust your OpenXML accordingly.

Solution 2:[2]

For anyone that still need it.

You can use tables, without visible borders, to achieve something that looks close to that, put the image in a cell with same width as image + plus margins, and the text in the other, then use cell_style to put the text where u want.

In more complex cases you can use TableCellModel with another table inside, but beware that Caracal doesn't provide, yet, no mean to really control row height, and rows in different tables nested in the same table may have different heights, depending on the content.

Not exactly what was asked, but much easier than editing XML by yourself.

Solution 3:[3]

I also ran into this issue for inlining image in Header of document. you can apply same logic for adding image in document. I've gone through this particular issue's discussion on the Caracal gem

Add support for header and footer content

You can have a look on my fix for this issue over there in the discussion to inline image with text. https://github.com/urvin-compliance/caracal/issues/94#issuecomment-1102596261

For reference:

logo = Caracal::Core::Models::TableCellModel.new do
  img "path of image", width: 70, height: 50
end

org_name = Caracal::Core::Models::TableCellModel.new do
  p "Name of Organization"
end

docx.table [[logo, org_name]] do
  cell_style rows[0][0], colspan: 2
end

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dirk Vollmar
Solution 2 Alan Thomaz
Solution 3 Kinjal Chotalia