From bac3bb8302ca7cecc94d78c0cbc2d9a167607d4d Mon Sep 17 00:00:00 2001 From: Jepzter Date: Wed, 18 Dec 2019 21:29:00 +0100 Subject: [PATCH 1/3] Add possibility to set custom page margins * Added a new method on the Maroto PDF interface to allow backwards compatibility instead of adding extra parameter to NewMaroto --- pkg/pdf/example_test.go | 6 ++++++ pkg/pdf/pdf.go | 12 ++++++++++++ pkg/pdf/pdf_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) diff --git a/pkg/pdf/example_test.go b/pkg/pdf/example_test.go index 26a34079..4c8778f2 100644 --- a/pkg/pdf/example_test.go +++ b/pkg/pdf/example_test.go @@ -359,3 +359,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 3b0bece2..b243e42e 100644 --- a/pkg/pdf/pdf.go +++ b/pkg/pdf/pdf.go @@ -26,6 +26,8 @@ type Maroto interface { GetBorder() bool GetPageSize() (float64, float64) GetCurrentPage() int + SetPageMargins(left, top, right float64) + GetPageMargins() (float64, float64, float64, float64) // Outside Col/Row Components TableList(header []string, contents [][]string, prop ...props.TableList) @@ -132,6 +134,16 @@ func (s *PdfMaroto) GetCurrentPage() int { return s.pageIndex } +// 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) +} + +func (s *PdfMaroto) GetPageMargins() (float64, float64, float64, 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 3e87d895..8ed7a11c 100644 --- a/pkg/pdf/pdf_test.go +++ b/pkg/pdf/pdf_test.go @@ -1516,6 +1516,13 @@ func newMarotoTest(fpdf *mocks.Pdf, math *mocks.Math, font *mocks.Font, text *mo return m } +func basePdfHelperTest(left, top, right float64) *mocks.Pdf { + pdf := &mocks.Pdf{} + pdf.On("SetMargins", mock.AnythingOfType("float64"), mock.AnythingOfType("float64"), mock.AnythingOfType("float64")) + pdf.On("GetMargins").Return(left, top, right, 0.0) + return pdf +} + func basePdfTest() *mocks.Pdf { pdf := &mocks.Pdf{} pdf.On("GetPageSize").Return(100.0, 100.0) @@ -1723,3 +1730,37 @@ func TestPdfMaroto_GetCurrentPage(t *testing.T) { c.assert(t, pageIndex) } } + +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 := basePdfHelperTest(12.3, 19.3, 0) + + m := newMarotoTest(pdf, nil, nil, nil, nil, nil, nil) + + // Act + c.act(m) + + // Assert + left, top, right, _ := m.GetPageMargins() + c.assert(t, left, top, right) + } +} From 08dfd11f1cbe6b4b20a901ec87e1898ce0b7f03e Mon Sep 17 00:00:00 2001 From: Jepzter Date: Fri, 20 Dec 2019 23:17:13 +0100 Subject: [PATCH 2/3] ISSUE-123, add comment to GetMargins and move mock into basePdfTest --- pkg/pdf/pdf.go | 4 +++- pkg/pdf/pdf_test.go | 58 ++++++++++++++++++++------------------------- 2 files changed, 29 insertions(+), 33 deletions(-) diff --git a/pkg/pdf/pdf.go b/pkg/pdf/pdf.go index db01b248..b8fadc3a 100644 --- a/pkg/pdf/pdf.go +++ b/pkg/pdf/pdf.go @@ -152,7 +152,9 @@ func (s *PdfMaroto) SetPageMargins(left, top, right float64) { s.Pdf.SetMargins(left, top, right) } -func (s *PdfMaroto) GetPageMargins() (float64, float64, float64, float64) { +// 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() } diff --git a/pkg/pdf/pdf_test.go b/pkg/pdf/pdf_test.go index da9bb9a9..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,20 +1531,14 @@ func newMarotoTest(fpdf *mocks.Pdf, math *mocks.Math, font *mocks.Font, text *mo return m } -func basePdfHelperTest(left, top, right float64) *mocks.Pdf { - pdf := &mocks.Pdf{} - pdf.On("SetMargins", mock.AnythingOfType("float64"), mock.AnythingOfType("float64"), mock.AnythingOfType("float64")) - pdf.On("GetMargins").Return(left, top, right, 0.0) - return pdf -} - -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 } @@ -1646,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() @@ -1730,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() @@ -1798,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() @@ -1817,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() @@ -1834,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() @@ -1876,9 +1870,9 @@ func TestPdfMaroto_SetPageMargins(t *testing.T) { for _, c := range cases { // Arrange - pdf := basePdfHelperTest(12.3, 19.3, 0) + pdf := basePdfTest(12.3, 19.3, 0) - m := newMarotoTest(pdf, nil, nil, nil, nil, nil, nil) + m := newMarotoTest(pdf, nil, nil, nil, nil, nil, nil, nil) // Act c.act(m) From 6f6dcbc281e251f262e0b3aefb29a1b73ce67481 Mon Sep 17 00:00:00 2001 From: jfercher Date: Fri, 20 Dec 2019 21:23:26 -0300 Subject: [PATCH 3/3] Fix signature for margins not default --- internal/signature.go | 4 ++-- internal/signature_test.go | 26 +++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) 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) +}