Includes all methods of a Fragment
Add a header to the document. Returns a Header Object.
Example:
const header = doc.header()
header.text('This is a header')
Add a footer to the document. Returns a Footer Object.
Example:
const footer = doc.footer()
footer.text('This is a footer')
Note: please note that merging PDFs is still considered incomplete and has a couple of outstanding issues.
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)
Note: please note that merging PDFs is still considered incomplete and has a couple of outstanding issues.
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)
Note: please note that merging PDFs is still considered incomplete and has a couple of outstanding issues.
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 - firstPageOnly (default: false) - whether to only apply the template to the first page
Example:
const src = fs.readFileSync('other.pdf')
const ext = new pdf.ExternalDocument(src)
doc.setTemplate(ext)
Document is a Readable
stream and can therefore piped into other streams, e.g.:
Example:
doc.pipe(fs.createWriteStream('output.pdf'))
Must be called to finish writing the PDF document.
Note: Make sure something is reading from the document, otherwise this will not finish.
Example:
await doc.end()
Can be used to render the document as a buffer. Returns a Promise
; the usage of callback
is optional.
Arguments:
- opts:
- end (default:
true
) - if set to false, the document will not automatically be ended whenasBuffer
is called (#118)
- end (default:
- callback - called once everything has been written to the buffer
Note: When using .asBuffer()
, do not call .end()
(neither before nor after asBuffer
). Though, if you are calling .asBuffer({end: false})
you have to call doc.end()
yourself once you are done.
Examples:
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' })
}
})
const buf = doc.asBuffer({end: false})
// create your document, once done:
doc.end()
const data = await buf;