Skip to content

Commit

Permalink
another fix for showHeaderOnEveryPage (#264)
Browse files Browse the repository at this point in the history
* Update PDFCalculations.swift
account for space.header and space.footer in page margins calculations
* Update PDFTableObject.swift
- if table has a header that repeats on everypage, make sure the cells are shifted by headerHeight and adjust the position of the size wrt minOffset. also add table padding to the surrounding cells
- fixed the spacing between the first row and the table header on rest of the pages (after firstpage)
* Update PDFTableObjectSpec.swift
Co-authored-by: Philip Niedertscheider <dev@philpri.me>
  • Loading branch information
Sgkhour authored and Philip Niedertscheider committed Aug 26, 2021
1 parent dee3c04 commit f05d99c
Show file tree
Hide file tree
Showing 8 changed files with 400 additions and 276 deletions.
5 changes: 4 additions & 1 deletion Documentation/Usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ All values are in dots and are rendered using 72 DPI (dots per inch), as this is

You can also used the predefined formats. For details please refer to the source file [PDFPageFormat.swift](https://github.com/techprimate/TPPDF/blob/master/Source/PDFPageFormat.swift)

Keep in mind that the `space.header` is only applied, if there is at least one element in a header container.
The same applies to the `space.footer` for footer containers and elements.

If you need your page in landscape format, use the `landscapeSize` variable.

![Layout](../resources/layout.png)
Expand Down Expand Up @@ -820,4 +823,4 @@ If you want to enable a debug overlay, set the flag `debug` of the `PDFGenerator
let document: PDFDocument
let generator = PDFDocumentGenerator(document: document)
generator.debug = true
```
```
20 changes: 0 additions & 20 deletions Source/Internal/Graphics/PDFContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,18 @@ public class PDFContext {

internal func beginPDFPage(_ pageInfo: CFDictionary?) {
// Do not create page immediately, instead invoke it as soon as necessary
print("beginPDFPage")
delayedCommands.append(.beginPDFPage(pageConfig: pageInfo))
currentPageContainsDrawnContent = false
hasActivePage = true
}

internal func endPDFPage() {
print("endPDFPage")
applyDelayedCommands()
cgContext.endPDFPage()
hasActivePage = false
}

internal func closePDF() {
print("closePDFPage")
applyDelayedCommands()
cgContext.closePDF()
hasActivePage = false
Expand All @@ -61,33 +58,28 @@ public class PDFContext {
// MARK: - Translation

internal func translateBy(x: CGFloat, y: CGFloat) {
print("translateBy")
delayedCommands.append(.translateBy(x: x, y: y))
}

internal func scaleBy(x: CGFloat, y: CGFloat) {
print("scaleBy")
delayedCommands.append(.scaleBy(x: x, y: y))
}

// MARK: - Drawing

internal func drawPath(using mode: CGPathDrawingMode) {
print("drawPath")
applyDelayedCommands()
cgContext.drawPath(using: mode)
currentPageContainsDrawnContent = true
}

internal func drawPDFPage(_ page: CGPDFPage) {
print("drawPDFPage")
applyDelayedCommands()
cgContext.drawPDFPage(page)
currentPageContainsDrawnContent = true
}

internal func draw(image: CGImage, in frame: CGRect, flipped: Bool) {
print("draw(image:)")
applyDelayedCommands()
cgContext.draw(image: image, in: frame, flipped: flipped)
currentPageContainsDrawnContent = true
Expand All @@ -96,7 +88,6 @@ public class PDFContext {
// MARK: - Colors

internal func setFillColor(_ color: CGColor) {
print("setFillColor")
applyDelayedCommands()
cgContext.setFillColor(color)
currentPageContainsDrawnContent = true
Expand All @@ -105,42 +96,36 @@ public class PDFContext {
// MARK: - Paths

internal func beginPath() {
print("beginPath")
applyDelayedCommands()
cgContext.beginPath()
currentPageContainsDrawnContent = true
}

internal func addPath(_ path: CGPath) {
print("addPath")
applyDelayedCommands()
cgContext.addPath(path)
currentPageContainsDrawnContent = true
}

internal func setLineDash(phase: CGFloat, lengths: [CGFloat]) {
print("setLineDash")
applyDelayedCommands()
cgContext.setLineDash(phase: phase, lengths: lengths)
currentPageContainsDrawnContent = true
}

internal func setLineCap(_ cap: CGLineCap) {
print("setLineCap")
applyDelayedCommands()
cgContext.setLineCap(cap)
currentPageContainsDrawnContent = true
}

internal func setLineWidth(_ width: CGFloat) {
print("setLineWidth")
applyDelayedCommands()
cgContext.setLineWidth(width)
currentPageContainsDrawnContent = true
}

internal func setStrokeColor(_ color: CGColor) {
print("setStrokeColor")
applyDelayedCommands()
cgContext.setStrokeColor(color)
currentPageContainsDrawnContent = true
Expand All @@ -149,13 +134,11 @@ public class PDFContext {
// MARK: - State

internal func saveGState() {
print("saveGState")
applyDelayedCommands()
cgContext.saveGState()
}

internal func restoreGState() {
print("restoreGState")
applyDelayedCommands()
cgContext.restoreGState()
}
Expand All @@ -172,7 +155,6 @@ public class PDFContext {
}

internal func draw(ctFrame frameRef: CTFrame) {
print("draw(ctFrame:)")
applyDelayedCommands()
CTFrameDraw(frameRef, cgContext)
currentPageContainsDrawnContent = true
Expand All @@ -181,7 +163,6 @@ public class PDFContext {
// MARK: - Masking

internal func clip() {
print("clip")
applyDelayedCommands()
cgContext.clip()
currentPageContainsDrawnContent = true
Expand All @@ -190,7 +171,6 @@ public class PDFContext {
// MARK: - Metadata

internal func setURL(_ url: CFURL, for rect: CGRect) {
print("setURL")
applyDelayedCommands()
cgContext.setURL(url, for: rect)
currentPageContainsDrawnContent = true
Expand Down
39 changes: 33 additions & 6 deletions Source/Internal/Table/PDFTableObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -284,19 +284,25 @@ internal class PDFTableObject: PDFRenderObject {
let startPosition: CGPoint = cells.first?.frames.cell.origin ?? .zero
var nextPageCells: [PDFTableCalculatedCell] = cells
var pageEnd = CGPoint.null
var headerShift = table.showHeadersOnEveryPage


repeat {
var pageStart = CGPoint.null

// Calculate top page inset
var minOffset = PDFCalculations.calculateTopMinimum(for: generator)
// Calculate bottom page maximum limit
let maxOffset = PDFCalculations.calculateBottomMaximum(for: generator)

if !firstPage, let headerCells = headerCells {
for item in headerCells {
var cellFrame = item.frames.cell
var contentFrame = item.frames.content
cellFrame.origin.y -= startPosition.y - minOffset
cellFrame.origin.y += table.margin
contentFrame.origin.y -= startPosition.y - minOffset
contentFrame.origin.y += table.margin

pageStart = pageStart == .null ? cellFrame.origin : pageStart
pageEnd = CGPoint(x: cellFrame.maxX, y: cellFrame.maxY) + CGPoint(x: table.margin, y: table.margin)
Expand Down Expand Up @@ -329,11 +335,18 @@ internal class PDFTableObject: PDFRenderObject {
}
minOffset += headerHeight
}
if !firstPage {
// shift the rest of the cells down by headerHeight
if headerShift {
nextPageCells = shiftCellsBy(cells: nextPageCells, shiftValue: headerHeight)
headerShift = false
}
//add table padding around cells
nextPageCells = shiftCellsBy(cells: nextPageCells, shiftValue: table.margin)
}

let filterResult = filterCellsOnPage(for: generator,
items: nextPageCells,
minOffset: minOffset,
maxOffset: maxOffset,
let filterResult = filterCellsOnPage(for: generator, items: nextPageCells,
minOffset: minOffset, maxOffset: maxOffset,
shouldSplitCellsOnPageBreak: table.shouldSplitCellsOnPageBreak)
let onPageCells = filterResult.cells
nextPageCells = filterResult.remainder
Expand Down Expand Up @@ -432,7 +445,6 @@ internal class PDFTableObject: PDFRenderObject {
if shouldSplitCellsOnPageBreak && cellFrame.minY < maxOffset {
result.cells.append(item)
}
// In any case, if the cell does not fit on the active page entirely, it must be repositioned for further pages
var nextPageCell = item
if shouldSplitCellsOnPageBreak {
nextPageCell.frames.cell.origin.y -= contentHeight
Expand All @@ -450,7 +462,22 @@ internal class PDFTableObject: PDFRenderObject {
}
return result
}


internal typealias ShiftedCells = [PDFTableCalculatedCell]

internal func shiftCellsBy(cells: [PDFTableCalculatedCell], shiftValue: CGFloat) -> ShiftedCells {
var shiftedCells: [PDFTableCalculatedCell] = []

for cell in cells {
var shiftedCell = cell

shiftedCell.frames.cell.origin.y += shiftValue
shiftedCell.frames.content.origin.y += shiftValue
shiftedCells.append(shiftedCell)
}
return shiftedCells
}

internal func createSliceObject(frame: CGRect, elements: [PDFRenderObject], minOffset: CGFloat, maxOffset: CGFloat) -> PDFSlicedObject {
let sliceObject = PDFSlicedObject(children: elements, frame: frame)
if frame.maxY > maxOffset {
Expand Down
Loading

0 comments on commit f05d99c

Please sign in to comment.