Skip to content

Instantly share code, notes, and snippets.

@jlsync
Created June 30, 2011 19:01
Show Gist options
  • Save jlsync/1056925 to your computer and use it in GitHub Desktop.
Save jlsync/1056925 to your computer and use it in GitHub Desktop.
How to prevent prawn wrap_by_char? charred.pdf
#!/usr/bin/env ruby
require 'rubygems'
require 'prawn'
@pdf = Prawn::Document.new
x = 0
texts = [ "alexander", "the alexander", "the alexander building" ]
texts.each do |text|
@pdf.bounding_box([x, 700], :height => 250, :width => 150) do
@pdf.stroke_bounds
@pdf.text_box text, :size => 100, :min_font_size => 2, :overflow => :shrink_to_fit
x += 200
end
end
PrawnSplitWord = Class.new(StandardError)
module Prawn
module Core
module Text
module Formatted #:nodoc:
class LineWrap #:nodoc:
def wrap_by_char(segment)
raise PrawnSplitWord
end
end
end
end
end
end
module Prawn
module Text
module Formatted
class Box
def shrink_to_fit(text)
begin
wrap(text)
rescue PrawnSplitWord
@everything_printed = false
end
until @everything_printed || @font_size <= @min_font_size
@font_size = [@font_size - 0.5, @min_font_size].max
@document.font_size = @font_size
begin
wrap(text)
rescue PrawnSplitWord
@everything_printed = false
end
end
end
end
end
end
end
x = 0
texts.each do |text|
@pdf.bounding_box([x, 300], :height => 250, :width => 150) do
@pdf.stroke_bounds
@pdf.text_box text, :size => 100, :min_font_size => 2, :overflow => :shrink_to_fit
x += 200
end
end
@pdf.render_file 'charred.pdf'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment