Skip to content

Instantly share code, notes, and snippets.

@benib
Last active October 30, 2019 13:53
Show Gist options
  • Save benib/91bb7ccdc04a8d706dbe4848497da99d to your computer and use it in GitHub Desktop.
Save benib/91bb7ccdc04a8d706dbe4848497da99d to your computer and use it in GitHub Desktop.
// The standard chrome points per pixel value
const chromePPI = 96;
function mmToInch(mm) {
return mm / 25.4;
}
// this is NZZ print specific
const columnGap = 5;
const columnWidth = 54.2;
function colsToMm(cols) {
// explanation of the math here:
// 1 column is 54.2mm wide
// the gap between the columns is 5mm, so for 2 columns, we need to add 1*5 mm for the column gap.
// For 2.5 columns we need to add 2*5mm for the gap (therefore the Math.ceil)
// the whole thing gets rounded to 2 positions after the . in the end.
let mm = Math.round((cols * columnWidth + (Math.ceil(cols) - 1) * columnGap) * 100) / 100;
// if we have some 1/2 columns, we need to remove 1/2 column gap
// because these graphics will always be combined with another one with 1/2 column next to it, and there should be some gap between
if (Math.ceil(cols) - cols === 0.5) {
mm = mm - columnGap / 2;
}
if (cols >= 6) {
return mm + 24; // Add 24mm if the graphic spans across pages (more than 5 columns)
}
return mm;
}
const dpi = 1200;
// dpr is the devicePixelRatio we tell the headless chrome to use
// it is the factor from CSS pixels to physical pixels (physical in this case: what ends up in the PNG)
// we need this to end up with correct font sizes
const dpr = dpi / chromePPI;
const screenshotParameters = {
dpr,
//the screenshot width (a CSS pixel value) is the width in inch * target dpi / dpr
width: Math.round((mmToInch(colsToMm(numberOfColumns)) * dpi) / dpr))
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment