Skip to content
This repository has been archived by the owner on Sep 25, 2023. It is now read-only.

Latest commit

 

History

History
111 lines (72 loc) · 2.09 KB

document.md

File metadata and controls

111 lines (72 loc) · 2.09 KB

Document

Includes all methods of a Fragment

.header()

Add a header to the document. Returns a Header Object.

Example:

const header = doc.header()
header.text('This is a header')

.footer()

Add a footer to the document. Returns a Footer Object.

Example:

const footer = doc.footer()
footer.text('This is a footer')

.addPagesOff(external)

Add all pages of an external PDF into this document (aka merge an external document into this document).

Arguments:

  • external - a pdf.ExternalDocument object

Example:

const src = fs.readFileSync('other.pdf')
const ext = new pdf.ExternalDocument(src)
doc.addPagesOf(ext)

.addPageOff(page, external)

Add one specific page of an external PDF into this document (aka merge an external document into this document).

Arguments:

  • page - the number of the page that should be added
  • external - a pdf.ExternalDocument object

Example:

const src = fs.readFileSync('other.pdf')
const ext = new pdf.ExternalDocument(src)
doc.addPageOf(1, ext)
doc.addPageOf(3, ext)

.setTemplate(external)

Use an external document as a page template (i.e. external PDF will be used as a starting point / as a background for all pages).

Arguments:

  • external - a pdf.ExternalDocument object

Example:

const src = fs.readFileSync('other.pdf')
const ext = new pdf.ExternalDocument(src)
doc.setTemplate(ext)

.pipe(stream)

Document is a Readable stream and can therefore piped into other streams, e.g.:

doc.pipe(fs.createWriteStream('output.pdf'))

async .end()

Must be called to finish writing the PDF document.

await doc.end()

.asBuffer([callback])

Can be used to render the document as a buffer. Returns a Promise; the usage of callback is optional.

doc.asBuffer().then(data => fs.writeFileSync('test.pdf', data, { encoding: 'binary' }))
doc.asBuffer((err, data) => {
  if (err) {
    console.error(err)
  } else {
    fs.writeFileSync('test.pdf', data, { encoding: 'binary' })
  }
})