github.com/mundipagg/boleto-api@v0.0.0-20230620145841-3f9ec742599f/boleto/html.go (about)

     1  package boleto
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/base64"
     6  	"errors"
     7  
     8  	"github.com/boombuler/barcode"
     9  	"github.com/boombuler/barcode/twooffive"
    10  
    11  	"image/png"
    12  
    13  	"github.com/mundipagg/boleto-api/models"
    14  	"github.com/mundipagg/boleto-api/tmpl"
    15  	"github.com/mundipagg/boleto-api/util"
    16  )
    17  
    18  type HTMLBoleto struct {
    19  	View          models.BoletoView
    20  	ConfigBank    ConfigBank
    21  	Barcode64     string
    22  	Format        string
    23  	DigitableLine string
    24  }
    25  
    26  //HTML renderiza HTML do boleto
    27  func HTML(boletoView models.BoletoView, format string) (string, error) {
    28  
    29  	if boletoView.Barcode == "" {
    30  		return "", errors.New("boleto not found")
    31  	}
    32  	b := tmpl.New()
    33  	html := HTMLBoleto{
    34  		View:       boletoView,
    35  		ConfigBank: GetConfig(boletoView.Boleto),
    36  		Format:     format,
    37  	}
    38  	bcode, _ := twooffive.Encode(boletoView.Barcode, true)
    39  	orgBounds := bcode.Bounds()
    40  	orgWidth := orgBounds.Max.X - orgBounds.Min.X
    41  	img, _ := barcode.Scale(bcode, orgWidth, 50)
    42  	buf := new(bytes.Buffer)
    43  	err := png.Encode(buf, img)
    44  	html.Barcode64 = base64.StdEncoding.EncodeToString(buf.Bytes())
    45  	html.DigitableLine = textToImage(boletoView.DigitableLine)
    46  	templateBoleto, boletoForm := getTemplateBank(boletoView)
    47  	s, err := b.From(html).To(templateBoleto).Transform(boletoForm)
    48  	if err != nil {
    49  		return "", err
    50  	}
    51  	return s, nil
    52  }
    53  
    54  //MinifyHTML Minifica o HTML do um boleto a partir de um boletoView
    55  func MinifyHTML(bv models.BoletoView) string {
    56  	bhtml, _ := HTML(bv, "html")
    57  	return util.MinifyString(bhtml, "text/html")
    58  }
    59  
    60  func getTemplateBank(bv models.BoletoView) (string, string) {
    61  
    62  	switch bv.Boleto.BankNumber {
    63  	case models.Caixa:
    64  		return getTemplateCaixa()
    65  	case models.Itau:
    66  		return getTemplateItau(bv.Boleto.Title.BoletoType)
    67  	case models.Bradesco:
    68  		return getTemplatePerWallet(bv)
    69  	case models.Pefisa:
    70  		return getTemplatePefisa()
    71  	case models.Stone:
    72  		return getTemplateStone()
    73  	case models.JPMorgan:
    74  		return getTemplateJPMorgan()
    75  	default:
    76  		return getTemplateDefault(bv.Boleto.Title.BoletoType)
    77  	}
    78  }
    79  
    80  func getTemplatePerWallet(bv models.BoletoView) (string, string) {
    81  	switch int(bv.Boleto.Agreement.Wallet) {
    82  	case 25, 26:
    83  		return getTemplateBradescoShopFacil(bv.Boleto.Title.BoletoType)
    84  	default:
    85  		return getTemplateDefault(bv.Boleto.Title.BoletoType)
    86  	}
    87  }