Security News
How Threat Actors are Abusing GitHub’s File Upload Feature to Host Malware
GitHub is susceptible to a CDN flaw that allows attackers to host malware on any public repository.
@t00nday/nestjs-pdf
Advanced tools
Readme
A simple PDF generator module for nestjs framework.
Installation is as simple as running:
npm install @t00nday/nestjs-pdf
or
yarn add @t00nday/nestjs-pdf
.
A basic usage example:
app.module.ts
import { Module } from '@nestjs/common';
import { PDFModule } from '@t00nday/nestjs-pdf';
@Module({
imports: [
// ... other modules
PDFModule.register({
view: {
root: '/path/to/template',
engine: 'pug',
},
}),
],
})
export class AppModule {}
The module could also be registered asynchronously using the registerAsync
method.
Examples below:
import { Module } from '@nestjs/common';
import { PDFModule, PDFModuleOptions } from '@t00nday/nestjs-pdf';
@Module({
imports: [
// ... other modules
PDFModule.registerAsync({
useFactory: (): PDFModuleOptions => ({
view: {
root: '/path/to/template',
engine: 'pug',
},
}),
}),
],
})
export class AppModule {}
./pdf-config.service.ts
import {
PDFModuleOptions,
PDFOptionsFactory,
} from '@t00nday/nestjs-pdf';
import { Injectable } from '@nestjs/common';
@Injectable()
export class PdfConfigService implements PDFOptionsFactory {
createPdfOptions(): PDFModuleOptions {
return {
view: {
root: 'path/to/template',
engine: 'pug',
},
};
}
}
The PdfConfigService
SHOULD implement the PDFOptionsFactory
, MUST declare the createPdfOptions
method and MUST return PDFModuleOptions
object.
import { Module } from '@nestjs/common';
import { PDFModule } from '@t00nday/nestjs-pdf';
import { PdfConfigService } from './pdf-config.service';
@Module({
imports: [
// ... other modules
PDFModule.registerAsync({
useClass: PdfConfigService,
}),
],
})
export class AppModule {}
app.service.ts
import { Injectable } from '@nestjs/common';
import { PDFService } from '@t00nday/nestjs-pdf';
@Injectable()
export class AppService {
constructor(
// ...other dependencies...
private readonly pdfService: PDFService,
) {}
}
In addition to the above, in situations where all your pdf templates are grouped into a single directory but you expect pdf files to be generated in multiple contexts within your nestjs application, it is advisable to register the PDFModule once in the root module of your application and providing it globally. This can be done by setting isGlobal
to true either in the PDFModuleRegisterOptions
or PDFModuleRegisterAsyncOptions
as below:
@Module({
imports: [
PDFModule.register({
isGlobal: true,
view: {
root: '/path/to/template',
engine: 'pug',
},
})
// or...
PDFModule.registerAsync({
isGlobal: true,
useFactory: (): PDFModuleOptions => ({
view: {
root: '/path/to/template',
engine: 'pug',
},
}),
}),
]
})
export class RootModule {}
This library uses the html-pdf npm package by marcbachmann under the hood which in turn uses phantomjs by ariya for the html-to-pdf conversion, consolidate by tj as html engine parser allowing users to specify their desired engine, as well as juice by Automattic for inlining resources.
The configuration object received by the register
method is as below:
export interface PDFModuleRegisterOptions {
view: ViewOptions;
juice?: JuiceOptions;
}
The ViewOptions
can be further broken down into:
export interface ViewOptions {
root: string;
engine: engine;
extension?: string;
engineOptions?: ViewEngineOptions;
}
where:
root
(required) is the location of the template(s). This MUST be a directory.engine
(required) MUST be a string name of the engines supported by the consolidate
engine parser listed here.extension
(optional) SHOULD be provided where the file extension of the engine used is different from its name. e.g. a swig
template would use .html
as its file extension which is quite different from the engine name. Detailed example found hereengineOptions
(optional) is a JavaScript object representation of the configuration options of engine used.The JuiceOptions
is exactly the same as required in the juice
package specifications here.
After completing the configuration(s), you can go ahead and inject the pdf
service into your class. The service provides three (3) methods (samples below) which can be used to either generate PDF:
Readable
); orBuffer
.import { Injectable } from '@nestjs/common';
import { PDFService } from '@t00nday/nestjs-pdf';
@Injectable()
export class YourService {
constructor(private readonly pdfService: PDFService);
generatePDFToFile(
template: string,
filename: string,
options?: PDFOptions,
) {
this.pdfService.toFile(template, filename, options); // returns Observable<FileInfo>;
}
generatePDFToStream(template: string, options?: PDFOptions) {
this.pdfService.toStream(template, options); // returns Observable<Readable>;
}
generatePDFToBuffer(template: string, options?: PDFOptions) {
this.pdfService.toBuffer(template, options); // returns Observable<Buffer>;
}
}
pdf()
as the default service provided by the module.PDFService
as its default service.toFile
, toStream
or toBuffer
.Contributions are welcome. However, please read the contribution's guide.
FAQs
PDF generator for nestjs framework.
The npm package @t00nday/nestjs-pdf receives a total of 610 weekly downloads. As such, @t00nday/nestjs-pdf popularity was classified as not popular.
We found that @t00nday/nestjs-pdf demonstrated a not healthy version release cadence and project activity because the last version was released 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
GitHub is susceptible to a CDN flaw that allows attackers to host malware on any public repository.
Security News
At Node Congress, Socket CEO Feross Aboukhadijeh uncovers the darker aspects of open source, where applications that rely heavily on third-party dependencies can be exploited in supply chain attacks.
Research
Security News
The Socket Research team found this npm package includes code for collecting sensitive developer information, including your operating system username, Git username, and Git email.