tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

PDFOptions.ts (6134B)


      1 /**
      2 * @license
      3 * Copyright 2020 Google Inc.
      4 * SPDX-License-Identifier: Apache-2.0
      5 */
      6 
      7 /**
      8 * @public
      9 */
     10 export interface PDFMargin {
     11  top?: string | number;
     12  bottom?: string | number;
     13  left?: string | number;
     14  right?: string | number;
     15 }
     16 
     17 /**
     18 * @public
     19 */
     20 export type LowerCasePaperFormat =
     21  | 'letter'
     22  | 'legal'
     23  | 'tabloid'
     24  | 'ledger'
     25  | 'a0'
     26  | 'a1'
     27  | 'a2'
     28  | 'a3'
     29  | 'a4'
     30  | 'a5'
     31  | 'a6';
     32 
     33 /**
     34 * All the valid paper format types when printing a PDF.
     35 *
     36 * @remarks
     37 *
     38 * The sizes of each format are as follows:
     39 *
     40 * - `Letter`: 8.5in x 11in / 21.59cm x 27.94cm
     41 *
     42 * - `Legal`: 8.5in x 14in / 21.59cm x 35.56cm
     43 *
     44 * - `Tabloid`: 11in x 17in / 27.94cm x 43.18cm
     45 *
     46 * - `Ledger`: 17in x 11in / 43.18cm x 27.94cm
     47 *
     48 * - `A0`: 33.1102in x 46.811in / 84.1cm x 118.9cm
     49 *
     50 * - `A1`: 23.3858in x 33.1102in / 59.4cm x 84.1cm
     51 *
     52 * - `A2`: 16.5354in x 23.3858in / 42cm x 59.4cm
     53 *
     54 * - `A3`: 11.6929in x 16.5354in / 29.7cm x 42cm
     55 *
     56 * - `A4`: 8.2677in x 11.6929in / 21cm x 29.7cm
     57 *
     58 * - `A5`: 5.8268in x 8.2677in / 14.8cm x 21cm
     59 *
     60 * - `A6`: 4.1339in x 5.8268in / 10.5cm x 14.8cm
     61 *
     62 * @public
     63 */
     64 export type PaperFormat =
     65  | Uppercase<LowerCasePaperFormat>
     66  | Capitalize<LowerCasePaperFormat>
     67  | LowerCasePaperFormat;
     68 
     69 /**
     70 * Valid options to configure PDF generation via {@link Page.pdf}.
     71 * @public
     72 */
     73 export interface PDFOptions {
     74  /**
     75   * Scales the rendering of the web page. Amount must be between `0.1` and `2`.
     76   * @defaultValue `1`
     77   */
     78  scale?: number;
     79  /**
     80   * Whether to show the header and footer.
     81   * @defaultValue `false`
     82   */
     83  displayHeaderFooter?: boolean;
     84  /**
     85   * HTML template for the print header. Should be valid HTML with the following
     86   * classes used to inject values into them:
     87   *
     88   * - `date` formatted print date
     89   *
     90   * - `title` document title
     91   *
     92   * - `url` document location
     93   *
     94   * - `pageNumber` current page number
     95   *
     96   * - `totalPages` total pages in the document
     97   */
     98  headerTemplate?: string;
     99  /**
    100   * HTML template for the print footer. Has the same constraints and support
    101   * for special classes as {@link PDFOptions.headerTemplate}.
    102   */
    103  footerTemplate?: string;
    104  /**
    105   * Set to `true` to print background graphics.
    106   * @defaultValue `false`
    107   */
    108  printBackground?: boolean;
    109  /**
    110   * Whether to print in landscape orientation.
    111   * @defaultValue `false`
    112   */
    113  landscape?: boolean;
    114  /**
    115   * Paper ranges to print, e.g. `1-5, 8, 11-13`.
    116   * @defaultValue The empty string, which means all pages are printed.
    117   */
    118  pageRanges?: string;
    119  /**
    120   * @remarks
    121   * If set, this takes priority over the `width` and `height` options.
    122   * @defaultValue `letter`.
    123   */
    124  format?: PaperFormat;
    125  /**
    126   * Sets the width of paper. You can pass in a number or a string with a unit.
    127   */
    128  width?: string | number;
    129  /**
    130   * Sets the height of paper. You can pass in a number or a string with a unit.
    131   */
    132  height?: string | number;
    133  /**
    134   * Give any CSS `@page` size declared in the page priority over what is
    135   * declared in the `width` or `height` or `format` option.
    136   * @defaultValue `false`, which will scale the content to fit the paper size.
    137   */
    138  preferCSSPageSize?: boolean;
    139  /**
    140   * Set the PDF margins.
    141   * @defaultValue `undefined` no margins are set.
    142   */
    143  margin?: PDFMargin;
    144  /**
    145   * The path to save the file to.
    146   *
    147   * @remarks
    148   *
    149   * If the path is relative, it's resolved relative to the current working directory.
    150   *
    151   * @defaultValue `undefined`, which means the PDF will not be written to disk.
    152   */
    153  path?: string;
    154  /**
    155   * Hides default white background and allows generating pdfs with transparency.
    156   * @defaultValue `false`
    157   */
    158  omitBackground?: boolean;
    159  /**
    160   * Generate tagged (accessible) PDF.
    161   *
    162   * @defaultValue `true`
    163   * @experimental
    164   */
    165  tagged?: boolean;
    166  /**
    167   * Generate document outline.
    168   *
    169   * @defaultValue `false`
    170   * @experimental
    171   */
    172  outline?: boolean;
    173  /**
    174   * Timeout in milliseconds. Pass `0` to disable timeout.
    175   *
    176   * The default value can be changed by using {@link Page.setDefaultTimeout}
    177   *
    178   * @defaultValue `30_000`
    179   */
    180  timeout?: number;
    181  /**
    182   * If true, waits for `document.fonts.ready` to resolve. This might require
    183   * activating the page using {@link Page.bringToFront} if the page is in the
    184   * background.
    185   *
    186   * @defaultValue `true`
    187   */
    188  waitForFonts?: boolean;
    189 }
    190 
    191 /**
    192 * @internal
    193 */
    194 export interface PaperFormatDimensions {
    195  width: number;
    196  height: number;
    197 }
    198 
    199 /**
    200 * @internal
    201 */
    202 export interface ParsedPDFOptionsInterface {
    203  width: number;
    204  height: number;
    205  margin: {
    206    top: number;
    207    bottom: number;
    208    left: number;
    209    right: number;
    210  };
    211 }
    212 
    213 /**
    214 * @internal
    215 */
    216 export type ParsedPDFOptions = Required<
    217  Omit<PDFOptions, 'path' | 'format' | 'timeout'> & ParsedPDFOptionsInterface
    218 >;
    219 
    220 /**
    221 * @internal
    222 *
    223 * @remarks All A series paper format sizes in inches are calculated from centimeters
    224 * rounded mathematically to four decimal places.
    225 */
    226 export const paperFormats: Record<
    227  LowerCasePaperFormat,
    228  Record<'cm' | 'in', PaperFormatDimensions>
    229 > = {
    230  letter: {
    231    cm: {width: 21.59, height: 27.94},
    232    in: {width: 8.5, height: 11},
    233  },
    234  legal: {
    235    cm: {width: 21.59, height: 35.56},
    236    in: {width: 8.5, height: 14},
    237  },
    238  tabloid: {
    239    cm: {width: 27.94, height: 43.18},
    240    in: {width: 11, height: 17},
    241  },
    242  ledger: {
    243    cm: {width: 43.18, height: 27.94},
    244    in: {width: 17, height: 11},
    245  },
    246  a0: {
    247    cm: {width: 84.1, height: 118.9},
    248    in: {width: 33.1102, height: 46.811},
    249  },
    250  a1: {
    251    cm: {width: 59.4, height: 84.1},
    252    in: {width: 23.3858, height: 33.1102},
    253  },
    254  a2: {
    255    cm: {width: 42, height: 59.4},
    256    in: {width: 16.5354, height: 23.3858},
    257  },
    258  a3: {
    259    cm: {width: 29.7, height: 42},
    260    in: {width: 11.6929, height: 16.5354},
    261  },
    262  a4: {
    263    cm: {width: 21, height: 29.7},
    264    in: {width: 8.2677, height: 11.6929},
    265  },
    266  a5: {
    267    cm: {width: 14.8, height: 21},
    268    in: {width: 5.8268, height: 8.2677},
    269  },
    270  a6: {
    271    cm: {width: 10.5, height: 14.8},
    272    in: {width: 4.1339, height: 5.8268},
    273  },
    274 } as const;