Giter VIP home page Giter VIP logo

prawn_plus's Introduction

Prawn+

Prawn+ is a lightweight Rails Engine that wraps the Prawn PDF generator in order to render PDFs within Rails views. This allows you to build view templates which render PDFs using the Prawn syntax.

Features

  • Loads the Prawn gem by default (no Gemfile entry necessary).

  • Registers PDF as a MIME type.

  • Registers a template handler for rendering “.prawn” template view files.

Requirements

Setup

To install with security, run:

# 💡 Skip this line if you already have the public certificate installed.
gem cert --add <(curl --compressed --location https://alchemists.io/gems.pem)
gem install prawn_plus --trust-policy HighSecurity

To install without security, run:

gem install prawn_plus

You can also add the gem directly to your project:

bundle add prawn_plus

Once the gem is installed, you only need to require it:

require "prawn_plus"

Usage

Views

Within your views you can craft Prawn templates using Ruby code. For example, assuming there are document resources, then the following structure might exist:

/views/documents/show.html.slim
/views/documents/show.pdf.prawn

The show.html.slim could have a link to the PDF download. Example:

= link_to "PDF Download", action: "show", id: @document.id, format: "pdf"

The show.pdf.prawn file would contain the Prawn syntax for crafting the PDF. A simple example might look like this:

pdf.text "Hello, I'm a PDF!"

…​which would render the following output:

Basic Example

You could also render a more complex PDF with tabular information, for example:

pdf.text "Metals"
pdf.move_down 10
pdf.font_size = 10

data = [
  ["Name", "Atomic Number", "Price"],
  ["Mercury", "80", number_to_currency(10)],
  ["Platinum", "78", number_to_currency(25)],
  ["Titanium", "22", number_to_currency(50)]
]

pdf.table data, header: true, column_widths: [100, 50, 50], row_colors: ["FFFFFF", "E5ECF9"] do
  columns(0).align = :left
  columns(1..2).align = :right
  row(0).text_color = "FFFFFF"
  row(0).background_color = "000000"
  row(0).columns(0..2).font_style = :bold
  row(0).columns(0..2).align = :center
end

…​which would render the following output:

Complex Example

NOTE: The pdf object must always be referenced when making using of the Prawn syntax - it is initialized for you as a Prawn::Document instance.

Controllers

Within your controller, only the respond_to method is required. Example:

class DocumentsController < ApplicationController
  respond_to :pdf

  def show
  end
end

That’s it!

Development

To contribute, run:

git clone https://github.com/bkuhlmann/prawn_plus
cd prawn_plus
bin/setup

You can also use the IRB console for direct access to all objects:

bin/console

Tests

To test, run:

bin/rake

Credits

prawn_plus's People

Contributors

alex-ross avatar bkuhlmann avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

prawn_plus's Issues

setting Page size

Hello,

could you please tell a rails newbie, howto set the page size to A4 and the page layout to landscape for the resuling PDF?

As far as I know, it can't be done the .prawn layout file, the only way seems to be to give theese params by creating the doument.

Any ideas? Thanks in advance!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.