Creating pages containing grids
So essentially, you want to layout an array of items in a 2D grid. This problem can be solved by using division and remainder operations to calculate the coordinates.


x is the remainder of dividing the index by maxX
y is the remainder of dividing by maxY the result of dividing the index by maxX.
A new page made is when both x and y are 0


Here's a rough sketch of how it looks:

let i = item index
let maxX = 5
let maxY = 6
let x = i % maxX 
let y = (i / maxX) % maxY 
let p = !(i % (maxX * maxY))

i: 0 1 2 3 4 5 6 7 8 9 ... 29 30 31 32 33 34 35
x: 0 1 2 3 4 0 1 2 3 4 ... 4  0  1  2  3  4  0
y: 0 0 0 0 0 1 1 1 1 1 ... 5  0  0  0  0  0  1
p: 1 0 0 0 0 0 0 0 0 0 ... 0  1  0  0  0  0  0


In code, this would be:

const griditems = []

const pageconstants = {
 grid:{
   x: 5, // 5 across
   y: 6, // 6 down
 }
}

const doc = new PDFDocument({
  layout : 'portrait',
  width: 612,
  height: 792,
  autoFirstPage: false
})

const getX = i =&gt; i % pageconstants.grid.x
const getY = i =&gt; Math.floor((i / pageconstants.grid.x) % pageconstants.grid.y)
const isNewPage = i =&gt; !(i % (pageconstants.grid.x * pageconstants.grid.y))

const positions = griditems.forEach((item, index) =&gt; {
  const x = 100 * getX(index)
  const y = 100 * getY(index)

  if (isNewPage(index)) createNewPage(doc)

  addGridItem(doc, { x, y }, item)
})