Git Product home page Git Product logo

md_to_pdf's Introduction

ActsAsPdf

Quick way to convert markdown to a template pdf.

Installation

Add this line to your application's Gemfile:

gem 'acts_as_pdf'

And then execute:

$ bundle

Or install it yourself as:

$ gem install acts_as_pdf

Usage

bill.rb

class Bill < ApplicationRecord

  acts_as_pdf :field, {method: :generate_bill, font_size: '10', font_name: 'Arial', line_height: '180%'}

  def generate_bill user, company, obj # has many arguments or no one 
    {
      variables: {
        "//COMPANY_NAME//": company.company_name,
        "//COMPANY_FULL_ADDRESS//": company.company_full_address,
        "//COMPANY_USER_POST//": company.company_user_post,
        "//USER_FIRST_NAME//": user.first_name,
        "//USER_LAST_NAME//": user.last_name,
        "//OBJECT_PRICE//": obj.salary,
        "//OBJECT_CURRENCY//": obj.currency
      },
      options: { # optional, only if you need footer or header
        header: { font_size: 16, center: 'header' }
        footer: { font_size: 10, center: 'footer'}
      }
    }
  end

end

bills_controller.rb

class BillsController < ApplicationController
  preview_pdf
  # preview_pdf {view: 'preview.pdf'}
end

views/bills/preview.pdf.prawn

@pdf

views/bills/_form.html.haml

= simple_form_for resource do |f|
    = f.text :field # with the markdown gem you want (be aware of whitespaces)
    = f.submit 'preview', name: 'preview',  action: 'preview', formtarget: "_blank", id: "first"
    = f.submit class: 'btn btn-custom inline', id: "second"

:javascript # in other file
  $('#first').removeAttr('data-disable-with')
  $('#second').removeAttr('data-disable-with')

routes.rb

    resources :bills do
      post :create, on: :member, action: :preview
      patch :update, on: :member, action: :preview
    end

Now you can use it at runtime:

users_controller.rb

class UsersController < ApplicationController

  def after_buying_something
    bill = Bill.find(param[:bill_id])
    product = Product.find(param[:product_id])
    @pdf = bill.generate_pdf(params, [@user, product.company, product])
  end
end

views/bills/after_buying_something.pdf.prawn

@pdf

License

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

md_to_pdf's People

Contributors

ocarta-l avatar jeanne-b- avatar

Stargazers

d-r-e avatar cryptomonein avatar  avatar Jilano avatar

Watchers

 avatar  avatar

Forkers

aperez42 bastes

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.