Skip to content

🎥 Make videos programmatically with React

remove pdfoptions

Jonny Burger9ff2d391d0c8
diff --git a/packages/renderer/src/browser/PDFOptions.ts b/packages/renderer/src/browser/PDFOptions.ts
deleted file mode 100644
index 8ad6e17ac8..0000000000
--- a/packages/renderer/src/browser/PDFOptions.ts
+++ /dev/null
@@ -1,200 +0,0 @@
-/**
- * Copyright 2020 Google Inc. All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/**
- * @public
- */
-export interface PDFMargin {
- top?: string | number;
- bottom?: string | number;
- left?: string | number;
- right?: string | number;
-}
-
-/**
- * @public
- */
-export type LowerCasePaperFormat =
- | 'letter'
- | 'legal'
- | 'tabloid'
- | 'ledger'
- | 'a0'
- | 'a1'
- | 'a2'
- | 'a3'
- | 'a4'
- | 'a5'
- | 'a6';
-
-/**
- * All the valid paper format types when printing a PDF.
- *
- * @remarks
- *
- * The sizes of each format are as follows:
- * - `Letter`: 8.5in x 11in
- *
- * - `Legal`: 8.5in x 14in
- *
- * - `Tabloid`: 11in x 17in
- *
- * - `Ledger`: 17in x 11in
- *
- * - `A0`: 33.1in x 46.8in
- *
- * - `A1`: 23.4in x 33.1in
- *
- * - `A2`: 16.54in x 23.4in
- *
- * - `A3`: 11.7in x 16.54in
- *
- * - `A4`: 8.27in x 11.7in
- *
- * - `A5`: 5.83in x 8.27in
- *
- * - `A6`: 4.13in x 5.83in
- *
- * @public
- */
-export type PaperFormat =
- | Uppercase<LowerCasePaperFormat>
- | Capitalize<LowerCasePaperFormat>
- | LowerCasePaperFormat;
-
-/**
- * Valid options to configure PDF generation via {@link Page.pdf}.
- * @public
- */
-export interface PDFOptions {
- /**
- * Scales the rendering of the web page. Amount must be between `0.1` and `2`.
- * @defaultValue 1
- */
- scale?: number;
- /**
- * Whether to show the header and footer.
- * @defaultValue false
- */
- displayHeaderFooter?: boolean;
- /**
- * HTML template for the print header. Should be valid HTML with the following
- * classes used to inject values into them:
- * - `date` formatted print date
- *
- * - `title` document title
- *
- * - `url` document location
- *
- * - `pageNumber` current page number
- *
- * - `totalPages` total pages in the document
- */
- headerTemplate?: string;
- /**
- * HTML template for the print footer. Has the same constraints and support
- * for special classes as {@link PDFOptions.headerTemplate}.
- */
- footerTemplate?: string;
- /**
- * Set to `true` to print background graphics.
- * @defaultValue false
- */
- printBackground?: boolean;
- /**
- * Whether to print in landscape orientation.
- * @defaultValue = false
- */
- landscape?: boolean;
- /**
- * Paper ranges to print, e.g. `1-5, 8, 11-13`.
- * @defaultValue The empty string, which means all pages are printed.
- */
- pageRanges?: string;
- /**
- * @remarks
- * If set, this takes priority over the `width` and `height` options.
- * @defaultValue `letter`.
- */
- format?: PaperFormat;
- /**
- * Sets the width of paper. You can pass in a number or a string with a unit.
- */
- width?: string | number;
- /**
- * Sets the height of paper. You can pass in a number or a string with a unit.
- */
- height?: string | number;
- /**
- * Give any CSS `@page` size declared in the page priority over what is
- * declared in the `width` or `height` or `format` option.
- * @defaultValue `false`, which will scale the content to fit the paper size.
- */
- preferCSSPageSize?: boolean;
- /**
- * Set the PDF margins.
- * @defaultValue no margins are set.
- */
- margin?: PDFMargin;
- /**
- * The path to save the file to.
- *
- * @remarks
- *
- * If the path is relative, it's resolved relative to the current working directory.
- *
- * @defaultValue the empty string, which means the PDF will not be written to disk.
- */
- path?: string;
- /**
- * Hides default white background and allows generating pdfs with transparency.
- * @defaultValue false
- */
- omitBackground?: boolean;
- /**
- * Timeout in milliseconds
- * @defaultValue 30000
- */
- timeout?: number;
-}
-
-/**
- * @internal
- */
-export interface PaperFormatDimensions {
- width: number;
- height: number;
-}
-
-/**
- * @internal
- */
-export const _paperFormats: Record<
- LowerCasePaperFormat,
- PaperFormatDimensions
-> = {
- letter: {width: 8.5, height: 11},
- legal: {width: 8.5, height: 14},
- tabloid: {width: 11, height: 17},
- ledger: {width: 17, height: 11},
- a0: {width: 33.1, height: 46.8},
- a1: {width: 23.4, height: 33.1},
- a2: {width: 16.54, height: 23.4},
- a3: {width: 11.7, height: 16.54},
- a4: {width: 8.27, height: 11.7},
- a5: {width: 5.83, height: 8.27},
- a6: {width: 4.13, height: 5.83},
-} as const;
diff --git a/packages/renderer/src/browser/Page.ts b/packages/renderer/src/browser/Page.ts
index bdd938ba3d..e5ded92989 100644
--- a/packages/renderer/src/browser/Page.ts
+++ b/packages/renderer/src/browser/Page.ts
@@ -15,7 +15,6 @@
*/
import {Protocol} from 'devtools-protocol';
-import type {Readable} from 'stream';
import {assert, assertNever} from './assert';
import {Browser, BrowserContext} from './Browser';
import {CDPSession, CDPSessionEmittedEvents} from './Connection';
@@ -43,7 +42,6 @@ import {
NetworkConditions,
NetworkManagerEmittedEvents,
} from './NetworkManager';
-import {LowerCasePaperFormat, PDFOptions, _paperFormats} from './PDFOptions';
import {Viewport} from './PuppeteerViewport';
import {Target} from './Target';
import {TaskQueue} from './TaskQueue';
@@ -53,10 +51,7 @@ import {
debugError,
evaluationString,
getExceptionMessage,
- getReadableAsBuffer,
- getReadableFromProtocolStream,
isErrorLike,
- isNumber,
isString,
pageBindingDeliverErrorString,
pageBindingDeliverErrorValueString,
@@ -231,12 +226,6 @@ export const enum PageEmittedEvents {
* ```
*/
Console = 'console',
- /**
- * Emitted when a JavaScript dialog appears, such as `alert`, `prompt`,
- * `confirm` or `beforeunload`. Puppeteer can respond to the dialog via
- * {@link Dialog.accept} or {@link Dialog.dismiss}.
- */
- Dialog = 'dialog',
/**
* Emitted when the JavaScript
* {@link https://developer.mozilla.org/en-US/docs/Web/Events/DOMContentLoaded | DOMContentLoaded } event is dispatched.
@@ -556,9 +545,6 @@ export class Page extends EventEmitter {
client.on('Runtime.bindingCalled', (event) => {
return this.#onBindingCalled(event);
});
- client.on('Page.javascriptDialogOpening', (event) => {
- return this.#onDialog(event);
- });
client.on('Runtime.exceptionThrown', (exception) => {
return this.#handleException(exception.exceptionDetails);
});
@@ -1453,30 +1439,6 @@ export class Page extends EventEmitter {
this.emit(PageEmittedEvents.Console, message);
}
- #onDialog(event: Protocol.Page.JavascriptDialogOpeningEvent): void {
- let dialogType = null;
- const validDialogTypes = new Set<Protocol.Page.DialogType>([
- 'alert',
- 'confirm',
- 'prompt',
- 'beforeunload',
- ]);
-
- if (validDialogTypes.has(event.type)) {
- dialogType = event.type as Protocol.Page.DialogType;
- }
-
- assert(dialogType, 'Unknown javascript dialog type: ' + event.type);
-
- const dialog = new Dialog(
- this.#client,
- dialogType,
- event.message,
- event.defaultPrompt
- );
- this.emit(PageEmittedEvents.Dialog, dialog);
- }
-
/**
* Resets default white background
*/
@@ -2708,106 +2670,6 @@ export class Page extends EventEmitter {
}
}
- /**
- * Generates a PDF of the page with the `print` CSS media type.
- * @remarks
- *
- * NOTE: PDF generation is only supported in Chrome headless mode.
- *
- * To generate a PDF with the `screen` media type, call
- * {@link Page.emulateMediaType | `page.emulateMediaType('screen')`} before
- * calling `page.pdf()`.
- *
- * By default, `page.pdf()` generates a pdf with modified colors for printing.
- * Use the
- * {@link https://developer.mozilla.org/en-US/docs/Web/CSS/-webkit-print-color-adjust | `-webkit-print-color-adjust`}
- * property to force rendering of exact colors.
- *
- *
- * @param options - options for generating the PDF.
- */
- async createPDFStream(options: PDFOptions = {}): Promise<Readable> {
- const {
- scale = 1,
- displayHeaderFooter = false,
- headerTemplate = '',
- footerTemplate = '',
- printBackground = false,
- landscape = false,
- pageRanges = '',
- preferCSSPageSize = false,
- margin = {},
- omitBackground = false,
- timeout = 30000,
- } = options;
-
- let paperWidth = 8.5;
- let paperHeight = 11;
- if (options.format) {
- const format =
- _paperFormats[options.format.toLowerCase() as LowerCasePaperFormat];
- assert(format, 'Unknown paper format: ' + options.format);
- paperWidth = format.width;
- paperHeight = format.height;
- } else {
- paperWidth = convertPrintParameterToInches(options.width) || paperWidth;
- paperHeight =
- convertPrintParameterToInches(options.height) || paperHeight;
- }
-
- const marginTop = convertPrintParameterToInches(margin.top) || 0;
- const marginLeft = convertPrintParameterToInches(margin.left) || 0;
- const marginBottom = convertPrintParameterToInches(margin.bottom) || 0;
- const marginRight = convertPrintParameterToInches(margin.right) || 0;
-
- if (omitBackground) {
- await this.#setTransparentBackgroundColor();
- }
-
- const printCommandPromise = this.#client.send('Page.printToPDF', {
- transferMode: 'ReturnAsStream',
- landscape,
- displayHeaderFooter,
- headerTemplate,
- footerTemplate,
- printBackground,
- scale,
- paperWidth,
- paperHeight,
- marginTop,
- marginBottom,
- marginLeft,
- marginRight,
- pageRanges,
- preferCSSPageSize,
- });
-
- const result = await waitWithTimeout(
- printCommandPromise,
- 'Page.printToPDF',
- timeout
- );
-
- if (omitBackground) {
- await this.#resetDefaultBackgroundColor();
- }
-
- assert(result.stream, '`stream` is missing from `Page.printToPDF');
- return getReadableFromProtocolStream(this.#client, result.stream);
- }
-
- /**
- * @param options -
- * @returns
- */
- async pdf(options: PDFOptions = {}): Promise<Buffer> {
- const {path = undefined} = options;
- const readable = await this.createPDFStream(options);
- const buffer = await getReadableAsBuffer(readable, path);
- assert(buffer, 'Could not create buffer');
- return buffer;
- }
-
/**
* @returns The page's title
* @remarks
@@ -3065,46 +2927,3 @@ const supportedMetrics = new Set<string>([
'JSHeapUsedSize',
'JSHeapTotalSize',
]);
-
-const unitToPixels = {
- px: 1,
- in: 96,
- cm: 37.8,
- mm: 3.78,
-};
-
-function convertPrintParameterToInches(
- parameter?: string | number
-): number | undefined {
- if (typeof parameter === 'undefined') {
- return undefined;
- }
-
- let pixels;
- if (isNumber(parameter)) {
- // Treat numbers as pixel values to be aligned with phantom's paperSize.
- pixels = parameter;
- } else if (isString(parameter)) {
- const text = parameter;
- let unit = text.substring(text.length - 2).toLowerCase();
- let valueText = '';
- if (unit in unitToPixels) {
- valueText = text.substring(0, text.length - 2);
- } else {
- // In case of unknown unit try to parse the whole parameter as number of pixels.
- // This is consistent with phantom's paperSize behavior.
- unit = 'px';
- valueText = text;
- }
-
- const value = Number(valueText);
- assert(!isNaN(value), 'Failed to parse parameter value: ' + text);
- pixels = value * unitToPixels[unit as keyof typeof unitToPixels];
- } else {
- throw new Error(
- 'page.pdf() Cannot handle parameter type: ' + typeof parameter
- );
- }
-
- return pixels / 96;
-}