diff --git a/gluon.d.ts b/gluon.d.ts index d1c9eea..aae44ce 100644 --- a/gluon.d.ts +++ b/gluon.d.ts @@ -91,28 +91,34 @@ type PrintToPDFOptions = { paperHeight?: number, /** - * Top margin in inches. - * @default 0.4 + * Set the margins of the PDF (inches). + * @default 0.4 for all */ - marginTop?: number, + margins?: { + /** + * Top margin in inches. + * @default 0.4 (1cm) + */ + top?: number, - /** - * Bottom margin in inches. - * @default 0.4 - */ - marginBottom?: number, + /** + * Bottom margin in inches. + * @default 0.4 (1cm) + */ + bottom?: number, - /** - * Left margin in inches. - * @default 0.4 - */ - marginLeft?: number, + /** + * Left margin in inches. + * @default 0.4 (1cm) + */ + left?: number, - /** - * Right margin in inches. - * @default 0.4 - */ - marginRight?: number, + /** + * Right margin in inches. + * @default 0.4 (1cm) + */ + right?: number + }, /** * Paper ranges to print, one based, (eg '1-5, 8, 11-13'). diff --git a/src/launcher/inject.js b/src/launcher/inject.js index 6b431fb..5fc546d 100644 --- a/src/launcher/inject.js +++ b/src/launcher/inject.js @@ -118,19 +118,29 @@ export default async (CDP, proc, injectionType = 'browser', { dataPath, browserN }, printToPDF: async (...args) => { - let path, options; + let path, options = {}; if (args.length === 1) { if (typeof args[0] === 'string') path = args[0]; - else options = args[0]; + else options = { ...args[0] }; } if (args.length === 2) { - [ path, options ] = args; + path = args[0]; + options = { ...args[1] }; } + if (options.margins) { + const { top, bottom, left, right } = options.margins; + if (top) options.marginTop = top; + if (bottom) options.marginBottom = bottom; + if (left) options.marginLeft = left; + if (right) options.marginRight = right; - const { data } = await CDP.send('Page.printToPDF', options); + delete options.margins; + } + + const { data } = await Window.cdp.send('Page.printToPDF', options); const buffer = Buffer.from(data, 'base64'); if (path) await writeFile(path, buffer);