adonis-pdf
Advanced tools
Comparing version 0.0.11 to 0.0.12
'use strict' | ||
/** | ||
* For more details on each configuration option see http://pdfmake.org/#/gettingstarted. | ||
* Some of the configuration options have a slightly different name than their pdfmake | ||
* counterparts but they should still be self-explanatory. | ||
*/ | ||
module.exports = { | ||
@@ -7,3 +13,3 @@ | ||
|-------------------------------------------------------------------------- | ||
| Font Descriptors | ||
| Custom Font descriptors | ||
|-------------------------------------------------------------------------- | ||
@@ -15,4 +21,60 @@ | | ||
*/ | ||
fontDescriptors: null | ||
fonts: null, | ||
/* | ||
|-------------------------------------------------------------------------- | ||
| Custom style dictionaries | ||
|-------------------------------------------------------------------------- | ||
| | ||
| If you reuse the same styles across your document(s) you can set them | ||
| here and they will be available for use within your content. | ||
| | ||
*/ | ||
styles: {}, | ||
/* | ||
|-------------------------------------------------------------------------- | ||
| Custom header | ||
|-------------------------------------------------------------------------- | ||
| | ||
| Set a header for each page. This value must be a String or a Function. | ||
| | ||
*/ | ||
header: null, | ||
/* | ||
|-------------------------------------------------------------------------- | ||
| Custom footer | ||
|-------------------------------------------------------------------------- | ||
| | ||
| Set a footer for each page. This value must be a String or a Function. | ||
| | ||
*/ | ||
footer: null, | ||
/* | ||
|-------------------------------------------------------------------------- | ||
| Custom background | ||
|-------------------------------------------------------------------------- | ||
| | ||
| Set a background for each page. This value must be a String or a | ||
| Function. | ||
| | ||
*/ | ||
background: null, | ||
/* | ||
|-------------------------------------------------------------------------- | ||
| Custom page configuration | ||
|-------------------------------------------------------------------------- | ||
| | ||
| Set up the page configuration for each page of your PDFs. | ||
| | ||
*/ | ||
page: { | ||
size: null, | ||
orientation: null, | ||
margins: null | ||
} | ||
} |
{ | ||
"name": "adonis-pdf", | ||
"version": "0.0.11", | ||
"version": "0.0.12", | ||
"description": "Create PDF files within Adonis using pdfmake", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "node test/pdf.spec.js" | ||
}, | ||
"repository": { | ||
@@ -29,8 +26,7 @@ "type": "git", | ||
"devDependencies": { | ||
"@adonisjs/ace": "^5.0.0", | ||
"@adonisjs/fold": "^4.0.5", | ||
"@adonisjs/sink": "^1.0.13", | ||
"base64-stream": "^0.1.3", | ||
"japa": "^1.0.6", | ||
"pify": "^3.0.0" | ||
"base64-stream": "^0.1.3" | ||
} | ||
} |
@@ -7,7 +7,6 @@ 'use strict' | ||
register () { | ||
_registerPDF () { | ||
this.app.singleton('PDF', (app) => { | ||
const Config = app.use('Adonis/Src/Config') | ||
const PDF = require('../src/PDF') | ||
return new PDF(Config) | ||
@@ -17,4 +16,13 @@ }) | ||
_registerCommands () { | ||
this.app.bind('Adonis/Commands/PDF:GetConfig', (app) => require('../commands/GetConfig')) | ||
} | ||
register () { | ||
this._registerPDF() | ||
this._registerCommands() | ||
} | ||
} | ||
module.exports = PDFProvider |
@@ -6,13 +6,24 @@ # adonis-pdf | ||
## Installation | ||
- Run `npm i adonis-pdf` or `yarn add adonis-pdf` | ||
- Create `config/pdf.js` and set any custom fonts you wish to use: | ||
```js | ||
module.exports = { | ||
fontDescriptors: { | ||
// my fonts here | ||
} | ||
} | ||
* Run `npm i adonis-pdf` or `yarn add adonis-pdf` | ||
* Add `'adonis-pdf/providers/PdfProvider'` to the `providers` array within `start/app.js` | ||
## Configuration | ||
Configuration is done through `config/pdf.js`. This file will not exist in your project by default so you may copy the [config file](config/pdf.js) from this package or run the following command to create it: | ||
```bash | ||
adonis pdf:getconfig | ||
``` | ||
- Add `'adonis-pdf/providers/PdfProvider'` to the `providers` array within `start/app.js` | ||
By default all configuration options are `null` (i.e. we use the pdfmake defaults) but feel free to override them as per the [pdfmake documentation](http://pdfmake.org/#/gettingstarted): | ||
| Option | Description | | ||
|:---|:---| | ||
| fonts | Customise the fonts used in your PDFs; if no fonts are specified this package will automatically load default Roboto fonts | | ||
| styles | Custom style dictionaries allowing you to maintain consistent styles across each PDF | | ||
| header | Custom header for each page within your PDFs | | ||
| footer | Custom footer for each page within your PDFs | | ||
| background | Custom background for each page within your PDFs | | ||
| page.size | Set the page size to be used | | ||
| page.orientation | Set the page orientation | | ||
| page.margins | Set up the page margins | | ||
## Usage | ||
@@ -55,2 +66,4 @@ Add `const PDF = use('PDF')` to whatever file you wish to use it, then call `PDF.create()`. This method accepts two parameters: | ||
} | ||
module.exports = MyController | ||
``` |
'use strict' | ||
const { join } = require('path') | ||
const stream = require('stream') | ||
const PdfPrinter = require('pdfmake/src/printer') | ||
const defaultConfig = require('../../config/pdf.js') | ||
@@ -12,45 +11,168 @@ | ||
constructor (Config) { | ||
this.options = Config.merge('pdf', defaultConfig) | ||
} | ||
_setupPrinter () { | ||
this.printer = new PdfPrinter( | ||
(this.options.fontDescriptors !== null && typeof this.options.fontDescriptors === 'object') ? this.options.fontDescriptors : { | ||
Roboto: { | ||
normal: '../../assets/fonts/Roboto-Regular.ttf', | ||
bold: '../../assets/fonts/Roboto-Medium.ttf', | ||
italics: '../../assets/fonts/Roboto-Italic.ttf', | ||
bolditalics: '../../assets/fonts/Roboto-MediumItalic.ttf' | ||
}, | ||
RobotoBold: { | ||
normal: '../../assets/fonts/Roboto-Bold.ttf', | ||
italics: '../../assets/fonts/Roboto-BoldItalic.ttf' | ||
}, | ||
RobotoBlack: { | ||
normal: '../../assets/fonts/Roboto-Black.ttf', | ||
italics: '../../assets/fonts/Roboto-BlackItalic.ttf' | ||
}, | ||
RobotoLight: { | ||
normal: '../../assets/fonts/Roboto-Light.ttf', | ||
italics: '../../assets/fonts/Roboto-LightItalic.ttf' | ||
}, | ||
RobotoThin: { | ||
normal: '../../assets/fonts/Roboto-Thin.ttf', | ||
italics: '../../assets/fonts/Roboto-ThinItalic.ttf', | ||
typeof this.options.fonts === 'object' | ||
? this.options.fonts | ||
: { | ||
Roboto: { | ||
normal: join(__dirname, '..', '..', 'assets/fonts/Roboto-Regular.ttf'), | ||
bold: join(__dirname, '..', '..', 'assets/fonts/Roboto-Medium.ttf'), | ||
italics: join(__dirname, '..', '..', 'assets/fonts/Roboto-Italic.ttf'), | ||
bolditalics: join(__dirname, '..', '..', 'assets/fonts/Roboto-MediumItalic.ttf') | ||
}, | ||
RobotoBold: { | ||
normal: join(__dirname, '..', '..', 'assets/fonts/Roboto-Bold.ttf'), | ||
italics: join(__dirname, '..', '..', 'assets/fonts/Roboto-BoldItalic.ttf') | ||
}, | ||
RobotoBlack: { | ||
normal: join(__dirname, '..', '..', 'assets/fonts/Roboto-Black.ttf'), | ||
italics: join(__dirname, '..', '..', 'assets/fonts/Roboto-BlackItalic.ttf') | ||
}, | ||
RobotoLight: { | ||
normal: join(__dirname, '..', '..', 'assets/fonts/Roboto-Light.ttf'), | ||
italics: join(__dirname, '..', '..', 'assets/fonts/Roboto-LightItalic.ttf') | ||
}, | ||
RobotoThin: { | ||
normal: join(__dirname, '..', '..', 'assets/fonts/Roboto-Thin.ttf'), | ||
italics: join(__dirname, '..', '..', 'assets/fonts/Roboto-ThinItalic.ttf'), | ||
} | ||
} | ||
) | ||
} | ||
_configureStyles () { | ||
if ( | ||
this.options.styles !== null && | ||
typeof this.options.styles === 'object' | ||
) { | ||
this.definition.styles = this.options.styles | ||
} | ||
} | ||
_configureHeader () { | ||
if ( | ||
this.options.header !== null && | ||
(typeof this.options.header === 'string' || typeof this.options.header === 'function') | ||
) { | ||
this.definition.header = this.options.header | ||
} | ||
} | ||
_configureFooter () { | ||
if ( | ||
this.options.footer !== null && | ||
(typeof this.options.footer === 'string' || typeof this.options.footer === 'function') | ||
) { | ||
this.definition.footer = this.options.footer | ||
} | ||
} | ||
_configureBackground () { | ||
if ( | ||
this.options.background !== null && | ||
(typeof this.options.background === 'string' || typeof this.options.background === 'function') | ||
) { | ||
this.definition.background = this.options.background | ||
} | ||
} | ||
_hasPageConfigurationOption (prop) { | ||
return (this.options.page !== null && typeof this.options.page === 'object') && | ||
this.options.page.hasOwnProperty(prop) | ||
} | ||
_configurePageSize () { | ||
if (this._hasPageConfigurationOption('size')) { | ||
if (this_.isOfType(this.options.page.size, 'string')) { | ||
this.definition.pageSize = this.options.page.size | ||
} | ||
if (this_.isOfType(this.options.page.size, 'object')) { | ||
if ( | ||
this.options.page.size.hasOwnProperty('width') && | ||
this.options.page.size.hasOwnProperty('height') | ||
) { | ||
this.definition.pageSize = this.options.page.size | ||
} else { | ||
throw new Error('page.size configuration must have "width" and "height" properties if passed as an Object') | ||
} | ||
} | ||
) | ||
} | ||
} | ||
create (definition, stream) { | ||
if (typeof definition === 'object') { | ||
this.document = this.printer.createPdfKitDocument(definition) | ||
this.document.pipe(stream) | ||
this.document.end() | ||
_configurePageOrientation () { | ||
if (this._hasPageConfigurationOption('orientation')) { | ||
if (this._isOfType(this.options.page.orientation, 'string')) { | ||
this.definition.pageOrientation = this.options.page.orientation | ||
} else { | ||
throw new Error('page.orientation configuration must be a String') | ||
} | ||
} | ||
} | ||
_configurePageMargins () { | ||
if (this._hasPageConfigurationOption('margins')) { | ||
if ( | ||
Array.isArray(this.options.page.margins) && | ||
(this.options.page.margins.length === 2 || this.options.page.margins.length === 4) | ||
) { | ||
this.definition.pageMargins = this.options.page.margins | ||
} else { | ||
throw new Error('page.margin configuration must be an Array of either [left, top, right, bottom] or [horizontal, vertical]') | ||
} | ||
} | ||
} | ||
_applyConfiguration () { | ||
this._configureStyles() | ||
this._configureHeader() | ||
this._configureFooter() | ||
this._configureBackground() | ||
} | ||
_finaliseDefinition (content) { | ||
return this.definition.content = content | ||
} | ||
_generatePDF () { | ||
return this.document = this.printer.createPdfKitDocument(this.definition) | ||
} | ||
_pipeTo (stream) { | ||
if (stream instanceof stream.Readable || stream instanceof stream.writeable) { | ||
return this.document.pipe(stream) | ||
} else { | ||
throw { status: 'error', message: 'PDF content must be an Object' } | ||
throw new Error('You may only pipe to a readable/writeable stream.') | ||
} | ||
} | ||
_end () { | ||
return this.document.end() | ||
} | ||
_isOfType (prop, type) { | ||
return typeof prop === type | ||
} | ||
create (content, stream) { | ||
try { | ||
if (Array.isArray(content)) { | ||
this._setupPrinter() | ||
this._applyConfiguration() | ||
this._finaliseDefinition(content) | ||
this._generatePDF() | ||
this._pipeTo(stream) | ||
this._end() | ||
} else { | ||
throw new Error('Your PDF content must be an Array.') | ||
} | ||
} catch (error) { | ||
throw { status: 'error', error: error } | ||
} | ||
} | ||
} | ||
module.exports = PDF |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
2145874
4
281
68
0
1