feat: added tagged (accessible) PDFs option (#11182)

This commit is contained in:
Nikolay Vitkov 2023-10-17 16:37:07 +02:00 committed by GitHub
parent 443ee99a8d
commit 0316863391
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 50 additions and 9 deletions

View File

@ -29,5 +29,6 @@ export interface PDFOptions
| preferCSSPageSize | <code>optional</code> | boolean | Give any CSS <code>@page</code> size declared in the page priority over what is declared in the <code>width</code> or <code>height</code> or <code>format</code> option. | <code>false</code>, which will scale the content to fit the paper size. |
| printBackground | <code>optional</code> | boolean | Set to <code>true</code> to print background graphics. | <code>false</code> |
| scale | <code>optional</code> | number | Scales the rendering of the web page. Amount must be between <code>0.1</code> and <code>2</code>. | <code>1</code> |
| tagged | <code>optional</code> | boolean | Generate tagged (accessible) PDF. | <code>false</code> |
| timeout | <code>optional</code> | number | Timeout in milliseconds. Pass <code>0</code> to disable timeout. | <code>30_000</code> |
| width | <code>optional</code> | string \| number | Sets the width of paper. You can pass in a number or a string with a unit. | |

View File

@ -2677,7 +2677,7 @@ export abstract class Page extends EventEmitter<PageEvents> {
options: PDFOptions = {},
lengthUnit: 'in' | 'cm' = 'in'
): ParsedPDFOptions {
const defaults = {
const defaults: Omit<ParsedPDFOptions, 'width' | 'height' | 'margin'> = {
scale: 1,
displayHeaderFooter: false,
headerTemplate: '',
@ -2688,6 +2688,7 @@ export abstract class Page extends EventEmitter<PageEvents> {
preferCSSPageSize: false,
omitBackground: false,
timeout: 30000,
tagged: false,
};
let width = 8.5;
@ -2714,15 +2715,13 @@ export abstract class Page extends EventEmitter<PageEvents> {
convertPrintParameterToInches(options.margin?.right, lengthUnit) || 0,
};
const output = {
return {
...defaults,
...options,
width,
height,
margin,
};
return output;
}
/**

View File

@ -1140,6 +1140,7 @@ export class CdpPage extends Page {
preferCSSPageSize,
omitBackground,
timeout: ms,
tagged: generateTaggedPDF,
} = this._getPDFOptions(options);
if (omitBackground) {
@ -1164,6 +1165,7 @@ export class CdpPage extends Page {
marginRight: margin.right,
pageRanges,
preferCSSPageSize,
generateTaggedPDF,
}
);

View File

@ -166,6 +166,12 @@ export interface PDFOptions {
* @defaultValue `false`
*/
omitBackground?: boolean;
/**
* Generate tagged (accessible) PDF.
* @defaultValue `false`
* @experimental
*/
tagged?: boolean;
/**
* Timeout in milliseconds. Pass `0` to disable timeout.
* @defaultValue `30_000`

View File

@ -1007,6 +1007,12 @@
"parameters": ["chrome", "webDriverBiDi"],
"expectations": ["PASS"]
},
{
"testIdPattern": "[page.spec] Page Page.pdf can print to PDF with accessible",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["webDriverBiDi"],
"expectations": ["SKIP"]
},
{
"testIdPattern": "[page.spec] Page Page.pdf should respect timeout",
"platforms": ["darwin", "linux", "win32"],
@ -3197,6 +3203,12 @@
"parameters": ["cdp", "firefox"],
"expectations": ["FAIL"]
},
{
"testIdPattern": "[page.spec] Page Page.pdf can print to PDF with accessible",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["cdp", "firefox"],
"expectations": ["SKIP"]
},
{
"testIdPattern": "[page.spec] Page Page.removeExposedFunction should work",
"platforms": ["darwin", "linux", "win32"],

View File

@ -1989,8 +1989,30 @@ describe('Page', function () {
const outputFile = __dirname + '/../assets/output.pdf';
await page.goto(server.PREFIX + '/pdf.html');
await page.pdf({path: outputFile});
expect(fs.readFileSync(outputFile).byteLength).toBeGreaterThan(0);
fs.unlinkSync(outputFile);
try {
expect(fs.readFileSync(outputFile).byteLength).toBeGreaterThan(0);
} finally {
fs.unlinkSync(outputFile);
}
});
it('can print to PDF with accessible', async () => {
const {page, server} = await getTestState();
const outputFile = __dirname + '/../assets/output.pdf';
const outputFileAccessible =
__dirname + '/../assets/output-accessible.pdf';
await page.goto(server.PREFIX + '/pdf.html');
await page.pdf({path: outputFile});
await page.pdf({path: outputFileAccessible, tagged: true});
try {
expect(
fs.readFileSync(outputFileAccessible).byteLength
).toBeGreaterThan(fs.readFileSync(outputFile).byteLength);
} finally {
fs.unlinkSync(outputFileAccessible);
fs.unlinkSync(outputFile);
}
});
it('can print to PDF and stream the result', async () => {
@ -2009,9 +2031,8 @@ describe('Page', function () {
await page.goto(server.PREFIX + '/pdf.html');
let error!: Error;
await page.pdf({timeout: 1}).catch(_error => {
return (error = _error);
const error = await page.pdf({timeout: 1}).catch(err => {
return err;
});
expect(error).toBeInstanceOf(TimeoutError);
});