Skip to content

Instantly share code, notes, and snippets.

@eSlivinski
Created August 11, 2017 14:12
Show Gist options
  • Save eSlivinski/b83b47a4612d7b5094a46f4a04b88b57 to your computer and use it in GitHub Desktop.
Save eSlivinski/b83b47a4612d7b5094a46f4a04b88b57 to your computer and use it in GitHub Desktop.
Page size to screen size conversion
const dpi = 72; // dots per inch (ie. resolution)
const factor = 2.54; // convert inches to cm
const dpcm = dpi / factor; // dots per cm
/**
* Calculate the phantom browser's paper size property based on the
* desired size of the export document
* @param {Number} [widthIn] Width in inches of phantom export doc
* @param {Number} [heightIn] Height in inches of phantom export doc
* @param {String} [orientation] Paper orientation of phantom export doc
* @return {Object} Paper size property for phantom browser
*/
exports.getPaperSize = (widthIn, heightIn, orientation) => {
orientation = orientation || 'portrait';
widthIn = widthIn || orientation === 'portrait' ? 8.5 : 11;
heightIn = heightIn || orientation === 'portrait' ? 11 : 8.5;
let widthCm = widthIn * factor,
heightCm = heightIn * factor;
return {
width: Math.round(widthCm * dpcm) + 'px',
height: Math.round(heightCm * dpcm) + 'px',
orientation: 'portrait',
margin: '1cm'
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment