Skip to content

Commit

Permalink
feat: added tagged (accessible) PDFs option (#11182)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightning00Blade committed Oct 17, 2023
1 parent 443ee99 commit 0316863
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 9 deletions.
1 change: 1 addition & 0 deletions docs/api/puppeteer.pdfoptions.md
Expand Up @@ -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. | |
7 changes: 3 additions & 4 deletions packages/puppeteer-core/src/api/Page.ts
Expand Up @@ -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: '',
Expand All @@ -2688,6 +2688,7 @@ export abstract class Page extends EventEmitter<PageEvents> {
preferCSSPageSize: false,
omitBackground: false,
timeout: 30000,
tagged: false,
};

let width = 8.5;
Expand All @@ -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;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/puppeteer-core/src/cdp/Page.ts
Expand Up @@ -1140,6 +1140,7 @@ export class CdpPage extends Page {
preferCSSPageSize,
omitBackground,
timeout: ms,
tagged: generateTaggedPDF,
} = this._getPDFOptions(options);

if (omitBackground) {
Expand All @@ -1164,6 +1165,7 @@ export class CdpPage extends Page {
marginRight: margin.right,
pageRanges,
preferCSSPageSize,
generateTaggedPDF,
}
);

Expand Down
6 changes: 6 additions & 0 deletions packages/puppeteer-core/src/common/PDFOptions.ts
Expand Up @@ -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`
Expand Down
12 changes: 12 additions & 0 deletions test/TestExpectations.json
Expand Up @@ -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"],
Expand Down Expand Up @@ -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"],
Expand Down
31 changes: 26 additions & 5 deletions test/src/page.spec.ts
Expand Up @@ -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 () => {
Expand All @@ -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);
});
Expand Down

0 comments on commit 0316863

Please sign in to comment.