github.com/hi-fi/sss/print@v0.0.0-20230212204231-b8661fcee5d7/pkg/pdf/utils.go (about)

     1  package pdf
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/base64"
     6  	"fmt"
     7  	"image"
     8  	"net/http"
     9  	"strings"
    10  
    11  	"github.com/hi-fi/sss/print/pkg/model"
    12  
    13  	"github.com/phpdave11/gofpdf"
    14  )
    15  
    16  var gofpdfDir string
    17  
    18  func addSongs(data model.Model, pdf *gofpdf.Fpdf, bottomMargin, colWd float64) {
    19  	var (
    20  		verseFontSize   = float64(data.FontSize)
    21  		titleFontSize   = verseFontSize * 1.2
    22  		titleLineHeight = titleFontSize * 1.1
    23  		verseLineHeight = verseFontSize * 1.1
    24  		pageHt          = data.Page.Height
    25  	)
    26  	for index, song := range data.Songs {
    27  		pdf.SetFont("dejavu", "B", titleFontSize)
    28  		titleHeightNeeded := getCellHeightNeeded(pdf, song.Title, colWd, titleLineHeight) + float64(data.FontSize)
    29  		if len(song.Verses) > 0 && (pdf.GetY()+(titleHeightNeeded+getCellHeightNeeded(pdf, song.Verses[0].Lyrics, colWd, verseLineHeight))) > (pageHt-bottomMargin) {
    30  			pdf.Ln(pageHt - pdf.GetY())
    31  		}
    32  		pdf.MultiCell(colWd, titleLineHeight, fmt.Sprintf("%d - %s", index+1, song.Title), "", "", false)
    33  		pdf.Ln(-1)
    34  		pdf.SetFont("dejavu", "", verseFontSize)
    35  		for _, verse := range song.Verses {
    36  			verseHeight := getCellHeightNeeded(pdf, verse.Lyrics, colWd, verseLineHeight)
    37  			if pdf.GetY()+verseHeight > (pageHt - bottomMargin) {
    38  				pdf.Ln(pageHt - pdf.GetY())
    39  			}
    40  			pdf.MultiCell(colWd, verseLineHeight, verse.Lyrics, "", "", false)
    41  			pdf.Ln(-1)
    42  		}
    43  	}
    44  }
    45  
    46  func getCellHeightNeeded(pdf *gofpdf.Fpdf, text string, lineWidth, lineHeight float64) float64 {
    47  	lines := len(pdf.SplitText(text, lineWidth))
    48  	return float64(lines) * lineHeight
    49  }
    50  
    51  func initFPDF(init *gofpdf.InitType) (pdf *gofpdf.Fpdf) {
    52  	pdf = gofpdf.NewCustom(init)
    53  	fontsToLoad := []Font{
    54  		{Font: "dejavu", Style: "", FilePath: "fonts/DejaVuSansCondensed.ttf"},
    55  		{Font: "dejavu", Style: "B", FilePath: "fonts/DejaVuSansCondensed-Bold.ttf"},
    56  		{Font: "dejavu", Style: "I", FilePath: "fonts/DejaVuSansCondensed-Oblique.ttf"},
    57  		{Font: "dejavu", Style: "BI", FilePath: "fonts/DejaVuSansCondensed-BoldOblique.ttf"},
    58  	}
    59  
    60  	for _, font := range fontsToLoad {
    61  		pdf.AddUTF8Font(font.Font, font.Style, font.FilePath)
    62  		if pdf.Error() != nil {
    63  			fmt.Printf("Font loading failed. Error: %v\n", pdf.Error())
    64  		}
    65  	}
    66  	return
    67  }
    68  
    69  func getImageType(image *Image) error {
    70  	switch imageType := http.DetectContentType(image.Data); imageType {
    71  	case "image/png":
    72  		image.Type = "PNG"
    73  	case "image/jpg":
    74  		image.Type = "JPG"
    75  	case "image/jpeg":
    76  		image.Type = "JPEG"
    77  	case "image/gif":
    78  		image.Type = "GIF"
    79  	default:
    80  		return fmt.Errorf("Not valid image")
    81  	}
    82  	return nil
    83  }
    84  
    85  func decodeBase64ToBytes(data string) (Image, error) {
    86  	decodedData, err := base64.StdEncoding.DecodeString(data[strings.IndexByte(data, ',')+1:])
    87  	return Image{Data: decodedData}, err
    88  }
    89  
    90  func getImageSize(imageData *Image) {
    91  	info, _, err := image.DecodeConfig(bytes.NewReader(imageData.Data))
    92  	if err != nil {
    93  		fmt.Printf("Error. %v", err)
    94  	}
    95  	imageData.Width = info.Width
    96  	imageData.Height = info.Height
    97  }