danfse

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 1, 2026 License: BSD-3-Clause Imports: 9 Imported by: 0

README

go-danfse-v2

Go Reference License

Gerador em Go para o Documento Auxiliar da Nota Fiscal de Serviço Eletrônica (DANFSe) no Padrão Nacional (v2.0).

Implementação baseada nas diretrizes de layout da Nota Técnica Nº 008 – Versão 1.0 (Maio de 2026).

⚠️ Status do Projeto

  • Como o layout é recente, o mapeamento de coordenadas foi validado contra os PDFs de exemplo oficiais da Receita Federal. Ainda não foi comparado contra documentos reais gerados em produção.
  • A extração das coordenadas base foi realizada com auxílio de IA e revisada manualmente. A renderização utiliza desenho absoluto.

💡 Motivação

O serviço público deixará de fornecer o PDF da NFS-e via API a partir de 1º de julho de 2026. O desenho e geração do DANFSe passam a ser de responsabilidade do emissor.

Este pacote efetua a montagem visual da nota sem depender de motores baseados em HTML, gerando o arquivo com coordenadas usando gopdf.

✨ Características

  • Sem CGO/Binários externos: Geração via gopdf, dispensando instalação de wkhtmltopdf ou ferramentas baseadas no Chrome/Puppeteer.
  • Containers menores: Adequado para imagens Docker baseadas em scratch ou ambientes serverless.
  • Layout aderente à norma: Mantém fontes, margens, espessuras e posicionamento exigidos pela legislação.
  • Stateless: A entrada (XML) e a saída (PDF) são manipuladas em []byte.

📦 Instalação

go get github.com/wevertonj/go-danfse-v2

🚀 Como Usar

Exemplo de uso básico:

package main

import (
	"log"
	"os"

	"github.com/wevertonj/go-danfse-v2"
)

func main() {
	renderer := danfse.NewDanfseRenderer()

	xmlData, err := os.ReadFile("nota_fiscal.xml")
	if err != nil {
		log.Fatalf("Falha ao ler XML: %v", err)
	}

	pdfBytes, err := renderer.Render(xmlData)
	if err != nil {
		log.Fatalf("Erro ao gerar DANFSe: %v", err)
	}

	os.WriteFile("danfse.pdf", pdfBytes, 0644)
}

🔌 Integração

A biblioteca não interage com o sistema de arquivos após sua distribuição, pois as fontes de texto (Liberation Sans) e a logo obrigatória oficial são embutidos no binário (//go:embed).

  1. Instancie o DanfseRenderer na inicialização do serviço.
  2. Forneça o []byte do XML autorizado recebido em sua aplicação para o método Render().
  3. O payload []byte do PDF gerado pode ser trafegado diretamente para APIs de email, storages de nuvem (S3) ou repassado ao cliente HTTP, dispensando alocação em disco.

🛠️ Testes

Usando os mocks embarcados no projeto:

go test ./...
go run examples/basic/main.go

📄 Licença

Este projeto é distribuído sob a licença BSD 3-Clause. Veja o arquivo LICENSE para mais detalhes.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DPSNode

type DPSNode struct {
	InfDPS InfDPSNode `xml:"http://www.sped.fazenda.gov.br/nfse infDPS"`
}

type DanfseRenderer

type DanfseRenderer interface {
	Render(xmlData []byte) ([]byte, error)
}

func NewDanfseRenderer

func NewDanfseRenderer() DanfseRenderer

type DestNode

type DestNode struct {
	CNPJ  string `xml:"http://www.sped.fazenda.gov.br/nfse CNPJ"`
	XNome string `xml:"http://www.sped.fazenda.gov.br/nfse xNome"`
	Fone  string `xml:"http://www.sped.fazenda.gov.br/nfse fone"`
	Email string `xml:"http://www.sped.fazenda.gov.br/nfse email"`
	CMun  string `xml:"http://www.sped.fazenda.gov.br/nfse cMun"`
	XMun  string `xml:"http://www.sped.fazenda.gov.br/nfse xMun"`
	CEP   string `xml:"http://www.sped.fazenda.gov.br/nfse CEP"`
	UF    string `xml:"http://www.sped.fazenda.gov.br/nfse UF"`
	End   struct {
		EndNac  EnderNacNode `xml:"http://www.sped.fazenda.gov.br/nfse endNac"`
		XLgr    string       `xml:"http://www.sped.fazenda.gov.br/nfse xLgr"`
		Nro     string       `xml:"http://www.sped.fazenda.gov.br/nfse nro"`
		XCpl    string       `xml:"http://www.sped.fazenda.gov.br/nfse xCpl"`
		XBairro string       `xml:"http://www.sped.fazenda.gov.br/nfse xBairro"`
	} `xml:"http://www.sped.fazenda.gov.br/nfse end"`
}

type EmitNode

type EmitNode struct {
	CNPJ     string       `xml:"http://www.sped.fazenda.gov.br/nfse CNPJ"`
	XNome    string       `xml:"http://www.sped.fazenda.gov.br/nfse xNome"`
	EnderNac EnderNacNode `xml:"http://www.sped.fazenda.gov.br/nfse enderNac"`
	Fone     string       `xml:"http://www.sped.fazenda.gov.br/nfse fone"`
	Email    string       `xml:"http://www.sped.fazenda.gov.br/nfse email"`
}

type EnderNacNode

type EnderNacNode struct {
	XLgr    string `xml:"http://www.sped.fazenda.gov.br/nfse xLgr"`
	Nro     string `xml:"http://www.sped.fazenda.gov.br/nfse nro"`
	XCpl    string `xml:"http://www.sped.fazenda.gov.br/nfse xCpl"`
	XBairro string `xml:"http://www.sped.fazenda.gov.br/nfse xBairro"`
	CMun    string `xml:"http://www.sped.fazenda.gov.br/nfse cMun"`
	XMun    string `xml:"http://www.sped.fazenda.gov.br/nfse xMun"`
	UF      string `xml:"http://www.sped.fazenda.gov.br/nfse UF"`
	CEP     string `xml:"http://www.sped.fazenda.gov.br/nfse CEP"`
}

type IBSCBSNode

type IBSCBSNode struct {
	Dest             DestNode `xml:"http://www.sped.fazenda.gov.br/nfse dest"`
	CIndOp           string   `xml:"http://www.sped.fazenda.gov.br/nfse cIndOp"`
	CLocalidadeIncid string   `xml:"http://www.sped.fazenda.gov.br/nfse cLocalidadeIncid"`
	XLocalidadeIncid string   `xml:"http://www.sped.fazenda.gov.br/nfse xLocalidadeIncid"`
	Valores          struct {
		VDescIncond    string `xml:"http://www.sped.fazenda.gov.br/nfse vDescIncond"`
		VCalcReeRepRes string `xml:"http://www.sped.fazenda.gov.br/nfse vCalcReeRepRes"`
		VBC            string `xml:"http://www.sped.fazenda.gov.br/nfse vBC"`
		UF             struct {
			PRedAliqUF  string `xml:"http://www.sped.fazenda.gov.br/nfse pRedAliqUF"`
			PIBSUF      string `xml:"http://www.sped.fazenda.gov.br/nfse pIBSUF"`
			PAliqEfetUF string `xml:"http://www.sped.fazenda.gov.br/nfse pAliqEfetUF"`
		} `xml:"http://www.sped.fazenda.gov.br/nfse uf"`
		Mun struct {
			PRedAliqMun  string `xml:"http://www.sped.fazenda.gov.br/nfse pRedAliqMun"`
			PIBSMun      string `xml:"http://www.sped.fazenda.gov.br/nfse pIBSMun"`
			PAliqEfetMun string `xml:"http://www.sped.fazenda.gov.br/nfse pAliqEfetMun"`
		} `xml:"http://www.sped.fazenda.gov.br/nfse mun"`
		Fed struct {
			PRedAliqCBS  string `xml:"http://www.sped.fazenda.gov.br/nfse pRedAliqCBS"`
			PCBS         string `xml:"http://www.sped.fazenda.gov.br/nfse pCBS"`
			PAliqEfetCBS string `xml:"http://www.sped.fazenda.gov.br/nfse pAliqEfetCBS"`
		} `xml:"http://www.sped.fazenda.gov.br/nfse fed"`
		Trib struct {
			GIBSCBS struct {
				CClassTrib string `xml:"http://www.sped.fazenda.gov.br/nfse cClassTrib"`
			} `xml:"http://www.sped.fazenda.gov.br/nfse gIBSCBS"`
		} `xml:"http://www.sped.fazenda.gov.br/nfse trib"`
	} `xml:"http://www.sped.fazenda.gov.br/nfse valores"`
	TotCIBS struct {
		GIBS struct {
			GIBSMunTot struct {
				VIBSMun string `xml:"http://www.sped.fazenda.gov.br/nfse vIBSMun"`
			} `xml:"http://www.sped.fazenda.gov.br/nfse gIBSMunTot"`
			GIBSUFTot struct {
				VIBSUF string `xml:"http://www.sped.fazenda.gov.br/nfse vIBSUF"`
			} `xml:"http://www.sped.fazenda.gov.br/nfse gIBSUFTot"`
			VIBSTot string `xml:"http://www.sped.fazenda.gov.br/nfse vIBSTot"`
		} `xml:"http://www.sped.fazenda.gov.br/nfse gIBS"`
		GCBS struct {
			VCBS string `xml:"http://www.sped.fazenda.gov.br/nfse vCBS"`
		} `xml:"http://www.sped.fazenda.gov.br/nfse gCBS"`
		VTotNF string `xml:"http://www.sped.fazenda.gov.br/nfse vTotNF"`
	} `xml:"http://www.sped.fazenda.gov.br/nfse totCIBS"`
}

type InfDPSNode

type InfDPSNode struct {
	TpAmb   string     `xml:"http://www.sped.fazenda.gov.br/nfse tpAmb"`
	DhEmi   string     `xml:"http://www.sped.fazenda.gov.br/nfse dhEmi"`
	Serie   string     `xml:"http://www.sped.fazenda.gov.br/nfse serie"`
	NDPS    string     `xml:"http://www.sped.fazenda.gov.br/nfse nDPS"`
	DCompet string     `xml:"http://www.sped.fazenda.gov.br/nfse dCompet"`
	Prest   PrestNode  `xml:"http://www.sped.fazenda.gov.br/nfse prest"`
	Toma    TomaNode   `xml:"http://www.sped.fazenda.gov.br/nfse toma"`
	Interm  IntermNode `xml:"http://www.sped.fazenda.gov.br/nfse interm"`
	Serv    ServNode   `xml:"http://www.sped.fazenda.gov.br/nfse serv"`
	IBSCBS  IBSCBSNode `xml:"http://www.sped.fazenda.gov.br/nfse IBSCBS"`
	Valores ValoresDPS `xml:"http://www.sped.fazenda.gov.br/nfse valores"`
}

type InfNFSeNode

type InfNFSeNode struct {
	ID            string   `xml:"Id,attr"`
	XLocEmi       string   `xml:"http://www.sped.fazenda.gov.br/nfse xLocEmi"`
	XLocPrestacao string   `xml:"http://www.sped.fazenda.gov.br/nfse xLocPrestacao"`
	NNFSe         string   `xml:"http://www.sped.fazenda.gov.br/nfse nNFSe"`
	CLocIncid     string   `xml:"http://www.sped.fazenda.gov.br/nfse cLocIncid"`
	XLocIncid     string   `xml:"http://www.sped.fazenda.gov.br/nfse xLocIncid"`
	XTribNac      string   `xml:"http://www.sped.fazenda.gov.br/nfse xTribNac"`
	AmbGer        string   `xml:"http://www.sped.fazenda.gov.br/nfse ambGer"`
	DhProc        string   `xml:"http://www.sped.fazenda.gov.br/nfse dhProc"`
	Emit          EmitNode `xml:"http://www.sped.fazenda.gov.br/nfse emit"`
	Valores       Valores  `xml:"http://www.sped.fazenda.gov.br/nfse valores"`
	DPS           DPSNode  `xml:"http://www.sped.fazenda.gov.br/nfse DPS"`
}

type InfoComplNode

type InfoComplNode struct {
	XOutInf string `xml:"http://www.sped.fazenda.gov.br/nfse xOutInf"`
}

type IntermNode

type IntermNode struct {
	CNPJ  string `xml:"http://www.sped.fazenda.gov.br/nfse CNPJ"`
	XNome string `xml:"http://www.sped.fazenda.gov.br/nfse xNome"`
	IM    string `xml:"http://www.sped.fazenda.gov.br/nfse IM"`
	Fone  string `xml:"http://www.sped.fazenda.gov.br/nfse fone"`
	Email string `xml:"http://www.sped.fazenda.gov.br/nfse email"`
	CMun  string `xml:"http://www.sped.fazenda.gov.br/nfse cMun"`
	XMun  string `xml:"http://www.sped.fazenda.gov.br/nfse xMun"`
	CEP   string `xml:"http://www.sped.fazenda.gov.br/nfse CEP"`
	UF    string `xml:"http://www.sped.fazenda.gov.br/nfse UF"`
	End   struct {
		EndNac  EnderNacNode `xml:"http://www.sped.fazenda.gov.br/nfse endNac"`
		XLgr    string       `xml:"http://www.sped.fazenda.gov.br/nfse xLgr"`
		Nro     string       `xml:"http://www.sped.fazenda.gov.br/nfse nro"`
		XCpl    string       `xml:"http://www.sped.fazenda.gov.br/nfse xCpl"`
		XBairro string       `xml:"http://www.sped.fazenda.gov.br/nfse xBairro"`
	} `xml:"http://www.sped.fazenda.gov.br/nfse end"`
}

type NFSe

type NFSe struct {
	XMLName xml.Name    `xml:"http://www.sped.fazenda.gov.br/nfse NFSe"`
	InfNFSe InfNFSeNode `xml:"http://www.sped.fazenda.gov.br/nfse infNFSe"`
}

func ParseXML

func ParseXML(xmlData []byte) (*NFSe, error)

ParseXML converte o XML da NFS-e em structs para uso na renderização

type PrestNode

type PrestNode struct {
	CNPJ    string `xml:"http://www.sped.fazenda.gov.br/nfse CNPJ"`
	IM      string `xml:"http://www.sped.fazenda.gov.br/nfse IM"`
	Fone    string `xml:"http://www.sped.fazenda.gov.br/nfse fone"`
	Email   string `xml:"http://www.sped.fazenda.gov.br/nfse email"`
	RegTrib struct {
		OpSimpNac   string `xml:"http://www.sped.fazenda.gov.br/nfse opSimpNac"`
		RegApTribSN string `xml:"http://www.sped.fazenda.gov.br/nfse regApTribSN"`
		RegEspTrib  string `xml:"http://www.sped.fazenda.gov.br/nfse regEspTrib"`
	} `xml:"http://www.sped.fazenda.gov.br/nfse regTrib"`
}

type ServNode

type ServNode struct {
	LocPrest struct {
		XLocPrestacao  string `xml:"http://www.sped.fazenda.gov.br/nfse xLocPrestacao"`
		CPaisPrestacao string `xml:"http://www.sped.fazenda.gov.br/nfse cPaisPrestacao"`
		CMun           string `xml:"http://www.sped.fazenda.gov.br/nfse cMun"`
		XMun           string `xml:"http://www.sped.fazenda.gov.br/nfse xMun"`
		CEP            string `xml:"http://www.sped.fazenda.gov.br/nfse CEP"`
	} `xml:"http://www.sped.fazenda.gov.br/nfse locPrest"`
	Obra struct {
		CObra string `xml:"http://www.sped.fazenda.gov.br/nfse cObra"`
		Art   string `xml:"http://www.sped.fazenda.gov.br/nfse art"`
	} `xml:"http://www.sped.fazenda.gov.br/nfse obra"`
	CServ struct {
		CTribNac  string `xml:"http://www.sped.fazenda.gov.br/nfse cTribNac"`
		CTribMun  string `xml:"http://www.sped.fazenda.gov.br/nfse cTribMun"`
		CNBS      string `xml:"http://www.sped.fazenda.gov.br/nfse cNBS"`
		XTribNac  string `xml:"http://www.sped.fazenda.gov.br/nfse xTribNac"`
		XTribMun  string `xml:"http://www.sped.fazenda.gov.br/nfse xTribMun"`
		XDescServ string `xml:"http://www.sped.fazenda.gov.br/nfse xDescServ"`
	} `xml:"http://www.sped.fazenda.gov.br/nfse cServ"`
	InfoCompl InfoComplNode `xml:"http://www.sped.fazenda.gov.br/nfse infoCompl"`
}

type TomaNode

type TomaNode struct {
	CNPJ  string `xml:"http://www.sped.fazenda.gov.br/nfse CNPJ"`
	XNome string `xml:"http://www.sped.fazenda.gov.br/nfse xNome"`
	IM    string `xml:"http://www.sped.fazenda.gov.br/nfse IM"`
	Fone  string `xml:"http://www.sped.fazenda.gov.br/nfse fone"`
	Email string `xml:"http://www.sped.fazenda.gov.br/nfse email"`
	End   struct {
		EndNac  EnderNacNode `xml:"http://www.sped.fazenda.gov.br/nfse endNac"`
		XLgr    string       `xml:"http://www.sped.fazenda.gov.br/nfse xLgr"`
		Nro     string       `xml:"http://www.sped.fazenda.gov.br/nfse nro"`
		XCpl    string       `xml:"http://www.sped.fazenda.gov.br/nfse xCpl"`
		XBairro string       `xml:"http://www.sped.fazenda.gov.br/nfse xBairro"`
	} `xml:"http://www.sped.fazenda.gov.br/nfse end"`
}

type Valores

type Valores struct {
	VLiq       string `xml:"http://www.sped.fazenda.gov.br/nfse vLiq"`
	VTotalRet  string `xml:"http://www.sped.fazenda.gov.br/nfse vTotalRet"`
	TpBM       string `xml:"http://www.sped.fazenda.gov.br/nfse tpBM"`
	VCalcBM    string `xml:"http://www.sped.fazenda.gov.br/nfse vCalcBM"`
	VCalcDR    string `xml:"http://www.sped.fazenda.gov.br/nfse vCalcDR"`
	VBC        string `xml:"http://www.sped.fazenda.gov.br/nfse vBC"`
	PAliqAplic string `xml:"http://www.sped.fazenda.gov.br/nfse pAliqAplic"`
	VISSQN     string `xml:"http://www.sped.fazenda.gov.br/nfse vISSQN"`
}

type ValoresDPS

type ValoresDPS struct {
	VServPrest struct {
		VServ string `xml:"http://www.sped.fazenda.gov.br/nfse vServ"`
	} `xml:"http://www.sped.fazenda.gov.br/nfse vServPrest"`
	Trib struct {
		TotTrib struct {
			PTotTribSN string `xml:"http://www.sped.fazenda.gov.br/nfse pTotTribSN"`
		} `xml:"http://www.sped.fazenda.gov.br/nfse totTrib"`
		TribMun struct {
			TribISSQN   string `xml:"http://www.sped.fazenda.gov.br/nfse tribISSQN"`
			CLocIncid   string `xml:"http://www.sped.fazenda.gov.br/nfse cLocIncid"`
			XLocIncid   string `xml:"http://www.sped.fazenda.gov.br/nfse xLocIncid"`
			CPaisResult string `xml:"http://www.sped.fazenda.gov.br/nfse cPaisResult"`
			TpImunidade string `xml:"http://www.sped.fazenda.gov.br/nfse tpImunidade"`
			ExigSusp    struct {
				TpSusp    string `xml:"http://www.sped.fazenda.gov.br/nfse tpSusp"`
				NProcesso string `xml:"http://www.sped.fazenda.gov.br/nfse nProcesso"`
			} `xml:"http://www.sped.fazenda.gov.br/nfse exigSusp"`
			TpRetISSQN string `xml:"http://www.sped.fazenda.gov.br/nfse tpRetISSQN"`
			BM         struct {
				VRedBCBM string `xml:"http://www.sped.fazenda.gov.br/nfse vRedBCBM"`
			} `xml:"http://www.sped.fazenda.gov.br/nfse BM"`
		} `xml:"http://www.sped.fazenda.gov.br/nfse tribMun"`
		TribFed struct {
			VRetIRRF  string `xml:"http://www.sped.fazenda.gov.br/nfse vRetIRRF"`
			VRetCP    string `xml:"http://www.sped.fazenda.gov.br/nfse vRetCP"`
			VRetCSLL  string `xml:"http://www.sped.fazenda.gov.br/nfse vRetCSLL"`
			PisCofins struct {
				VPis           string `xml:"http://www.sped.fazenda.gov.br/nfse vPis"`
				VCofins        string `xml:"http://www.sped.fazenda.gov.br/nfse vCofins"`
				TpRetPisCofins string `xml:"http://www.sped.fazenda.gov.br/nfse tpRetPisCofins"`
			} `xml:"http://www.sped.fazenda.gov.br/nfse piscofins"`
		} `xml:"http://www.sped.fazenda.gov.br/nfse tribFed"`
	} `xml:"http://www.sped.fazenda.gov.br/nfse trib"`
	VDedRed struct {
		VDR string `xml:"http://www.sped.fazenda.gov.br/nfse vDR"`
	} `xml:"http://www.sped.fazenda.gov.br/nfse vDedRed"`
	VDescCondIncond struct {
		VDescIncond string `xml:"http://www.sped.fazenda.gov.br/nfse vDescIncond"`
		VDescCond   string `xml:"http://www.sped.fazenda.gov.br/nfse vDescCond"`
	} `xml:"http://www.sped.fazenda.gov.br/nfse vDescCondIncond"`
}

Directories

Path Synopsis
cmd
update_ibge command
examples
basic command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL