Note
Looking for the old API? Pin the legacy gem:
gem 'prawn-component', github: 'halo/prawn-component', tag: 'v1.0.0'New here? Use the renamed gem from RubyGems (no require option needed):
gem 'prawn-widget'TL;DR
This is an implementation of the popular view_component gem for Prawn.
Usage
You define a widget just like you would using the view_component gem. The only difference, of course, is that you
don't have templates. We're in all-ruby land and the implementation resides in the #call method.
# app/widgets/hello_world_widget.rb
class HelloWorldWidget < Prawn::Widget
def call
pdf.text 'Hello World!'
end
endTo render a widget into an existing PDF, use the #render_in method:
# app/models/generate_some_pdf.rb
pdf = Prawn::Document.new
HelloWorldWidget.new.render_in(pdf)You can use renders_one and renders_many to use subwidgets.
In fact, when using your widget with a block, you must at least use renders_one to pass in your content.
Slots are set with with_* methods (e.g. box.with_title, blog.with_article), matching the current view_component API.
# app/widgets/box_widget.rb
class BoxWidget < Prawn::Widget
renders_one :title, TitleWidget
renders_one :description
renders_many :comments
def call
pdf.text 'This is a box'
title.draw # In Rails you would simply use `#to_s` but we use `#draw`.
description.draw
comments.each(&:draw)
end
end
# app/widgets/title_widget.rb
class TitleWidget < Prawn::Widget
renders_one :content
def call
pdf.font_size 14
content.draw
end
endAnd then render the widget:
# app/models/generate_some_pdf.rb
pdf = Prawn::Document.new
BoxWidget.new.render_in(pdf) do |box|
box.with_title do
pdf.text 'I am the title'
end
box.with_description do
pdf.text 'I am the description'
end
box.with_comment do
pdf.text 'First comment'
end
box.with_comment do
pdf.text 'Second comment'
end
endYou can define params and options like with the calls gem (built on dry-initializer).
# app/widgets/title_widget.rb
class SayItWidget < Prawn::Widget
option :message
def call
pdf.text message
end
endAnd call it like so:
# app/models/generate_some_pdf.rb
pdf = Prawn::Document.new
SayItWidget.new(message: 'Hello!').render_in(pdf) do |box|Of course you could render a widget inside of another widget:
# app/widgets/blog_widget.rb
class BlogWidget < Prawn::Widget
renders_many :articles, ArticleWidget
def call
pdf.push 'Welcome to my blog'
articles.each(&:draw)
end
end
# app/widgets/article_widget.rb
class ArticleWidget < Prawn::Widget
renders_one :title
renders_one :body
def call
TitleWidget.new.render_in(pdf) do |title|
title.with_content do
title.draw
end
end
body.draw
end
endAnd render it:
# app/models/generate_some_pdf.rb
pdf = Prawn::Document.new
@comments = load_some_comments!
BlogWidget.new.render_in(pdf) do |blog|
blog.with_article do |article|
article.with_title do
pdf.text 'Breaking news'
end
article.with_body do
pdf.text 'The weather is good.'
end
end
endCaveats
Rendering a Rails view is pretty simple compared to a PDF. Because you simply pre-generate an HTML String that you can wrap or concatenate.
In Prawn, the "view" is a living PDF document and you cannot pre-generate any content.
That means, we cannot use wrappers like this:
# Would be nice but does NOT work.
ButtonWidget.new.render_in(pdf) do
pdf.text 'I am a button'
endInstead we're forced to use this:
# Cumbersome, but it has its advantages.
ButtonWidget.new.render_in(pdf) do |button|
button.with_title do
pdf.text 'I am a button'
end
endThe technical reason behind this is this:
# This causes a problem for the block/yield mechanism.
BoxWidget.new.render_in(pdf) do |box|
box.with_title do
pdf.text 'I am the title'
end
pdf.text 'I am the description'
endIn this example, the box title is first properly set-up when the entire block is exectued. But executing that block already modifies the pdf in-place (with the description). Which makes this uninituitive (in Rails you would just capture the output of the description block and keep it for later use, that's not possible in a PDF).
So you have to use either or: blocks never have arguments or always (which we chose here for this gem).
History
When it comes to "reusable widgets", it's really hard to find the balance between too much opinion and too much flexibility.
For Prawn it might look like this or like this or this. For Rails you might come up with something like this but that's not good enough, so let's use this and after years of iterations, we finally landed here.
So, now that that API is somewhat stable, I felt it was time to use it with Prawn.
Copyright
MIT 2021 halo, SPDX-License-Identifier: MIT.
See MIT-LICENSE.