Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
A Portable Document Format (PDF) generation library targeting both the server- and client-side.
A Portable Document Format (PDF) generation library targeting both the server- and client-side.
AFM fonts and vector graphics are currently not implemented. If you are missing a feature, feel free to ask or to submit a PR.
var Document = require('pdfjs')
Arguments:
Example:
var Document = require('pdfjs')
fs.readFile('OpenSans-Regular.ttf', function(err, b) {
if (err) throw err
var doc = new Document(new Document.Font(b))
})
Arguments:
Options:
Example:
doc.text('Lorem ipsum dolor sit amet ...', {
size: 12,
align: 'justify',
lineSpacing: 1.2
})
This text method can be used to render more advanced/complex text fragments. It allows to combine multiple text styles. Returns a Text function.
Arguments:
text
object described in TextExample:
doc.text(function(text) {
// this === text
this.opts.size = 12
this.opts.lineSpacing = 1.35
text('This is a header', { bold: true, size: 18 }).br()
text('Content can be')('bold', { bold: true })
text(',')('italic', { italic: true })
text('or')('both', { italic: true, bold: true })('.')
text('You`re also able to use')('light', { light: true })
text(', which could be')('italic', { italic: true, light: true })(', too.').br()
text('If you want, you can also')('mixup different fonts', { font: sourceCodeProp })
text('or different')('font', { size: 10 })('sizes', { size: 16 })('.')
})
Result:
This method can be used to render an image.
Arguments:
Options:
width
nor height
are provided, the image is rendered to fit into the current context (e.g. page size). When either width
or height
is provided, the images is scaled proportionally; if both are provided, the image is stretched.wrap
is set to true
, align
can be used to set the horizontal positon (left, right, or center)wrap
is set to false
, x
and y
can be used to explicitly set the position if the imageExample:
fs.readFile('./picture.jpg', function(err, b) {
if (err) throw err
doc.image(b, { align: 'center' })
})
This method can be used to define tables. Returns a Table object.
Arguments:
Options:
Example:
doc.table({ header: true, size: 11 }, function() {
this.tr({ bold: true, borderWidth: { bottom: 1.5 } }, function() {
this.td('#')
this.td('Unit')
this.td('Subject')
this.td('Price', { align: 'right' })
this.td('Total', { align: 'right' })
})
this.tr({ borderWidth: { horizontal: 0.1 } }, function() {
this.td('2')
this.td('pc.')
this.td(function() {
this.text('Article A', { size: 11, bold: true })
this.text('Lorem ipsum ...', { size: 9, align: 'justify' })
})
this.td('500.00€', { align: 'right' })
this.td('1,000.00€', { align: 'right' })
})
this.tr({ borderWidth: { horizontal: 0.1 } }, function() {
this.td('1')
this.td('pc.')
this.td(function() {
this.text('Article B', { size: 11, bold: true })
this.text('Cum id fugiunt ...', { size: 9, align: 'justify' })
})
this.td('250.00€', { align: 'right' })
this.td('250.00€', { align: 'right' })
})
this.tr({ bold: true, align: 'right' }, function() {
this.td('Total', { colspan: 4 })
this.td('1,250.00€')
})
})
Result:
Returns the document as data URL.
Example:
<iframe id="preview" width="100%" height="650" frameborder="0">
</iframe>
document.querySelector('#preview')
.setAttribute('src', doc.toDataURL())
Returns the document as plain text.
Example:
var fs = require('fs')
fs.writeFile(__dirname + '/test.pdf', doc.toString(), 'ascii')
This is a function itself and an alias for .text()
. It is used to define more complex/advanced text fragments. Returns itself.
Same as doc.text(text[, opts])
Line Break. Returns the Text function.
Print the page number the text fragment is rendered on. Returns the Text function.
Example:
doc.text(function(text) {
text('Page').pageNumber()
})
The image object should be used, when adding an image multiple times.
This method is used to create a new image from the given buffer.
Example:
var img = doc.createImage(buffer)
doc.image(img, { align: 'right' })
This table object is used to define its rows and its behaviour on page breaks.
This method is used to define a table row. Returns a Table Row object.
Arguments:
Options:
Same as .tr([opts,] definition), but only rendered if the table is broken among two pages. This row is then rendered directly before the page break.
table.beforeBreak(function() {
this.td('Subtotal:', { colspan: 4, align: 'right' })
this.td(function() {
this.text(function(text) {
this.opts.align = 'right'
this.opts.size = 11
text(function() {
if (!this.table) return ''
return items.subtotalUntil(this.table.row)
})
})
})
})
Same as .tr([opts,] definition), but only rendered if the table is broken among two pages. This row is then rendered directly after the header on the new page.
Example:
table.afterBreak(function() {
this.td('Carryover:', { colspan: 4, align: 'right' })
this.td(function() {
this.text(function(text) {
this.opts.align = 'right'
this.opts.size = 11
text(function() {
if (!this.table) return ''
return items.subtotalUntil(this.table.row)
})
})
})
})
Result:
This table row object is used to define its cells.
This method is used to define a cell of the row.
Arguments:
Options:
This method is used to define a cell of the row.
Arguments:
Options:
Create a new font from the given buffer.
Instead of just providing one font style (regular), you can also provide multiple styles.
Styles: normal, italic, bold, boldItalic, light, lightItalic
Example:
new pdfjs.Font({
regular: fs.readFileSync(__dirname + '/open-sans/OpenSans-Regular.ttf'),
bold: fs.readFileSync(__dirname + '/open-sans/OpenSans-Bold.ttf'),
boldItalic: fs.readFileSync(__dirname + '/open-sans/OpenSans-BoldItalic.ttf'),
light: fs.readFileSync(__dirname + '/open-sans/OpenSans-Italic.ttf'),
italic: fs.readFileSync(__dirname + '/open-sans/OpenSans-LightItalic.ttf'),
lightItalic: fs.readFileSync(__dirname + '/open-sans/OpenSans-LightItalic.ttf'),
})
(the sync API is just used for presentation purpose and should not be used in production)
In the browser, you can load fonts using AJAX requests, e.g.:
function load(path, callback) {
var request = new XMLHttpRequest()
request.open('GET', path, true)
request.responseType = 'arraybuffer'
if (request.overrideMimeType) {
request.overrideMimeType('text/plain; charset=x-user-defined')
} else {
request.setRequestHeader('Accept-Charset', 'x-user-defined')
}
request.onload = function() {
if (request.status === 200) {
callback(null, request.response)
} else {
callback(request.status)
}
}
request.send(null)
}
load('path/to/font.ttf', function(err, buffer) {
// ...
})
Copyright (c) 2013-2014 Markus Ast
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
A Portable Document Format (PDF) generation library targeting both the server- and client-side.
The npm package pdfjs receives a total of 33,186 weekly downloads. As such, pdfjs popularity was classified as popular.
We found that pdfjs demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.