Skip to content

Instantly share code, notes, and snippets.

@harsha509
Created August 3, 2020 07:04
Show Gist options
  • Save harsha509/bf07f28bcf4ab93d453fd65e3c1773a3 to your computer and use it in GitHub Desktop.
Save harsha509/bf07f28bcf4ab93d453fd65e3c1773a3 to your computer and use it in GitHub Desktop.
printToPdf({orientation, scale, background, width, height, top, bottom, left, right, shrinkToFit, pageRanges}) {
let page = {};
let margin = {};
let keys = arguments[0];
let params= {};
Object.keys(keys).forEach(function(key){
switch (key) {
case 'orientation':
if(orientation && orientation !== 'landscape' && orientation !== 'portrait') {
throw new error.InvalidArgumentError(
`Invalid argument '${orientation}'. It should be either "landscape" (or) "portrait"`);
} else {
params.orientation = keys[key];
}
break;
case 'scale':
if(scale >= 0.1 && scale <=2 && typeof scale === 'number' && scale) {
params.scale = keys[key];
} else {
throw new error.InvalidArgumentError(
`Invalid argument '${scale}'. It scale should be in between > 0.1 and < 2`);
}
break;
case 'background':
if(background && background!==true && background !== false) {
throw new error.InvalidArgumentError(
`Invalid argument '${background}'. It should be either "true" (or) "false"`);
} else {
params.background = keys[key];
}
break;
case 'width':
if(width && typeof width === 'number' && width >= 0) {
page.width = keys[key];
params.page = page;
} else {
throw new error.InvalidArgumentError(
`Invalid argument '${width}'. It width should be of type 'integer' and >= 0`);
}
break;
case 'height':
if(height && typeof height === 'number' && height >= 0) {
page.height = keys[key];
params.page = page;
} else {
throw new error.InvalidArgumentError(
`Invalid argument '${height}'. It width should be of type 'integer' and >= 0`);
}
break;
case 'top':
if(top && typeof top === 'number' && top >= 0) {
margin.top = keys[key];
params.margin = margin;
} else {
throw new error.InvalidArgumentError(
`Invalid argument '${top}'. It width should be of type 'integer' and >= 0`);
}
break;
case 'left':
if(left && typeof left === 'number' && left >= 0) {
margin.left = keys[key];
params.margin = margin;
} else {
throw new error.InvalidArgumentError(
`Invalid argument '${left}'. It width should be of type 'integer' and >= 0`);
}
break;
case 'bottom':
if(bottom && typeof bottom === 'number' && bottom >= 0) {
margin.bottom = keys[key];
params.margin = margin;
} else {
throw new error.InvalidArgumentError(
`Invalid argument '${bottom}'. It width should be of type 'integer' and >= 0`);
}
break;
case 'right':
if(right && typeof right === 'number' && right >= 0) {
margin.right = keys[key];
params.margin = margin;
} else {
throw new error.InvalidArgumentError(
`Invalid argument '${right}'. It width should be of type 'integer' and >= 0`);
}
break;
case 'shrinkToFit':
if(shrinkToFit && shrinkToFit !== true && shrinkToFit !== false) {
throw new error.InvalidArgumentError(
`Invalid argument '${shrinkToFit}'. It should be either "true" (or) "false"`);
} else {
params.shrinkToFit = keys[key];
}
break;
case 'pageRanges':
if(pageRanges && pageRanges instanceof Array || typeof pageRanges === 'string') {
params.pageRanges = keys[key];
} else {
throw new error.InvalidArgumentError(
`Invalid argument '${shrinkToFit}'. It should be either array of page numbers (or) String notation `);
}
break;
}
});
console.log(params);
return this.execute(
new command.Command(command.Name.PRINT_PAGE).setParameters(params));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment