prawn icon indicating copy to clipboard operation
prawn copied to clipboard

formatted_text_box and shrink_to_fit don't work

Open donnfelker opened this issue 5 years ago • 2 comments

First - thank you for the library. This thing is great. :) I think I found a bug I'd like to report. I'd fix it, I'm just not sure how/where to go to fix it.

Steps to reproduce:

Save this file as example.rb and run it via ruby example.rb

require 'date'
require 'prawn'

timestamp = (Time.now.to_f * 1000).to_i

generated_pdf_name = "render-#{timestamp}.pdf"
Prawn::Document.generate(generated_pdf_name, page_layout: :landscape, page_size: "A0") do 
    formatted_text_box [{ 
        text: "This really long text should fit inside of the box and it should shrink to fit as this is a test.", 
        size: 124, 
        styles: [:bold]
    }], 
        at: [485, 1700], 
        width: 2240, 
        height: 345,
        overflow: :shrink_to_fit
end 

You'll notice that the text looks like this: image

The text is truncated.

Next, change the shrink_to_fit to expand (overflow: :expand) and re-run the file and you'll get this: image

The :expand option seems to work, yet the :shrink_to_fit does not.

Expected results: :shrink_to_fit would shrink the text to fill the box as it does with text_box.

I cannot use text_box because I need additional formatting (highlighters). Those have been removed in this example for brevity. The :shrink_to_fit option works fine on a regular `text_box though.

TLDR; It seems that the shrink_to_fit option does not work with the formatted_text_box. Why would that occur? Am I doing something incorrectly?

donnfelker avatar Jun 23 '19 13:06 donnfelker

Same problem here, did you find a workaround?

xijo avatar Apr 09 '22 10:04 xijo

So it does work if you apply the overflow option to the formatted_text_box itself:

doc.formatted_text_box [
  {
    text: "This is my text",
    styles: [:bold, :italic],
    callback: PrawnCustomUnderline.new(document: doc),
  }
], at: [0, 340], overflow: :shrink_to_fit, size: 20, height: 20, width: 500

xijo avatar Apr 09 '22 10:04 xijo