diff --git a/internal/signature.go b/internal/signature.go index c9755c92..7f35417a 100644 --- a/internal/signature.go +++ b/internal/signature.go @@ -28,9 +28,9 @@ func NewSignature(pdf gofpdf.Pdf, math Math, text Text) *signature { // AddSpaceFor create a space for a signature inside a cell func (s *signature) AddSpaceFor(label string, textProp props.Text, qtdCols float64, marginTop float64, actualCol float64) { widthPerCol := s.math.GetWidthPerCol(qtdCols) - left, _, right, _ := s.pdf.GetMargins() + left, _, _, _ := s.pdf.GetMargins() space := 4.0 - s.pdf.Line((widthPerCol*actualCol)+left+space, marginTop+5.0, widthPerCol*(actualCol+1)+right-space, marginTop+5.0) + s.pdf.Line((widthPerCol*actualCol)+left+space, marginTop+5.0, widthPerCol*(actualCol+1)+left-space, marginTop+5.0) s.text.Add(label, textProp, marginTop, actualCol, qtdCols) } diff --git a/internal/signature_test.go b/internal/signature_test.go index b935a896..30017b2c 100644 --- a/internal/signature_test.go +++ b/internal/signature_test.go @@ -17,7 +17,7 @@ func TestNewSignature(t *testing.T) { assert.Equal(t, fmt.Sprintf("%T", signature), "*internal.signature") } -func TestSignature_AddSpaceFor(t *testing.T) { +func TestSignature_AddSpaceFor_DefaultMargins(t *testing.T) { // Arrange pdf := &mocks.Pdf{} pdf.On("GetMargins").Return(10.0, 10.0, 10.0, 10.0) @@ -40,3 +40,27 @@ func TestSignature_AddSpaceFor(t *testing.T) { text.AssertNumberOfCalls(t, "Add", 1) text.AssertCalled(t, "Add", "label", props.Text{Size: 10.0}, 5.0, 2.0, 5.0) } + +func TestSignature_AddSpaceFor_NotDefaultMargins(t *testing.T) { + // Arrange + pdf := &mocks.Pdf{} + pdf.On("GetMargins").Return(20.0, 10.0, 10.0, 10.0) + pdf.On("Line", mock.Anything, mock.Anything, mock.Anything, mock.Anything) + + math := &mocks.Math{} + math.On("GetWidthPerCol", mock.Anything).Return(50.0) + + text := &mocks.Text{} + text.On("Add", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything) + + signature := internal.NewSignature(pdf, math, text) + + // Act + signature.AddSpaceFor("label", props.Text{Size: 10.0}, 5, 5, 2) + + // Assert + pdf.AssertNumberOfCalls(t, "Line", 1) + pdf.AssertCalled(t, "Line", 124.0, 10.0, 166.0, 10.0) + text.AssertNumberOfCalls(t, "Add", 1) + text.AssertCalled(t, "Add", "label", props.Text{Size: 10.0}, 5.0, 2.0, 5.0) +} diff --git a/pkg/pdf/example_test.go b/pkg/pdf/example_test.go index 18ae6ccf..372fdb04 100644 --- a/pkg/pdf/example_test.go +++ b/pkg/pdf/example_test.go @@ -361,3 +361,9 @@ func ExamplePdfMaroto_RegisterHeader() { // Do more things or not and save... } + +// ExamplePdfMaroto_SetPageMargins demonstrates how to set custom page margins. +func ExamplePdfMaroto_SetPageMargins() { + m := pdf.NewMaroto(consts.Portrait, consts.A4) + m.SetPageMargins(10, 60, 10) +} diff --git a/pkg/pdf/pdf.go b/pkg/pdf/pdf.go index 71c6b95b..b8fadc3a 100644 --- a/pkg/pdf/pdf.go +++ b/pkg/pdf/pdf.go @@ -27,6 +27,8 @@ type Maroto interface { GetPageSize() (float64, float64) GetCurrentPage() int GetCurrentOffset() float64 + SetPageMargins(left, top, right float64) + GetPageMargins() (float64, float64, float64, float64) // Outside Col/Row Components TableList(header []string, contents [][]string, prop ...props.TableList) @@ -144,6 +146,18 @@ func (s *PdfMaroto) GetCurrentOffset() float64 { return s.offsetY } +// SetPageMargins overrides default margins (10,10,10) +// the new page margin will affect all PDF pages +func (s *PdfMaroto) SetPageMargins(left, top, right float64) { + s.Pdf.SetMargins(left, top, right) +} + +// GetPageMargins returns the set page margins. Comes in order of Left, Top, Right, Bottom +// Default page margins is left: 10, top: 10, right: 10 +func (s *PdfMaroto) GetPageMargins() (left float64, top float64, right float64, bottom float64) { + return s.Pdf.GetMargins() +} + // Signature add a space for a signature inside a cell, // the space will have a line and a text below func (s *PdfMaroto) Signature(label string, prop ...props.Font) { diff --git a/pkg/pdf/pdf_test.go b/pkg/pdf/pdf_test.go index 806c1cbd..3d12034b 100644 --- a/pkg/pdf/pdf_test.go +++ b/pkg/pdf/pdf_test.go @@ -297,7 +297,7 @@ func TestPdfMaroto_Signature(t *testing.T) { for _, c := range cases { // Arrange signature := c.signature() - pdf := basePdfTest() + pdf := basePdfTest(10, 10, 10) math := baseMathTest() tableList := baseTableList() m := newMarotoTest(pdf, math, nil, nil, signature, nil, nil, tableList) @@ -421,7 +421,7 @@ func TestPdfMaroto_Text(t *testing.T) { for _, c := range cases { // Arrange text := baseTextTest() - pdf := basePdfTest() + pdf := basePdfTest(10, 10, 10) math := baseMathTest() tableList := baseTableList() m := newMarotoTest(pdf, math, nil, text, nil, nil, nil, tableList) @@ -589,7 +589,7 @@ func TestPdfMaroto_FileImage(t *testing.T) { for _, c := range cases { // Arrange - pdf := basePdfTest() + pdf := basePdfTest(10, 10, 10) math := baseMathTest() image := c.image() tableList := baseTableList() @@ -759,7 +759,7 @@ func TestPdfMaroto_Base64Image(t *testing.T) { for _, c := range cases { // Arrange - pdf := basePdfTest() + pdf := basePdfTest(10, 10, 10) math := baseMathTest() image := c.image() tableList := baseTableList() @@ -915,7 +915,7 @@ func TestPdfMaroto_QrCode(t *testing.T) { for _, c := range cases { // Arrange - pdf := basePdfTest() + pdf := basePdfTest(10, 10, 10) math := baseMathTest() code := c.code() tableList := baseTableList() @@ -999,7 +999,7 @@ func TestPdfMaroto_Barcode(t *testing.T) { for _, c := range cases { // Arrange - pdf := basePdfTest() + pdf := basePdfTest(10, 10, 10) math := baseMathTest() code := c.code() tableList := baseTableList() @@ -1116,7 +1116,7 @@ func TestPdfMaroto_Row(t *testing.T) { for _, c := range cases { // Arrange - pdf := basePdfTest() + pdf := basePdfTest(10, 10, 10) math := baseMathTest() tableList := baseTableList() @@ -1142,7 +1142,7 @@ func TestPdfMaroto_Line(t *testing.T) { { "One line", func() *mocks.Pdf { - pdf := basePdfTest() + pdf := basePdfTest(10, 10, 10) pdf.On("Line", mock.Anything, mock.Anything, mock.Anything, mock.Anything) return pdf }, @@ -1160,7 +1160,7 @@ func TestPdfMaroto_Line(t *testing.T) { { "Two lines", func() *mocks.Pdf { - pdf := basePdfTest() + pdf := basePdfTest(10, 10, 10) pdf.On("Line", mock.Anything, mock.Anything, mock.Anything, mock.Anything) return pdf }, @@ -1259,7 +1259,7 @@ func TestPdfMaroto_ColSpace(t *testing.T) { for _, c := range cases { // Arrange - pdf := basePdfTest() + pdf := basePdfTest(10, 10, 10) math := baseMathTest() tableList := baseTableList() @@ -1337,7 +1337,7 @@ func TestPdfMaroto_ColSpaces(t *testing.T) { for _, c := range cases { // Arrange - pdf := basePdfTest() + pdf := basePdfTest(10, 10, 10) math := baseMathTest() tableList := baseTableList() @@ -1364,7 +1364,7 @@ func TestPdfMaroto_Output(t *testing.T) { { "When Output returns an error", func() *mocks.Pdf { - pdf := basePdfTest() + pdf := basePdfTest(10, 10, 10) pdf.On("Output", mock.Anything).Return(errors.New("AnyError")) return pdf }, @@ -1385,7 +1385,7 @@ func TestPdfMaroto_Output(t *testing.T) { { "When Output not returns an error", func() *mocks.Pdf { - pdf := basePdfTest() + pdf := basePdfTest(10, 10, 10) pdf.On("Output", mock.Anything).Return(nil) return pdf }, @@ -1406,7 +1406,7 @@ func TestPdfMaroto_Output(t *testing.T) { { "When Output has footer", func() *mocks.Pdf { - pdf := basePdfTest() + pdf := basePdfTest(10, 10, 10) pdf.On("Output", mock.Anything).Return(nil) return pdf }, @@ -1463,7 +1463,7 @@ func TestPdfMaroto_OutputFileAndClose(t *testing.T) { { "When OutputFileAndClose returns an error", func() *mocks.Pdf { - pdf := basePdfTest() + pdf := basePdfTest(10, 10, 10) pdf.On("OutputFileAndClose", mock.Anything).Return(errors.New("AnyError")) return pdf }, @@ -1481,7 +1481,7 @@ func TestPdfMaroto_OutputFileAndClose(t *testing.T) { { "When OutputFileAndClose not returns an error", func() *mocks.Pdf { - pdf := basePdfTest() + pdf := basePdfTest(10, 10, 10) pdf.On("OutputFileAndClose", mock.Anything).Return(nil) return pdf }, @@ -1531,13 +1531,14 @@ func newMarotoTest(fpdf *mocks.Pdf, math *mocks.Math, font *mocks.Font, text *mo return m } -func basePdfTest() *mocks.Pdf { +func basePdfTest(left, top, right float64) *mocks.Pdf { pdf := &mocks.Pdf{} pdf.On("GetPageSize").Return(100.0, 100.0) - pdf.On("GetMargins").Return(10.0, 10.0, 10.0, 10.0) + pdf.On("GetMargins").Return(left, top, right, 0.0) pdf.On("CellFormat", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything) pdf.On("Ln", mock.Anything) pdf.On("GetFontSize").Return(1.0, 1.0) + pdf.On("SetMargins", mock.AnythingOfType("float64"), mock.AnythingOfType("float64"), mock.AnythingOfType("float64")) return pdf } @@ -1639,7 +1640,7 @@ func TestPdfMaroto_RegisterFooter(t *testing.T) { for _, c := range cases { // Arrange - pdf := basePdfTest() + pdf := basePdfTest(10, 10, 10) math := baseMathTest() text := baseTextTest() font := baseFontTest() @@ -1723,7 +1724,7 @@ func TestPdfMaroto_RegisterHeader(t *testing.T) { for _, c := range cases { // Arrange - pdf := basePdfTest() + pdf := basePdfTest(10, 10, 10) math := baseMathTest() text := baseTextTest() font := baseFontTest() @@ -1791,7 +1792,7 @@ func TestPdfMaroto_GetCurrentPage(t *testing.T) { for _, c := range cases { // Arrange - pdf := basePdfTest() + pdf := basePdfTest(10, 10, 10) math := baseMathTest() text := baseTextTest() font := baseFontTest() @@ -1810,7 +1811,7 @@ func TestPdfMaroto_GetCurrentPage(t *testing.T) { func TestPdfMaroto_GetCurrentPage_WhenCreateOffsetIsZero(t *testing.T) { // Arrange - pdf := basePdfTest() + pdf := basePdfTest(10, 10, 10) math := baseMathTest() text := baseTextTest() font := baseFontTest() @@ -1827,7 +1828,7 @@ func TestPdfMaroto_GetCurrentPage_WhenCreateOffsetIsZero(t *testing.T) { func TestPdfMaroto_GetCurrentPage_WhenIsNotZero(t *testing.T) { // Arrange - pdf := basePdfTest() + pdf := basePdfTest(10, 10, 10) math := baseMathTest() text := baseTextTest() font := baseFontTest() @@ -1847,3 +1848,37 @@ func TestPdfMaroto_GetCurrentPage_WhenIsNotZero(t *testing.T) { // Assert assert.Equal(t, offset, float64(20)) } + +func TestPdfMaroto_SetPageMargins(t *testing.T) { + cases := []struct { + name string + act func(m pdf.Maroto) + assert func(t *testing.T, left, top, right float64) + }{ + { + "Set page margins should override default", + func(m pdf.Maroto) { + m.SetPageMargins(12.3, 19.3, 0.0) + }, + func(t *testing.T, left, top, right float64) { + assert.Equal(t, 12.3, left) + assert.Equal(t, 19.3, top) + assert.Equal(t, 0.0, right) + }, + }, + } + + for _, c := range cases { + // Arrange + pdf := basePdfTest(12.3, 19.3, 0) + + m := newMarotoTest(pdf, nil, nil, nil, nil, nil, nil, nil) + + // Act + c.act(m) + + // Assert + left, top, right, _ := m.GetPageMargins() + c.assert(t, left, top, right) + } +}