api/page: printToPDF: provide margins option via object and fix
This commit is contained in:
parent
7f22aa55b0
commit
2d1c049a6d
42
gluon.d.ts
vendored
42
gluon.d.ts
vendored
@ -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').
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user