Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Docs] Add note that WeasyPrint package is required #27

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
# Pixelpress
[![CircleCI](https://circleci.com/gh/nerdgeschoss/pixelpress/tree/master.svg?style=svg)](https://circleci.com/gh/nerdgeschoss/pixelpress/tree/master)

Modeled after `ActionMailer` this gem allows you to render PDFs from HTML via Rails templating engine. Also you can preview your PDF templates via a supplied engine during development.
Modeled after `ActionMailer`, this gem allows you to render PDFs from HTML via Rails' templating engine. Additionally, you can preview your PDF templates via a supplied Rails engine during development.

## Installation

Add this line to your application's Gemfile:
Add Pixelpress to your application's Gemfile:

```ruby
gem 'pixelpress'
gem "pixelpress"
```

And then execute:

$ bundle

Or install it yourself as:
```bash
$ bundle
```

$ gem install pixelpress
You'll also need [WeasyPrint](https://weasyprint.org/). It needs to be installed and added to your `PATH`, so that `which weasyprint` returns the location of the binary. Read their installation instructions here: https://doc.courtbouillon.org/weasyprint/stable/first_steps.html#installation

## Usage

Expand All @@ -26,37 +25,40 @@ Run the printer generator providing the name of your printer with methods to be
rails generate pixelpress:printer NAME [method_name1 method_name2 ...] [options]
```

This creates an `ApplicationPrinter` in `app/printers` and the corresponding subclass and also mounts the engine in your `config/routes.rb` file.
This creates the new printer in `app/printers`. If you run it the first time, it will also add an `ApplicationPrinter` and mount the Rails engine in your `config/routes.rb` file.

**Example**

```bash
$ rails g pixelpress:printer invoice customer_invoice delivery_document
$ rails g pixelpress:printer invoice customer_invoice delivery_document
```
will generate `app/printers/invoice_printer.rb` file which looks like this:

will generate an `app/printers/invoice_printer.rb` file with this content:

```ruby
class InvoicePrinter < ApplicationPrinter
def customer_invoice
# put your code here
end
def customer_invoice
# put your code here
end

def delivery_document
# put your code here
end
def delivery_document
# put your code here
end
end
```

Also this command will generate the corresponding templates as `.pdf.erb` files located in `app/views/printers/invoice/`:
The command will also generate corresponding templates as `.pdf.erb` files located in `app/views/printers/invoice/`:

- `customer_invoice.pdf.erb`
- `delivery_document.pdf.erb`

You can preview your documents by running `rails s` and go to

```
localhost:3000/rails/printers
http://localhost:3000/rails/printers
```

To use your printers in code, you can instantiate them just like mailers:
To use your printers in code, you can use them similarly to how you'd use mailers:

```ruby
InvoicePrinter.customer_invoice.pdf # render a temporary pdf file
Expand Down
12 changes: 10 additions & 2 deletions lib/pixelpress/renderers/weasyprint_renderer.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
class Pixelpress::WeasyPrintRenderer
class WeasyPrintInstallationError < StandardError; end

def render(input)
output = Tempfile.new

system executable_path, "--encoding", "utf-8", input.path, output.path, exception: true
return output
end

def version
`#{executable_path} --version`.chomp.scan(/(\d+\.\d+)/).flatten.first
end

private

def executable_path
`which weasyprint`.chomp || raise(StandardError, "could not find weasyprint")
path = `which weasyprint`.chomp

if path.blank?
raise WeasyPrintInstallationError.new("Unable to locate weasyprint binary. Please make sure it's in your PATH.")
end

path
end
end
10 changes: 10 additions & 0 deletions spec/renderers/weasyprint_renderer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,14 @@
page = reader.pages.sole
expect(page.text).to eq("Hêllo Wörld")
end

context "when the weasyprint binary can't be found" do
it "raises an error" do
expect(renderer).to receive(:`).with('which weasyprint').and_return("")

expect {
PDF::Reader.new(renderer.render(input))
}.to raise_error(/Unable to locate weasyprint/)
end
end
end
Loading