Code Monkey home page Code Monkey logo

pixelpress's Introduction

Pixelpress

CircleCI

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.

Installation

Add this line to your application's Gemfile:

gem 'pixelpress'

And then execute:

$ bundle

Or install it yourself as:

$ gem install pixelpress

Usage

Run the printer generator providing the name of your printer with methods to be generated:

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.

Example

$ rails g pixelpress:printer invoice customer_invoice delivery_document 	

will generate app/printers/invoice_printer.rb file which looks like this:

class InvoicePrinter < ApplicationPrinter
 	def customer_invoice
 		# 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/:

  • customer_invoice.pdf.erb
  • delivery_document.pdf.erb

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

localhost:3000/rails/printers

To use your printers in code, you can instantiate them just like mailers:

InvoicePrinter.customer_invoice.pdf # render a temporary pdf file
InvoicePrinter.customer_invoice.html # get the rendered document in html format

So you can send them to the client via a controller action:

class InvoicesController < ApplicationController
  def show
    document = InvoicePrinter.customer_invoice
    respond_to do |format|
      format.html { render html: document.html }
      format.pdf { send_data document.pdf.read, disposition: 'inline', type: 'application/pdf' }
    end
  end
end

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/Alex/pixelpress.

License

The gem is available as open source under the terms of the MIT License.

pixelpress's People

Contributors

adeynack avatar jensravens avatar msievers avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

icodein

pixelpress's Issues

Migrate Unit Tests

This project is missing quite a lot of test. Migrate them over from the main codebase to this repo.

Duplicate "Printer" in generator names

If you generate a printer like this:

rails g pixelpress:printer invoice_printer

it generates a class called InvoicePrinterPrinter. Although it's not supposed to be used this way, we should take care of this user error and rename it to InvoicePrinter instead.

Keep in mind that also rails g pixelpress:printer Billing::InvoicePrinter should work.

Rails generator

rails generate printer invoices invoice refund

add a rails generator that creates

  • a printer file (app/printers/invoices_printer.rb inherits from ApplicationPrinter)
  • the application printer if not present (app/printers/application_printer.rb)
  • a preview file in the spec directory (if present) spec/printers/previews/invoices_preview.rb
  • a spec file (if spec folder present) spec/printers/invoices_spec.rb
  • the view files (app/views/printers/invoices/invoice.pdf.erb, app/views/printers/invoices/refund.pdf.erb)

Add Printer Previews

One of the main features are printer previews that allow to preview a pdf via /rails/printers. This functionality needs to be migrated.

Readme

The readme needs important info on how to

  • installation
    • add printers to the loading path of rails
    • use the generators
    • use the preview functionality and mount the engine
  • create pdfs
  • use custom renderers
  • override the file name
  • use the preview functionality and mount the engine

Encoding issue

Hi! Could you please help me how to fix current bug in my project and what should I setup to get specific encoding, to be more precisely: Slavic ones? Here is the output of my pdf report.
image

Custom Renderers

Currently the library is using wheasyprint which is quite a hassle to setup, but yields the best result. It would be nice to have a standard adapter for wk2html which is available as a ruby gem instead of a python library and make this configurable.

ArgumentError: wrong number of arguments (given 1, expected 0)

Hi,
I was trying to use this gem, followed readme, but got this error when I try to run InvoicePrinter.customer_invoice.pdf

This is the error in the console:

Running via Spring preloader in process 3031238
Loading development environment (Rails 5.0.7.2)
irb(main):001:0> InvoicePrinter.customer_invoice.pdf
ArgumentError: wrong number of arguments (given 1, expected 0)
        from /home/dorijan/ruby/potrcko/app/printers/invoice_printer.rb:2:in `customer_invoice'
        from /home/dorijan/.rbenv/versions/2.4.10/lib/ruby/gems/2.4.0/gems/pixelpress-0.2.3/lib/pixelpress/instance_invocation.rb:7:in `method_missing'
        from (irb):1
        from /home/dorijan/.rbenv/versions/2.4.10/lib/ruby/gems/2.4.0/gems/railties-5.0.7.2/lib/rails/commands/console.rb:65:in `start'
        from /home/dorijan/.rbenv/versions/2.4.10/lib/ruby/gems/2.4.0/gems/railties-5.0.7.2/lib/rails/commands/console_helper.rb:9:in `start'
        from /home/dorijan/.rbenv/versions/2.4.10/lib/ruby/gems/2.4.0/gems/railties-5.0.7.2/lib/rails/commands/commands_tasks.rb:78:in `console'
        from /home/dorijan/.rbenv/versions/2.4.10/lib/ruby/gems/2.4.0/gems/railties-5.0.7.2/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
        from /home/dorijan/.rbenv/versions/2.4.10/lib/ruby/gems/2.4.0/gems/railties-5.0.7.2/lib/rails/commands.rb:18:in `<top (required)>'
        from /home/dorijan/.rbenv/versions/2.4.10/lib/ruby/gems/2.4.0/gems/activesupport-5.0.7.2/lib/active_support/dependencies.rb:293:in `require'
        from /home/dorijan/.rbenv/versions/2.4.10/lib/ruby/gems/2.4.0/gems/activesupport-5.0.7.2/lib/active_support/dependencies.rb:293:in `block in require'
        from /home/dorijan/.rbenv/versions/2.4.10/lib/ruby/gems/2.4.0/gems/activesupport-5.0.7.2/lib/active_support/dependencies.rb:259:in `load_dependency'
        from /home/dorijan/.rbenv/versions/2.4.10/lib/ruby/gems/2.4.0/gems/activesupport-5.0.7.2/lib/active_support/dependencies.rb:293:in `require'
        from /home/dorijan/ruby/potrcko/bin/rails:9:in `<top (required)>'
        from /home/dorijan/.rbenv/versions/2.4.10/lib/ruby/gems/2.4.0/gems/activesupport-5.0.7.2/lib/active_support/dependencies.rb:287:in `load'
        from /home/dorijan/.rbenv/versions/2.4.10/lib/ruby/gems/2.4.0/gems/activesupport-5.0.7.2/lib/active_support/dependencies.rb:287:in `block in load'
        from /home/dorijan/.rbenv/versions/2.4.10/lib/ruby/gems/2.4.0/gems/activesupport-5.0.7.2/lib/active_support/dependencies.rb:259:in `load_dependency'
        from /home/dorijan/.rbenv/versions/2.4.10/lib/ruby/gems/2.4.0/gems/activesupport-5.0.7.2/lib/active_support/dependencies.rb:287:in `load'
        from /home/dorijan/.rbenv/versions/2.4.10/lib/ruby/gems/2.4.0/gems/spring-2.1.1/lib/spring/commands/rails.rb:6:in `call'
        from /home/dorijan/.rbenv/versions/2.4.10/lib/ruby/gems/2.4.0/gems/spring-2.1.1/lib/spring/command_wrapper.rb:38:in `call'
        from /home/dorijan/.rbenv/versions/2.4.10/lib/ruby/gems/2.4.0/gems/spring-2.1.1/lib/spring/application.rb:220:in `block in serve'
        from /home/dorijan/.rbenv/versions/2.4.10/lib/ruby/gems/2.4.0/gems/spring-2.1.1/lib/spring/application.rb:180:in `fork'
        from /home/dorijan/.rbenv/versions/2.4.10/lib/ruby/gems/2.4.0/gems/spring-2.1.1/lib/spring/application.rb:180:in `serve'
        from /home/dorijan/.rbenv/versions/2.4.10/lib/ruby/gems/2.4.0/gems/spring-2.1.1/lib/spring/application.rb:145:in `block in run'
        from /home/dorijan/.rbenv/versions/2.4.10/lib/ruby/gems/2.4.0/gems/spring-2.1.1/lib/spring/application.rb:139:in `loop'
        from /home/dorijan/.rbenv/versions/2.4.10/lib/ruby/gems/2.4.0/gems/spring-2.1.1/lib/spring/application.rb:139:in `run'
        from /home/dorijan/.rbenv/versions/2.4.10/lib/ruby/gems/2.4.0/gems/spring-2.1.1/lib/spring/application/boot.rb:19:in `<top (required)>'
        from /home/dorijan/.rbenv/versions/2.4.10/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb:85:in `require'
        from /home/dorijan/.rbenv/versions/2.4.10/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb:85:in `require'
        from -e:1:in `<main>'

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.