Generate DANFE (Auxiliary Document of the Electronic Invoice) from an XML
This Node.js project allows you to parse a Brazilian NF-e XML file and generate its DANFE (Documento Auxiliar da Nota Fiscal Eletrônica), typically as a PDF output.
- About
- Requirements
- Installation
- Usage
- Project Structure
- Configuration
- Available Scripts
- Contributing
- License
- Examples
This application:
- Reads and parses a NF-e XML file.
- Extracts all the relevant data for the DANFE.
- Generates a PDF (or other supported format) with the proper DANFE layout.
- Includes an example output file:
output.pdf.
Repository: gabrielgv456/danfenodejs
Before running the project, make sure you have:
- Node.js (LTS version recommended)
- npm or yarn package manager
- (Optional) Native libraries for PDF generation or HTML rendering (depending on your OS)
-
Clone the repository:
git clone https://github.com/gabrielgv456/danfenodejs.git cd danfenodejs -
Install dependencies:
npm install # or yarn install -
(Optional) Install system dependencies if your PDF generator library requires them.
Example of usage inside a Node.js project:
const path = require('path');
const { generateDanfe } = require('./src'); // adjust path as needed
const xmlPath = path.join(__dirname, 'invoice.xml');
const outputPath = path.join(__dirname, 'danfe.pdf');
generateDanfe(xmlPath, outputPath)
.then(() => {
console.log('DANFE successfully generated at', outputPath);
})
.catch(err => {
console.error('Failed to generate DANFE:', err);
});xmlPath: path to the input NF-e XML fileoutputPath: path where the generated DANFE PDF will be saved
You can also run it directly from the command line if a CLI script is added:
node src/index.js ./examples/invoice.xml ./output/danfe.pdfdanfenodejs/
├── src/
│ ├── index.js ← main entry point
│ ├── parser/ ← XML parsing logic
│ ├── template/ ← HTML/templates for DANFE
│ ├── pdf/ ← PDF generation logic
│ └── utils/ ← helper utilities
├── output.pdf ← sample generated DANFE
├── package.json
├── yarn.lock / package-lock.json
└── README.md
You may configure:
- Templates: HTML/CSS templates for DANFE layout
- Paper size & margins: A4, margins, orientation
- Branding: custom logos or headers
- Fonts: make sure to include the fonts you want to embed in the PDF
Environment variables may be added in the future for better flexibility.
Defined inside package.json, for example:
{
"scripts": {
"start": "node src/index.js",
"dev": "nodemon src/index.js",
"test": "jest",
"lint": "eslint .",
"generate": "node generate.js",
"clean": "rm -rf output.pdf"
}
}npm start→ runs the main applicationnpm run dev→ runs in development mode with hot reloadnpm test→ runs unit tests (if implemented)npm run lint→ checks code stylenpm run generate→ generates a DANFE using a sample XML
Contributions are welcome! To contribute:
- Open an issue to discuss new features or bugs.
- Fork this repository.
- Create a feature branch:
git checkout -b feature/my-feature. - Commit your changes with a clear message.
- Push to your branch and submit a Pull Request.
This project is licensed under the MIT License.
See the LICENSE file for details.
node src/index.js ./examples/sample.xml ./output/sample.pdfconst { generateDanfe } = require('danfenodejs');
generateDanfe('./invoice.xml', './danfe.pdf')
.then(() => console.log('Done!'))
.catch(console.error);🚀 With this, you’re ready to generate DANFE from NF-e XML files using Node.js!