Skip to content

Commit

Permalink
chore: fix BiDi to use cm (#9926)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightning00Blade committed Mar 28, 2023
1 parent 3866e46 commit c4e1675
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 37 deletions.
26 changes: 17 additions & 9 deletions packages/puppeteer-core/src/api/Page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2171,7 +2171,10 @@ export class Page extends EventEmitter {
/**
* @internal
*/
_getPDFOptions(options: PDFOptions = {}): ParsedPDFOptions {
_getPDFOptions(
options: PDFOptions = {},
lengthUnit: 'in' | 'cm' = 'in'
): ParsedPDFOptions {
const defaults = {
scale: 1,
displayHeaderFooter: false,
Expand All @@ -2194,15 +2197,19 @@ export class Page extends EventEmitter {
width = format.width;
height = format.height;
} else {
width = convertPrintParameterToInches(options.width) ?? width;
height = convertPrintParameterToInches(options.height) ?? height;
width = convertPrintParameterToInches(options.width, lengthUnit) ?? width;
height =
convertPrintParameterToInches(options.height, lengthUnit) ?? height;
}

const margin = {
top: convertPrintParameterToInches(options.margin?.top) || 0,
left: convertPrintParameterToInches(options.margin?.left) || 0,
bottom: convertPrintParameterToInches(options.margin?.bottom) || 0,
right: convertPrintParameterToInches(options.margin?.right) || 0,
top: convertPrintParameterToInches(options.margin?.top, lengthUnit) || 0,
left:
convertPrintParameterToInches(options.margin?.left, lengthUnit) || 0,
bottom:
convertPrintParameterToInches(options.margin?.bottom, lengthUnit) || 0,
right:
convertPrintParameterToInches(options.margin?.right, lengthUnit) || 0,
};

const output = {
Expand Down Expand Up @@ -2698,7 +2705,8 @@ export const unitToPixels = {
};

function convertPrintParameterToInches(
parameter?: string | number
parameter?: string | number,
lengthUnit: 'in' | 'cm' = 'in'
): number | undefined {
if (typeof parameter === 'undefined') {
return undefined;
Expand Down Expand Up @@ -2727,5 +2735,5 @@ function convertPrintParameterToInches(
'page.pdf() Cannot handle parameter type: ' + typeof parameter
);
}
return pixels / 96;
return pixels / unitToPixels[lengthUnit];
}
50 changes: 32 additions & 18 deletions packages/puppeteer-core/src/common/Page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1458,37 +1458,51 @@ export class CDPPage extends Page {
}

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();
}

const printCommandPromise = this.#client.send('Page.printToPDF', {
transferMode: 'ReturnAsStream',
landscape: params.landscape,
displayHeaderFooter: params.displayHeaderFooter,
headerTemplate: params.headerTemplate,
footerTemplate: params.footerTemplate,
printBackground: params.printBackground,
scale: params.scale,
paperWidth: params.width,
paperHeight: params.height,
marginTop: params.margin.top,
marginBottom: params.margin.bottom,
marginLeft: params.margin.left,
marginRight: params.margin.right,
pageRanges: params.pageRanges,
preferCSSPageSize: params.preferCSSPageSize,
landscape,
displayHeaderFooter,
headerTemplate,
footerTemplate,
printBackground,
scale,
paperWidth,
paperHeight,
marginTop: margin.top,
marginBottom: margin.bottom,
marginLeft: margin.left,
marginRight: margin.right,
pageRanges,
preferCSSPageSize,
});

const result = await waitWithTimeout(
printCommandPromise,
'Page.printToPDF',
params.timeout
timeout
);

if (params.omitBackground) {
if (omitBackground) {
await this.#resetDefaultBackgroundColor();
}

Expand Down
30 changes: 20 additions & 10 deletions packages/puppeteer-core/src/common/bidi/Page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,23 +206,33 @@ export class Page extends PageBase {

override async pdf(options: PDFOptions = {}): Promise<Buffer> {
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(
this.#context.connection.send('browsingContext.print', {
context: this.#context._contextId,
background: params.printBackground,
margin: params.margin,
orientation: params.landscape ? 'landscape' : 'portrait',
background,
margin,
orientation: landscape ? 'landscape' : 'portrait',
page: {
width: params.width,
height: params.height,
width,
height,
},
pageRanges: params.pageRanges.split(', '),
scale: params.scale,
shrinkToFit: !params.preferCSSPageSize,
pageRanges: pageRanges.split(', '),
scale,
shrinkToFit: !preferCSSPageSize,
}),
'browsingContext.print',
params.timeout
timeout
);

const buffer = Buffer.from(result.data, 'base64');
Expand Down

0 comments on commit c4e1675

Please sign in to comment.