puppeteer#PaperFormat TypeScript Examples

The following examples show how to use puppeteer#PaperFormat. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PdfOptions.ts    From pdf-generator-service with MIT License 6 votes vote down vote up
export function pdfOptionsFactory(options: Partial<PdfOptions>): PdfOptions {
  if (!options.content || !options.content.length) {
    throw new Error('content should not be empty')
  }

  // if we follow the puppeteer 9.0.0 types we have to introduce a breaking
  // change where all the page formats are in lower case format
  // to avoid us to introduce this breaking change me need to make the page format lowercase our selves.
  options.format = (options.format || 'A4').toLocaleLowerCase() as PaperFormat

  return defaults<Partial<PdfOptions>, PdfOptions>(options, {
    content: '',
    footer: '',
    header: '',
    orientation: 'portrait',
    tocContext: { _toc: [] },
    margin: defaults(options.margin, {
      top: '1.9cm',
      bottom: '1.9cm',
      left: '1.9cm',
      right: '1.9cm',
    }),
  })
}
Example #2
Source File: PdfOptions.spec.ts    From pdf-generator-service with MIT License 6 votes vote down vote up
describe('PdfOptions', () => {
  it('must trow error with message `content should not be empty` when content is empty', () => {
    throws(() => pdfOptionsFactory({ content: '' }), /content should not be empty/)
  })

  it('pdfOptionsFactory can be called only with content', () => {
    const options = pdfOptionsFactory({ content: '<div>Testing</div>' })
    expect(options.content).toBe('<div>Testing</div>')
    expect(options.orientation).toBe('portrait')
    expect(options.format).toBe('a4')
  })

  it('default parameters can be replaced', () => {
    const params = {
      content: '<h2>Hello</h2>',
      orientation: 'landscape' as PDFOrientation,
      format: 'ledger' as PaperFormat,
    }
    const options = pdfOptionsFactory(params)
    expect(options).toMatchObject(params)
  })
})