Inline images

59 views
Skip to first unread message

fred.a...@gmail.com

unread,
Mar 18, 2016, 12:44:22 PM3/18/16
to Prawn
Hi,

I am working on a tool to convert Markdown into PDF's and I'm using Prawn for the PDF output.

The basics seem to work well. I can do bold, italic, paragraphs, headings, indentation etc. and this works really well.

I have now started to tackle the problem of inline images which seem to be beyond my knowledge. Before I look into the insides of Prawn I would like to know if I'm on the right track or perhaps missing something obvious.

What I'm trying to achieve is that an image is somewhere in the middle of line, almost as if they are text. These inline images are supported by extended Markdown. 

My logic so far is that I should first render the text before the image using a pdf.text_box, then render the image using pdf.image and then finally the text behind the image again using pdf.text_box. I know the wanted dimensions of the image so in order to aline the bottom of the image with the text I have to offset the Y-coordinate for the text a bit so that both the image and the text neatly fit below the line above. 

The flow is like this:

  • compute the difference between the height of the image and the font size.
  • If this height is >0 move the cursor down this many points so that we know that the image fits nicely below the line above.
  • Plot the leading text before the image using pdf.text_box
  • Plot the image using pdf.image at an x-offset of the width of the leading text
  • Plot the trailing text after the image using pdf.text_box with an x-offset of the width of the leading text plus the width of the image.
For simple cases this logic works well. However there are many cases that break this logic. E.g. if the leading text spans more than one line this breaks. 

So alternatively I should be able to use :overlow => "truncate" on the text box and plot each line one by one. That however requires that I know before I plot if this line contains the image or not. If it doesn't I should use a normal line distance and if it does I should add the extra line distance to make it fit. 

In an ideal world this could be something that is inline in the text itself, e.g. "This is a line with an <image name='example.png' height=30> in the middle"

Can I know upfront if a text box will give excess_text?

Would an extension of inline images make sense, i.e. does this fit the style guide?

Am I on the right track or is this completely off? Any advise is appreciated.

Regards,

      Fred

J. Edward Ellis

unread,
Mar 18, 2016, 1:22:44 PM3/18/16
to prawn...@googlegroups.com
There was a recent post that kramdown and asciidoctor convert markdown to PDF. You might want to have a look at those. 

J. Edward Ellis
Sent from my iPhone
--
You received this message because you are subscribed to the Google Groups "Prawn" group.
To unsubscribe from this group and stop receiving emails from it, send an email to prawn-ruby+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Thomas Leitner

unread,
Mar 18, 2016, 2:12:47 PM3/18/16
to prawn...@googlegroups.com
On 2016-03-18 09:37 -0700 fred.a...@gmail.com wrote:
> I am working on a tool to convert Markdown into PDF's and I'm using
> Prawn for the PDF output.

You might wanna have a look at
https://github.com/gettalong/kramdown/blob/master/lib/kramdown/converter/pdf.rb
which implements a basic Markdown to Prawn PDF converter.

Cheers,
Thomas

fred.a...@gmail.com

unread,
Mar 18, 2016, 3:46:14 PM3/18/16
to Prawn
Thanks for the suggestion. If I install kramdown and use its pdf mode I actually get the warning that it doesn't support inline images:

Warning: Rendering span images is not supported for PDF converter

Given that you seem to be author of Kramdown I should mention that the gemspec misses the reference to prawn-table. It gives an error that it cannot read prawn/table. Easily fixed by running 'gem install prawn-table'.

    Fred

Thomas Leitner

unread,
Mar 18, 2016, 4:08:26 PM3/18/16
to prawn...@googlegroups.com
On 2016-03-18 12:46 -0700 fred.a...@gmail.com wrote:
> Thanks for the suggestion. If I install kramdown and use its pdf mode
> I actually get the warning that it doesn't support inline images:
>
> Warning: Rendering span images is not supported for PDF converter

Yeah, I know, I just wanted to show you that some of the work was
already done.

As for inline images: I did write this converter a long time ago and I
didn't find an easy way to do inline images. However, you may want to
look at
https://github.com/asciidoctor/asciidoctor-pdf/tree/master/lib/asciidoctor-pdf/formatted_text
where it seems that AsciiDoctor does support inline images.

> Given that you seem to be author of Kramdown I should mention that
> the gemspec misses the reference to prawn-table. It gives an error
> that it cannot read prawn/table. Easily fixed by running 'gem install
> prawn-table'.

Thanks! I have added prawn-table as development dependency.

Cheers
Thomas

fred.a...@gmail.com

unread,
Mar 18, 2016, 4:44:42 PM3/18/16
to Prawn
Thanks. Will look into this.
Reply all
Reply to author
Forward
0 new messages