From 031686339136873c555a19ffb871f7140a2c39d9 Mon Sep 17 00:00:00 2001
From: Nikolay Vitkov <34244704+Lightning00Blade@users.noreply.github.com>
Date: Tue, 17 Oct 2023 16:37:07 +0200
Subject: [PATCH] feat: added tagged (accessible) PDFs option (#11182)
---
docs/api/puppeteer.pdfoptions.md | 1 +
packages/puppeteer-core/src/api/Page.ts | 7 ++---
packages/puppeteer-core/src/cdp/Page.ts | 2 ++
.../puppeteer-core/src/common/PDFOptions.ts | 6 ++++
test/TestExpectations.json | 12 +++++++
test/src/page.spec.ts | 31 ++++++++++++++++---
6 files changed, 50 insertions(+), 9 deletions(-)
diff --git a/docs/api/puppeteer.pdfoptions.md b/docs/api/puppeteer.pdfoptions.md
index e764f614..00cc8aa3 100644
--- a/docs/api/puppeteer.pdfoptions.md
+++ b/docs/api/puppeteer.pdfoptions.md
@@ -29,5 +29,6 @@ export interface PDFOptions
| preferCSSPageSize | optional
| boolean | Give any CSS @page
size declared in the page priority over what is declared in the width
or height
or format
option. | false
, which will scale the content to fit the paper size. |
| printBackground | optional
| boolean | Set to true
to print background graphics. | false
|
| scale | optional
| number | Scales the rendering of the web page. Amount must be between 0.1
and 2
. | 1
|
+| tagged | optional
| boolean | Generate tagged (accessible) PDF. | false
|
| timeout | optional
| number | Timeout in milliseconds. Pass 0
to disable timeout. | 30_000
|
| width | optional
| string \| number | Sets the width of paper. You can pass in a number or a string with a unit. | |
diff --git a/packages/puppeteer-core/src/api/Page.ts b/packages/puppeteer-core/src/api/Page.ts
index a3052797..c23ab8d7 100644
--- a/packages/puppeteer-core/src/api/Page.ts
+++ b/packages/puppeteer-core/src/api/Page.ts
@@ -2677,7 +2677,7 @@ export abstract class Page extends EventEmitter {
options: PDFOptions = {},
lengthUnit: 'in' | 'cm' = 'in'
): ParsedPDFOptions {
- const defaults = {
+ const defaults: Omit = {
scale: 1,
displayHeaderFooter: false,
headerTemplate: '',
@@ -2688,6 +2688,7 @@ export abstract class Page extends EventEmitter {
preferCSSPageSize: false,
omitBackground: false,
timeout: 30000,
+ tagged: false,
};
let width = 8.5;
@@ -2714,15 +2715,13 @@ export abstract class Page extends EventEmitter {
convertPrintParameterToInches(options.margin?.right, lengthUnit) || 0,
};
- const output = {
+ return {
...defaults,
...options,
width,
height,
margin,
};
-
- return output;
}
/**
diff --git a/packages/puppeteer-core/src/cdp/Page.ts b/packages/puppeteer-core/src/cdp/Page.ts
index 722ac4ea..17197caf 100644
--- a/packages/puppeteer-core/src/cdp/Page.ts
+++ b/packages/puppeteer-core/src/cdp/Page.ts
@@ -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,
}
);
diff --git a/packages/puppeteer-core/src/common/PDFOptions.ts b/packages/puppeteer-core/src/common/PDFOptions.ts
index 7b9eed78..29326e61 100644
--- a/packages/puppeteer-core/src/common/PDFOptions.ts
+++ b/packages/puppeteer-core/src/common/PDFOptions.ts
@@ -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`
diff --git a/test/TestExpectations.json b/test/TestExpectations.json
index 7acb3a7e..f9d39463 100644
--- a/test/TestExpectations.json
+++ b/test/TestExpectations.json
@@ -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"],
diff --git a/test/src/page.spec.ts b/test/src/page.spec.ts
index 63d10f46..4ee9b9f7 100644
--- a/test/src/page.spec.ts
+++ b/test/src/page.spec.ts
@@ -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);
});