Skip to content

Instantly share code, notes, and snippets.

@cjharkins
Last active January 29, 2020 20:12
Show Gist options
  • Save cjharkins/0f37d026e6365c63e714ac3e1d3ead60 to your computer and use it in GitHub Desktop.
Save cjharkins/0f37d026e6365c63e714ac3e1d3ead60 to your computer and use it in GitHub Desktop.
import jsPDF from 'jspdf'
export default function({data, headers, filename}) {
const doc = new jsPDF({...})
{... I cut out previous code for brevity but it belongs here!}
const xPositions = [] // NEW CODE
// NEW CODE
// HEADERS
headers.forEach((heading, index) => {
// If you decide to add xPos to the header mappings this will fire
if (heading.hasOwnProperty('xPos')) {
doc.text(String(heading.label), heading.xPos, pageMargin)
xPositions.push(heading.xPos)
} else {
// get x position for heading
const xPositionForCurrentHeader = pageMargin + index * (liveArea.width/(headers.length))
// need to position our text within our live area
const yPositionForHeaders = pageMargin
doc.text(String(heading.label), index === 0 ? xPositionForCurrentHeader:(xPositionForCurrentHeader + padding), yPositionForHeaders)
// Push x position of current header to xPositions array
xPositions.push(index === 0 ? xPositionForCurrentHeader:(xPositionForCurrentHeader + padding))
}
})
doc.save(filename)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment