Skip to content

Commit

Permalink
Start with a blank PDF
Browse files Browse the repository at this point in the history
  • Loading branch information
bradgessler committed Feb 28, 2024
1 parent 8356179 commit 36625a7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 40 deletions.
29 changes: 16 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,41 @@ If bundler is not being used to manage dependencies, install the gem by executin
```ruby
require "phlex/pdf"

class ApplicationComponent < Phlex::PDF
def before_template
text "Before #{self.class.name}"
end
class PDFComponent < Phlex::PDF
end

class PDFPage < PDFComponent
# Creates a new page
def before_template = create_new_page

def after_template
text "After #{self.class.name}"
text "Page #{document.page_number}"
end
end

class BoxComponent < ApplicationComponent
class BoxComponent < PDFComponent
def view_template
text "I'm a box"
yield
end
end

class NoticeComponent < ApplicationComponent
class MyPage < PDFPage
def initialize(title:)
@title = title
end

def view_template
text "Hello World!"
text @title

render BoxComponent.new do
text "Look! I'm a box inside a box!"
end
end
end

# Render it to a file
NoticeComponent.render_file "hello.pdf"

# Or to a string
NoticeComponent.render
# Render the PDF
MyPage.new(title: "This is a PDF!").to_pdf
```

## Development
Expand Down
18 changes: 10 additions & 8 deletions lib/phlex/pdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

module Phlex
class PDF
class Error < StandardError; end

include Prawn::View

def call(document = Prawn::Document.new, &block)
def document = @document

def call(document = self.class.blank, &block)
@document = document
around_template do
if block_given?
Expand Down Expand Up @@ -44,6 +44,11 @@ def after_template
nil
end

def to_pdf(...)
call
@document.render(...)
end

def yield_content(&block)
return unless block_given?

Expand Down Expand Up @@ -76,11 +81,8 @@ def render(renderable, &block)
end

class << self
def document(...)
Prawn::Document.new.tap do |document|
new(...).call(document)
end
end
# Components should override this method to define their view.
def blank = Prawn::Document.new(skip_page_creation: true)
end
end
end
41 changes: 22 additions & 19 deletions spec/phlex/pdf_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,14 @@

require "phlex/pdf"

class ApplicationComponent < Phlex::PDF
def before_template
text "Before #{self.class.name}"
end

def view_template
text self.class.name
end
class PDFComponent < Phlex::PDF
end

def after_template
text "After #{self.class.name}"
end
class PDFPage < PDFComponent
def before_template = start_new_page
end

class WarningComponent < ApplicationComponent
class WarningComponent < PDFComponent
def initialize(message)
@message = message
end
Expand All @@ -26,19 +19,19 @@ def view_template
end
end

class HeaderComponent < ApplicationComponent
class HeaderComponent < PDFComponent
def view_template
text "Header"
text "Header", size: 24
end
end

class FooterComponent < ApplicationComponent
class FooterComponent < PDFComponent
def view_template
text "Footer"
text "Page #{document.page_number}"
end
end

class BodyComponent < ApplicationComponent
class BodyComponent < PDFComponent
def view_template
render "Body"
yield if block_given?
Expand All @@ -49,7 +42,7 @@ def greet(name)
end
end

class PageComponent < ApplicationComponent
class MyPage < PDFPage
def initialize(title, subtitle: )
@title = title
@subtitle = subtitle
Expand Down Expand Up @@ -83,8 +76,18 @@ def calm
end
end

class PDFDocument < PDFComponent
def view_template
render [
MyPage.new("Hi", subtitle: "There"),
MyPage.new("Friendly", subtitle: "Pal"),
MyPage.new("Whats", subtitle: "Up")
]
end
end

RSpec.describe Phlex::PDF do
it "generates a PDF" do
PageComponent.document("Hi", subtitle: "There").render
PDFDocument.new.to_pdf
end
end

0 comments on commit 36625a7

Please sign in to comment.