@@ -32,8 +32,9 @@ function PDFGenerator.new()
3232 root = nil ,
3333 page_width = 595 ,
3434 page_height = 842 ,
35+ header_height = 50 ,
3536 margin_x = {50 , 50 },
36- margin_y = {50 , 50 },
37+ margin_y = {50 , 80 },
3738 current_x = 0 ,
3839 current_y = 0 ,
3940 resources = {},
@@ -51,7 +52,10 @@ function PDFGenerator.new()
5152 }
5253
5354 self .header = function (pageId ) end
54- self .footer = function (pageId ) self :addParagraph (" Page %s of %s" % { pageId , # self .page_list }, { fontSize = 8 , alignment = " right" ,newPage = false }) end
55+ self .footer = function (pageId )
56+ self .moveY (self , 5 )
57+ self :addParagraph (" Page %s of %s" % { pageId , # self .page_list }, { fontSize = 8 , alignment = " right" ,newPage = false })
58+ end
5559
5660 -- Initialize document
5761 self .info = getNewObjNum ()
@@ -369,7 +373,7 @@ function PDFGenerator:addText(text, fontSize, color, alignment, width)
369373 numberToString (color [3 ]),
370374 numberToString (word_spacing ), -- Set word spacing using Tw operator
371375 numberToString (x_pos ),
372- numberToString (self .page_height - self . margin_y [ 1 ] - self . current_y ),
376+ numberToString (self .currentYPos ( self ) ),
373377 EscapeHtml (text )
374378 )
375379 return self
@@ -385,7 +389,7 @@ function PDFGenerator:addText(text, fontSize, color, alignment, width)
385389 numberToString (color [2 ]),
386390 numberToString (color [3 ]),
387391 numberToString (x_pos ),
388- numberToString (self .page_height - self . margin_y [ 1 ] - self . current_y ),
392+ numberToString (self .currentYPos ( self ) ),
389393 EscapeHtml (text )
390394 )
391395
@@ -447,7 +451,7 @@ function PDFGenerator:drawRowTable(columns, row_options)
447451
448452 self :calculateMaxHeight (columns )
449453
450- if row_options .newPage == true and self .page_height - self .current_y - self .current_table .current_row .height < self .margin_y [1 ] + self .margin_y [2 ] then
454+ if row_options .newPage == true and self .page_height - self .current_y - self .current_table .current_row .height - self . header_height < self .margin_y [1 ] + self .margin_y [2 ] then
451455 self :addPage ()
452456 end
453457
@@ -538,13 +542,15 @@ end
538542-- Move current X position
539543function PDFGenerator :moveX (x )
540544 self .current_x = self .current_x + x
541- return self
542545end
543546
544547-- Move current Y position
545548function PDFGenerator :moveY (y )
546549 self .current_y = self .current_y + y
547- return self
550+ end
551+
552+ function PDFGenerator :currentYPos ()
553+ return self .page_height - self .margin_y [1 ] - self .current_y - self .header_height
548554end
549555
550556-- Add paragraph to current page
@@ -560,7 +566,7 @@ function PDFGenerator:addParagraph(text, options)
560566 local lines = self :splitTextToLines (text , options .fontSize , options .width )
561567 for i , line in ipairs (lines ) do
562568 self .current_y = self .current_y + options .fontSize * 1.2
563- if options .new_page == true and self .page_height - self .current_y < self .margin_y [1 ] + self .margin_y [2 ] then
569+ if options .new_page == true and self .page_height - self .current_y - self . header_height < self .margin_y [1 ] + self .margin_y [2 ] then
564570 self :addPage ()
565571 end
566572 self :addText (line , options .fontSize , options .color , options .alignment , options .width )
@@ -732,7 +738,7 @@ function PDFGenerator:drawRectangle(width, height, borderWidth, borderStyle, bor
732738 content .stream = content .stream .. string.format (
733739 " %s %s %s %s re\n B\n " ,
734740 numberToString (self .margin_x [1 ] + self .current_x ),
735- numberToString (self .page_height - self . margin_y [ 1 ] - self . current_y - height ),
741+ numberToString (self .currentYPos ( self ) - height ),
736742 numberToString (width ),
737743 numberToString (height )
738744 )
@@ -896,25 +902,21 @@ end
896902function PDFGenerator :drawFooter (pageId )
897903 -- Reset position to top of page
898904 self .current_x = 0
899- self .current_y = self .page_height - self .margin_y [2 ] - self .margin_y [1 ]
900-
905+ self .current_y = self .page_height - self .margin_y [2 ] - self .margin_y [1 ] - self .header_height
901906 -- Execute footer function
902907 self .footer (pageId )
903908end
904909
905910-- Generate PDF and return as string
906911function PDFGenerator :generate ()
907912 local output = {}
908- local current_page = 1
909913 -- Add header and footer to all pages
910- for pageId , content in pairs (self .contents ) do
914+ for current_page , pageId in pairs (self .page_list ) do
911915 -- Set current page for header/footer drawing
912916 self .current_page_obj = pageId
913917
914918 self :drawHeader (current_page )
915919 self :drawFooter (current_page )
916-
917- current_page = current_page + 1
918920 end
919921 -- Add header
920922 table.insert (output , " %PDF-1.7\n %âãÏÓ\n " )
0 commit comments