# encoding: utf-8 # # This example shows how to use inline formatting # require "#{File.dirname(__FILE__)}/../example_helper.rb" Prawn::Document.generate("inline_format.pdf") do |pdf| pdf.text("hello world\nhow are you? world, how are you now?", :inline_format => true) pdf.text("left: " + "hello world how are you? Goodbye. " * 8, :inline_format => true) pdf.text("right: " + "hello world how are you? " * 2, :inline_format => true, :align => :right) pdf.text("center: " + "hello world how are you? " * 2, :inline_format => true, :align => :center) pdf.text("\njustify: " + "hello world goodbye " * 12 + "the end ", :inline_format => true, :align => :justify) pdf.text("\njustify: " + "hello world goodbye " * 12 + "the end ", :inline_format => true, :align => :justify) pdf.text("\njustify: " + "hello world goodbye " * 12 + "the end ", :align => :justify) pdf.text("link: please make this clickable. Here we have Asuperscript link and A subscript link.", :inline_format => true) pdf.text("CMYK", :inline_format => true) file = "#{Prawn::BASEDIR}/data/fonts/Action Man.dfont" pdf.font_families["Action Man"] = { :normal => { :file => file, :font => "ActionMan" }, :italic => { :file => file, :font => "ActionMan-Italic" }, :bold => { :file => file, :font => "ActionMan-Bold" }, :bold_italic => { :file => file, :font => "ActionMan-BoldItalic" } } pdf.font("Action Man") pdf.text("\nhello world\nhow are you?", :inline_format => true) pdf.font("Helvetica") class Highlight def initialize(options) @color = options[:color] @document = options[:document] end def render_behind(fragment) original_color = @document.fill_color @document.fill_color = @color @document.fill_rectangle(fragment.top_left, fragment.width, fragment.height) @document.fill_color = original_color end end class FragmentBorder def initialize(options) @radius = options[:radius] @connect_corners = options[:connect_corners] @document = options[:document] end def render_in_front(fragment) box = fragment.bounding_box if @connect_corners @document.stroke_polygon(fragment.top_left, fragment.top_right, fragment.bottom_right, fragment.bottom_left) end @document.stroke_circle_at(fragment.top_left, :radius => @radius) @document.stroke_circle_at(fragment.top_right, :radius => @radius) @document.stroke_circle_at(fragment.bottom_right, :radius => @radius) @document.stroke_circle_at(fragment.bottom_left, :radius => @radius) end end highlight_callback = Highlight.new(:color => 'ffff00', :document => pdf) border_callback = FragmentBorder.new(:radius => 2.5, :connect_corners => true, :document => pdf) pdf.formatted_text([ { :text => "\n" }, { :text => "hello ", :callback => highlight_callback }, { :text => "world", :size => 24, :character_spacing => 0, :callback => [highlight_callback, border_callback] }, { :text => " hello" } ], :indent_paragraphs => 40, :character_spacing => -2) end