Skip to content

Instantly share code, notes, and snippets.

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