🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

br_danfe

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

br_danfe

2.0.0
Rubygems
Version published
Maintainers
1
Created
Source

BrDanfe

Code Climate

This gem generates PDF files for Brazilian DANFE (Documento Auxiliar da Nota Fiscal Eletrônica) from a valid NF-e XML. It also can generates PDF file for CC-e (Carta de Correção Eletrônica).

See an example here.

This gem requires ruby >= 2.0.

Supported NF-e versions

XML versionSupported?
1.00no
2.00yes
3.10yes
4.00yes

Installing

gem install br_danfe

Usage

DANFE - Documento Auxiliar da Nota Fiscal Eletrônica

Usage in Ruby

xml = File.read("spec/fixtures/nfe/v2.00/nfe_simples_nacional.xml")

danfe = BrDanfe::Danfe.new(xml)
danfe.options.logo = "spec/fixtures/logo.png"
danfe.options.logo_dimensions = { width: 100, height: 100 }
danfe.save_pdf("output.pdf")

Usage in Rails Controller

class DanfeController < ApplicationController
  def new
    invoice = Invoice.find(params[:id])
    xml_as_string = invoice.generate_xml # your method that generates the NF-e's xml

    danfe = BrDanfe::Danfe.new(xml_as_string)

    send_data danfe.render_pdf, filename: "danfe.pdf", type: "application/pdf"
  end
end

Usage in Rails Controller with more than one xml

class DanfeController < ApplicationController
  def new
    invoices = Invoice.where(ids: params[:ids])
    xmls_as_string = []

    invoices.each do |invoice|
        xmls_as_string << invoice.generate_xml # your method that generates the NF-e's xml
    end

    danfe = BrDanfe::Danfe.new(xmls_as_string)

    send_data danfe.render_pdf, filename: "danfe.pdf", type: "application/pdf"
  end
end

Options

  • logo_path: Path of sender's logo image.
  • logo_dimensions: Dimensions of the logo. Ex: logo_dimensions = { width: 100, height: 90 }

CC-e - Carta de Correção Eletrônica

Usage in Ruby

xml = File.read("cce.xml")

cce = BrDanfe::Cce.new(xml)
cce.save_pdf("nfe.pdf")

Usage in Rails Controller

class CCeController < ApplicationController
  def new
    invoice = Invoice.find(params[:id])
    xml_as_string = invoice.generate_xml # your method that generates the CC-e's xml

    cce = BrDanfe::Cce.new(xml_as_string)

    send_data cce.render_pdf, filename: "cce.pdf", type: "application/pdf"
  end
end

DAMDFE - Documento Auxiliar do Manifesto Eletrônico de Documentos Fiscais

Usage in Ruby

xml = File.read("mdfe.xml")

mdfe = BrDanfe::Mdfe.new(xml)
mdfe.save_pdf("mdfe.pdf")

Usage in Rails Controller

class MdfeController < ApplicationController
  def new
    mdfe = Mdfe.find(params[:id])
    xml_as_string = mdfe.generate_xml # your method that generates the MDF-e's xml

    mdfe = BrDanfe::Mdfe.new(xml_as_string)

    send_data mdfe.render_pdf, filename: "mdfe.pdf", type: "application/pdf"
  end
end

I18n

By default, your rails application must be configured to pt-Br.

If you need to customize some message or field label, you can override the content of pt-Br.yml file.

Development - With Docker

You needs to build docker container like above:

$ docker-compose build

You can run all RSpec specs using:

$ docker-compose run --rm br_danfe rspec

Development - Without Docker

You needs to install all necessaries dependencies using bunder like above:

$ bundle install

You can run all RSpec specs using:

$ bundle exec rspec

About tests

If you modify something that caused general visual changes at output pdf's, so you have to rebuild the fixtures pdf files.

You can do this simply deleting the fixture pdf file. The have_same_content_of matcher will recreate the fixture in the next time you run the bundle exec rspec command.

Environment variables

The following variables are necessary to be set:

Environment varDevelopment?Test?CI?Production?Data
TZnonoyesnoAmerica/Sao_Paulo
BUNDLE_PATHnonoyesnovendor/bundle
CC_TEST_REPORTER_IDnonoyesnoget at codeclimate
RAILS_ENVnonoyesnotest

Code coverage

Code coverage is available through of SimpleCov. Just run bundle exec rspec and open the coverage report in your browser.

Fake data for generating new fixtures

If you need to generate new danfes for using as fixtures, please don't use real data.

These data bellow are suggested:

Sender:

FieldContent
NameNome do Remetente Ltda
TradeNome Fantasia do Remetente Ltda
AddressRua do Remetente
Number123
ComplementCasa
NeighborhoodBairro do Remetente
CEP12.345-678
CitySão Paulo - SP
Phone(11) 1234-5678
CNPJ62.013.294/0001-43
IE526.926.313.553

Recipient:

FieldContent
NameNome do Destinatário PJ Ltda
AddressRua do Destinatário PJ
Number345
ComplementSL 1 e 2
NeighborhoodBairro do Destinatário PJ
CEP23.456-789
CitySumaré - SP
Phone(19) 2345-6789
CNPJ71.058.884/0001-83
IE671.008.375.110
IE ST611.724.092.039
FieldContent
NameNome do Destinatário PF
AddressRua do Destinatário PF
Number345
Complement1o Andar
NeighborhoodBairro do Destinatário PF
CEP98.765-432
CityVinhedo - SP
Phone(16) 4567-8901
CPF485.325.574-57
RG11.420.947-9

Transporter:

FieldContent
NameNome do Transportador Ltda
AddressRua do Transportador, 456
CityVotorantim - SP
CNPJ71.434.064/0001-49
IE964.508.990.089

You can generate new data using 4devs generators.

Deploy to Rubygems

  • To deploy a new version:
  • Define the new version at lib/br_danfe/version.rb;
  • Execute $ bundle;
  • Commit all and push to Github;
  • Send the new version to Rubygems
  • $ bundle exec rake release
  • Choose the new tag (Ex.: v0.10.1)
  • Fill the Release title (Ex.: 0.10.1)
  • Click at Generate release notes
  • Click at Publish release
  • Done!

You can build or install the gem using one of the above tasks

$ bundle exec rake build    # Build br_danfe-X.X.X.gem into the pkg directory
$ bundle exec rake install  # Build and install br_danfe-X.X.X.gem into system gems

Login

$ gem signin
$ gem signout

Contributing

We encourage you to contribute to BrDanfe!

  • Fork it
  • Create your feature branch (git checkout -b my-new-feature)
  • Commit your changes (git commit -am 'Add some feature')
  • Push to the branch (git push origin my-new-feature)
  • Create new Pull Request

Ruby DANFE gem

This project is based on Ruby DANFE gem.

License

BrDanfe is released under the MIT License.

FAQs

Package last updated on 28 Apr 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts