feat(chromium): expose parameters to generate outline / tagged PDF (#29494)
Signed-off-by: Max Schmitt <max@schmitt.mx> Co-authored-by: Max Schmitt <max@schmitt.mx>
This commit is contained in:
parent
a3d62acbdd
commit
b11b118e02
|
|
@ -2988,6 +2988,18 @@ Give any CSS `@page` size declared in the page priority over what is declared in
|
||||||
[`option: height`] or [`option: format`] options. Defaults to `false`, which will scale the content to fit the paper
|
[`option: height`] or [`option: format`] options. Defaults to `false`, which will scale the content to fit the paper
|
||||||
size.
|
size.
|
||||||
|
|
||||||
|
### option: Page.pdf.tagged
|
||||||
|
* since: v1.42
|
||||||
|
- `tagged` <[boolean]>
|
||||||
|
|
||||||
|
Whether or not to generate tagged (accessible) PDF. Defaults to `false`.
|
||||||
|
|
||||||
|
### option: Page.pdf.outline
|
||||||
|
* since: v1.42
|
||||||
|
- `outline` <[boolean]>
|
||||||
|
|
||||||
|
Whether or not to embed the document outline into the PDF. Defaults to `false`.
|
||||||
|
|
||||||
## async method: Page.press
|
## async method: Page.press
|
||||||
* since: v1.8
|
* since: v1.8
|
||||||
* discouraged: Use locator-based [`method: Locator.press`] instead. Read more about [locators](../locators.md).
|
* discouraged: Use locator-based [`method: Locator.press`] instead. Read more about [locators](../locators.md).
|
||||||
|
|
|
||||||
|
|
@ -1203,6 +1203,8 @@ scheme.PagePdfParams = tObject({
|
||||||
left: tOptional(tString),
|
left: tOptional(tString),
|
||||||
right: tOptional(tString),
|
right: tOptional(tString),
|
||||||
})),
|
})),
|
||||||
|
tagged: tOptional(tBoolean),
|
||||||
|
outline: tOptional(tBoolean),
|
||||||
});
|
});
|
||||||
scheme.PagePdfResult = tObject({
|
scheme.PagePdfResult = tObject({
|
||||||
pdf: tBinary,
|
pdf: tBinary,
|
||||||
|
|
|
||||||
|
|
@ -78,6 +78,8 @@ export class CRPDF {
|
||||||
pageRanges = '',
|
pageRanges = '',
|
||||||
preferCSSPageSize = false,
|
preferCSSPageSize = false,
|
||||||
margin = {},
|
margin = {},
|
||||||
|
tagged = false,
|
||||||
|
outline = false
|
||||||
} = options;
|
} = options;
|
||||||
|
|
||||||
let paperWidth = 8.5;
|
let paperWidth = 8.5;
|
||||||
|
|
@ -96,7 +98,8 @@ export class CRPDF {
|
||||||
const marginLeft = convertPrintParameterToInches(margin.left) || 0;
|
const marginLeft = convertPrintParameterToInches(margin.left) || 0;
|
||||||
const marginBottom = convertPrintParameterToInches(margin.bottom) || 0;
|
const marginBottom = convertPrintParameterToInches(margin.bottom) || 0;
|
||||||
const marginRight = convertPrintParameterToInches(margin.right) || 0;
|
const marginRight = convertPrintParameterToInches(margin.right) || 0;
|
||||||
|
const generateDocumentOutline = outline;
|
||||||
|
const generateTaggedPDF = tagged;
|
||||||
const result = await this._client.send('Page.printToPDF', {
|
const result = await this._client.send('Page.printToPDF', {
|
||||||
transferMode: 'ReturnAsStream',
|
transferMode: 'ReturnAsStream',
|
||||||
landscape,
|
landscape,
|
||||||
|
|
@ -112,7 +115,9 @@ export class CRPDF {
|
||||||
marginLeft,
|
marginLeft,
|
||||||
marginRight,
|
marginRight,
|
||||||
pageRanges,
|
pageRanges,
|
||||||
preferCSSPageSize
|
preferCSSPageSize,
|
||||||
|
generateTaggedPDF,
|
||||||
|
generateDocumentOutline
|
||||||
});
|
});
|
||||||
return await readProtocolStream(this._client, result.stream!);
|
return await readProtocolStream(this._client, result.stream!);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
10
packages/playwright-core/types/types.d.ts
vendored
10
packages/playwright-core/types/types.d.ts
vendored
|
|
@ -3492,6 +3492,11 @@ export interface Page {
|
||||||
left?: string|number;
|
left?: string|number;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether or not to embed the document outline into the PDF. Defaults to `false`.
|
||||||
|
*/
|
||||||
|
outline?: boolean;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Paper ranges to print, e.g., '1-5, 8, 11-13'. Defaults to the empty string, which means print all pages.
|
* Paper ranges to print, e.g., '1-5, 8, 11-13'. Defaults to the empty string, which means print all pages.
|
||||||
*/
|
*/
|
||||||
|
|
@ -3519,6 +3524,11 @@ export interface Page {
|
||||||
*/
|
*/
|
||||||
scale?: number;
|
scale?: number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether or not to generate tagged (accessible) PDF. Defaults to `false`.
|
||||||
|
*/
|
||||||
|
tagged?: boolean;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Paper width, accepts values labeled with units.
|
* Paper width, accepts values labeled with units.
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -2174,6 +2174,8 @@ export type PagePdfParams = {
|
||||||
left?: string,
|
left?: string,
|
||||||
right?: string,
|
right?: string,
|
||||||
},
|
},
|
||||||
|
tagged?: boolean,
|
||||||
|
outline?: boolean,
|
||||||
};
|
};
|
||||||
export type PagePdfOptions = {
|
export type PagePdfOptions = {
|
||||||
scale?: number,
|
scale?: number,
|
||||||
|
|
@ -2193,6 +2195,8 @@ export type PagePdfOptions = {
|
||||||
left?: string,
|
left?: string,
|
||||||
right?: string,
|
right?: string,
|
||||||
},
|
},
|
||||||
|
tagged?: boolean,
|
||||||
|
outline?: boolean,
|
||||||
};
|
};
|
||||||
export type PagePdfResult = {
|
export type PagePdfResult = {
|
||||||
pdf: Binary,
|
pdf: Binary,
|
||||||
|
|
|
||||||
|
|
@ -1560,6 +1560,8 @@ Page:
|
||||||
bottom: string?
|
bottom: string?
|
||||||
left: string?
|
left: string?
|
||||||
right: string?
|
right: string?
|
||||||
|
tagged: boolean?
|
||||||
|
outline: boolean?
|
||||||
returns:
|
returns:
|
||||||
pdf: binary
|
pdf: binary
|
||||||
|
|
||||||
|
|
|
||||||
15
tests/assets/headings.html
Normal file
15
tests/assets/headings.html
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<title>Headings</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<h1>Title</h1>
|
||||||
|
<h2>Subtitle</h2>
|
||||||
|
<h3>Subsubtitle</h3>
|
||||||
|
<h2>Subtitle</h2>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
||||||
|
|
@ -26,3 +26,18 @@ it('should be able to save file', async ({ contextFactory, headless, browserName
|
||||||
await page.pdf({ path: outputFile });
|
await page.pdf({ path: outputFile });
|
||||||
expect(fs.readFileSync(outputFile).byteLength).toBeGreaterThan(0);
|
expect(fs.readFileSync(outputFile).byteLength).toBeGreaterThan(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should be able to generate outline', async ({ contextFactory, server, headless, browserName }, testInfo) => {
|
||||||
|
it.skip(!headless || browserName !== 'chromium', 'Printing to pdf is currently only supported in headless chromium.');
|
||||||
|
// const context = await contextFactory();
|
||||||
|
const context = await contextFactory({
|
||||||
|
baseURL: server.PREFIX,
|
||||||
|
});
|
||||||
|
const page = await context.newPage();
|
||||||
|
await page.goto('/headings.html');
|
||||||
|
const outputFileNoOutline = testInfo.outputPath('outputNoOutline.pdf');
|
||||||
|
const outputFileOutline = testInfo.outputPath('outputOutline.pdf');
|
||||||
|
await page.pdf({ path: outputFileNoOutline });
|
||||||
|
await page.pdf({ path: outputFileOutline, tagged: true, outline: true });
|
||||||
|
expect(fs.readFileSync(outputFileOutline).byteLength).toBeGreaterThan(fs.readFileSync(outputFileNoOutline).byteLength);
|
||||||
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue