chore: fix BiDi to use cm (#9926)
This commit is contained in:
parent
3866e462bc
commit
c4e1675589
@ -2171,7 +2171,10 @@ export class Page extends EventEmitter {
|
|||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
_getPDFOptions(options: PDFOptions = {}): ParsedPDFOptions {
|
_getPDFOptions(
|
||||||
|
options: PDFOptions = {},
|
||||||
|
lengthUnit: 'in' | 'cm' = 'in'
|
||||||
|
): ParsedPDFOptions {
|
||||||
const defaults = {
|
const defaults = {
|
||||||
scale: 1,
|
scale: 1,
|
||||||
displayHeaderFooter: false,
|
displayHeaderFooter: false,
|
||||||
@ -2194,15 +2197,19 @@ export class Page extends EventEmitter {
|
|||||||
width = format.width;
|
width = format.width;
|
||||||
height = format.height;
|
height = format.height;
|
||||||
} else {
|
} else {
|
||||||
width = convertPrintParameterToInches(options.width) ?? width;
|
width = convertPrintParameterToInches(options.width, lengthUnit) ?? width;
|
||||||
height = convertPrintParameterToInches(options.height) ?? height;
|
height =
|
||||||
|
convertPrintParameterToInches(options.height, lengthUnit) ?? height;
|
||||||
}
|
}
|
||||||
|
|
||||||
const margin = {
|
const margin = {
|
||||||
top: convertPrintParameterToInches(options.margin?.top) || 0,
|
top: convertPrintParameterToInches(options.margin?.top, lengthUnit) || 0,
|
||||||
left: convertPrintParameterToInches(options.margin?.left) || 0,
|
left:
|
||||||
bottom: convertPrintParameterToInches(options.margin?.bottom) || 0,
|
convertPrintParameterToInches(options.margin?.left, lengthUnit) || 0,
|
||||||
right: convertPrintParameterToInches(options.margin?.right) || 0,
|
bottom:
|
||||||
|
convertPrintParameterToInches(options.margin?.bottom, lengthUnit) || 0,
|
||||||
|
right:
|
||||||
|
convertPrintParameterToInches(options.margin?.right, lengthUnit) || 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
const output = {
|
const output = {
|
||||||
@ -2698,7 +2705,8 @@ export const unitToPixels = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
function convertPrintParameterToInches(
|
function convertPrintParameterToInches(
|
||||||
parameter?: string | number
|
parameter?: string | number,
|
||||||
|
lengthUnit: 'in' | 'cm' = 'in'
|
||||||
): number | undefined {
|
): number | undefined {
|
||||||
if (typeof parameter === 'undefined') {
|
if (typeof parameter === 'undefined') {
|
||||||
return undefined;
|
return undefined;
|
||||||
@ -2727,5 +2735,5 @@ function convertPrintParameterToInches(
|
|||||||
'page.pdf() Cannot handle parameter type: ' + typeof parameter
|
'page.pdf() Cannot handle parameter type: ' + typeof parameter
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return pixels / 96;
|
return pixels / unitToPixels[lengthUnit];
|
||||||
}
|
}
|
||||||
|
@ -1458,37 +1458,51 @@ export class CDPPage extends Page {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override async createPDFStream(options: PDFOptions = {}): Promise<Readable> {
|
override async createPDFStream(options: PDFOptions = {}): Promise<Readable> {
|
||||||
const params = this._getPDFOptions(options);
|
const {
|
||||||
|
landscape,
|
||||||
|
displayHeaderFooter,
|
||||||
|
headerTemplate,
|
||||||
|
footerTemplate,
|
||||||
|
printBackground,
|
||||||
|
scale,
|
||||||
|
width: paperWidth,
|
||||||
|
height: paperHeight,
|
||||||
|
margin,
|
||||||
|
pageRanges,
|
||||||
|
preferCSSPageSize,
|
||||||
|
omitBackground,
|
||||||
|
timeout,
|
||||||
|
} = this._getPDFOptions(options);
|
||||||
|
|
||||||
if (params.omitBackground) {
|
if (omitBackground) {
|
||||||
await this.#setTransparentBackgroundColor();
|
await this.#setTransparentBackgroundColor();
|
||||||
}
|
}
|
||||||
|
|
||||||
const printCommandPromise = this.#client.send('Page.printToPDF', {
|
const printCommandPromise = this.#client.send('Page.printToPDF', {
|
||||||
transferMode: 'ReturnAsStream',
|
transferMode: 'ReturnAsStream',
|
||||||
landscape: params.landscape,
|
landscape,
|
||||||
displayHeaderFooter: params.displayHeaderFooter,
|
displayHeaderFooter,
|
||||||
headerTemplate: params.headerTemplate,
|
headerTemplate,
|
||||||
footerTemplate: params.footerTemplate,
|
footerTemplate,
|
||||||
printBackground: params.printBackground,
|
printBackground,
|
||||||
scale: params.scale,
|
scale,
|
||||||
paperWidth: params.width,
|
paperWidth,
|
||||||
paperHeight: params.height,
|
paperHeight,
|
||||||
marginTop: params.margin.top,
|
marginTop: margin.top,
|
||||||
marginBottom: params.margin.bottom,
|
marginBottom: margin.bottom,
|
||||||
marginLeft: params.margin.left,
|
marginLeft: margin.left,
|
||||||
marginRight: params.margin.right,
|
marginRight: margin.right,
|
||||||
pageRanges: params.pageRanges,
|
pageRanges,
|
||||||
preferCSSPageSize: params.preferCSSPageSize,
|
preferCSSPageSize,
|
||||||
});
|
});
|
||||||
|
|
||||||
const result = await waitWithTimeout(
|
const result = await waitWithTimeout(
|
||||||
printCommandPromise,
|
printCommandPromise,
|
||||||
'Page.printToPDF',
|
'Page.printToPDF',
|
||||||
params.timeout
|
timeout
|
||||||
);
|
);
|
||||||
|
|
||||||
if (params.omitBackground) {
|
if (omitBackground) {
|
||||||
await this.#resetDefaultBackgroundColor();
|
await this.#resetDefaultBackgroundColor();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -206,23 +206,33 @@ export class Page extends PageBase {
|
|||||||
|
|
||||||
override async pdf(options: PDFOptions = {}): Promise<Buffer> {
|
override async pdf(options: PDFOptions = {}): Promise<Buffer> {
|
||||||
const {path = undefined} = options;
|
const {path = undefined} = options;
|
||||||
const params = this._getPDFOptions(options);
|
const {
|
||||||
|
printBackground: background,
|
||||||
|
margin,
|
||||||
|
landscape,
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
pageRanges,
|
||||||
|
scale,
|
||||||
|
preferCSSPageSize,
|
||||||
|
timeout,
|
||||||
|
} = this._getPDFOptions(options, 'cm');
|
||||||
const {result} = await waitWithTimeout(
|
const {result} = await waitWithTimeout(
|
||||||
this.#context.connection.send('browsingContext.print', {
|
this.#context.connection.send('browsingContext.print', {
|
||||||
context: this.#context._contextId,
|
context: this.#context._contextId,
|
||||||
background: params.printBackground,
|
background,
|
||||||
margin: params.margin,
|
margin,
|
||||||
orientation: params.landscape ? 'landscape' : 'portrait',
|
orientation: landscape ? 'landscape' : 'portrait',
|
||||||
page: {
|
page: {
|
||||||
width: params.width,
|
width,
|
||||||
height: params.height,
|
height,
|
||||||
},
|
},
|
||||||
pageRanges: params.pageRanges.split(', '),
|
pageRanges: pageRanges.split(', '),
|
||||||
scale: params.scale,
|
scale,
|
||||||
shrinkToFit: !params.preferCSSPageSize,
|
shrinkToFit: !preferCSSPageSize,
|
||||||
}),
|
}),
|
||||||
'browsingContext.print',
|
'browsingContext.print',
|
||||||
params.timeout
|
timeout
|
||||||
);
|
);
|
||||||
|
|
||||||
const buffer = Buffer.from(result.data, 'base64');
|
const buffer = Buffer.from(result.data, 'base64');
|
||||||
|
Loading…
Reference in New Issue
Block a user