{"id":13580268,"url":"https://github.com/toondaey/nestjs-pdf","last_synced_at":"2025-04-06T18:14:57.426Z","repository":{"id":41786488,"uuid":"260353458","full_name":"toondaey/nestjs-pdf","owner":"toondaey","description":"Nest js pdf generator","archived":false,"fork":false,"pushed_at":"2024-09-12T20:32:07.000Z","size":1055,"stargazers_count":83,"open_issues_count":29,"forks_count":37,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-10-12T05:07:38.160Z","etag":null,"topics":["html-pdf","nestjs","nestjs-backend","nestjs-pdf"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/toondaey.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-05-01T01:14:27.000Z","updated_at":"2024-09-19T10:02:57.000Z","dependencies_parsed_at":"2023-10-21T00:39:25.236Z","dependency_job_id":"302e17e1-3455-4f57-b58f-42ef2a16ae4e","html_url":"https://github.com/toondaey/nestjs-pdf","commit_stats":{"total_commits":149,"total_committers":7,"mean_commits":"21.285714285714285","dds":"0.49664429530201337","last_synced_commit":"bee54ce3864e406ad3a4ffcd16552dbff0c1cc57"},"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toondaey%2Fnestjs-pdf","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toondaey%2Fnestjs-pdf/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toondaey%2Fnestjs-pdf/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toondaey%2Fnestjs-pdf/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/toondaey","download_url":"https://codeload.github.com/toondaey/nestjs-pdf/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247526764,"owners_count":20953143,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["html-pdf","nestjs","nestjs-backend","nestjs-pdf"],"created_at":"2024-08-01T15:01:49.290Z","updated_at":"2025-04-06T18:14:57.405Z","avatar_url":"https://github.com/toondaey.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# Nestjs PDF Generator\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"http://nestjs.com/\" target=\"blank\"\u003e\u003cimg src=\"https://nestjs.com/img/logo-small.svg\" width=\"120\" alt=\"Nest Logo\" /\u003e\u003c/a\u003e\n  \u003ca href=\"\" target=\"blank\"\u003e\u003cimg src=\"https://raw.githubusercontent.com/toondaey/nestjs-pdf/master/pdf-icon.svg\" width=\"120\" alt=\"PDF Logo\" /\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp style='text-align:center;'\u003e\nA simple PDF generator module for \u003ca href=\"https://nestjs.com\"\u003enestjs\u003c/a\u003e framework.\n\u003c/p\u003e\n\n\u003cp align='center'\u003e\n    \u003ca href=\"https://www.npmjs.com/package/@t00nday/nestjs-pdf\" target='_blank'\u003e\u003cimg alt=\"npm\" src=\"https://img.shields.io/npm/dm/@t00nday/nestjs-pdf\" alt=\"NPM Downloads\"\u003e\u003c/a\u003e\n    \u003ca href=\"https://coveralls.io/github/toondaey/nestjs-pdf\" target=\"_blank\" rel=\"noopener noreferrer\"\u003e\u003cimg alt=\"Coveralls github\" src=\"https://img.shields.io/coveralls/github/toondaey/nestjs-pdf\"\u003e\u003c/a\u003e\n    \u003ca href=\"https://npmjs.com/@t00nday/nestjs-pdf\" target=\"_blank\" rel=\"noopener noreferrer\"\u003e\u003cimg alt=\"npm version\" src=\"https://img.shields.io/npm/v/@t00nday/nestjs-pdf\"\u003e\u003c/a\u003e\n    \u003ca href=\"https://npmjs.com/@t00nday/nestjs-pdf\" target=\"_blank\" rel=\"noopener noreferrer\"\u003e\u003cimg alt=\"LICENCE\" src=\"https://img.shields.io/npm/l/@t00nday/nestjs-pdf\"\u003e\u003c/a\u003e\n    \u003ca href=\"https://circleci.com/gh/toondaey/nestjs-pdf\" target=\"_blank\" rel=\"noopener noreferrer\"\u003e\u003cimg alt=\"CircleCI build\" src=\"https://img.shields.io/circleci/build/gh/toondaey/nestjs-pdf/master\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eTable of content\u003c/strong\u003e (click to expand)\u003c/summary\u003e\n\n\u003c!-- toc --\u003e\n\n-   [Installation](#installation)\n-   [Usage](#usage)\n-   [Configuration](#configuration)\n-   [Guide](#usage)\n-   [Changelog](#changelog)\n-   [Contributing](#contributing)\n    \u003c!-- tocstop --\u003e\n    \u003c/details\u003e\n\n## Installation\n\nInstallation is as simple as running:\n\n`npm install @t00nday/nestjs-pdf`\n\nor\n\n`yarn add @t00nday/nestjs-pdf`.\n\n## Usage\n\nA basic usage example:\n\n1. Register the module as a dependency in the module where pdf will be generated:\n\n`app.module.ts`\n\n```ts\nimport { Module } from '@nestjs/common';\nimport { PDFModule } from '@t00nday/nestjs-pdf';\n\n@Module({\n    imports: [\n        // ... other modules\n        PDFModule.register({\n            view: {\n                root: '/path/to/template',\n                engine: 'pug',\n            },\n        }),\n    ],\n})\nexport class AppModule {}\n```\n\nThe module could also be registered asynchronously using the `registerAsync` method.\n\nExamples below:\n\n-   Using factory provider approach\n\n```ts\nimport { Module } from '@nestjs/common';\nimport { PDFModule, PDFModuleOptions } from '@t00nday/nestjs-pdf';\n\n@Module({\n    imports: [\n        // ... other modules\n        PDFModule.registerAsync({\n            useFactory: (): PDFModuleOptions =\u003e ({\n                view: {\n                    root: '/path/to/template',\n                    engine: 'pug',\n                },\n            }),\n        }),\n    ],\n})\nexport class AppModule {}\n```\n\n-   Using class or existing provider approach:\n\n`./pdf-config.service.ts`\n\n```ts\nimport {\n    PDFModuleOptions,\n    PDFOptionsFactory,\n} from '@t00nday/nestjs-pdf';\nimport { Injectable } from '@nestjs/common';\n\n@Injectable()\nexport class PdfConfigService implements PDFOptionsFactory {\n    createPdfOptions(): PDFModuleOptions {\n        return {\n            view: {\n                root: 'path/to/template',\n                engine: 'pug',\n            },\n        };\n    }\n}\n```\n\nThe `PdfConfigService` **SHOULD** implement the `PDFOptionsFactory`, **MUST** declare the `createPdfOptions` method and **MUST** return `PDFModuleOptions` object.\n\n```ts\nimport { Module } from '@nestjs/common';\nimport { PDFModule } from '@t00nday/nestjs-pdf';\nimport { PdfConfigService } from './pdf-config.service';\n\n@Module({\n    imports: [\n        // ... other modules\n        PDFModule.registerAsync({\n            useClass: PdfConfigService,\n        }),\n    ],\n})\nexport class AppModule {}\n```\n\n2. Inject into service as a dependency:\n\n`app.service.ts`\n\n```ts\nimport { Injectable } from '@nestjs/common';\nimport { PDFService } from '@t00nday/nestjs-pdf';\n\n@Injectable()\nexport class AppService {\n    constructor(\n        // ...other dependencies...\n        private readonly pdfService: PDFService,\n    ) {}\n}\n```\n\nIn 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:\n\n```ts\n@Module({\n    imports: [\n        PDFModule.register({\n            isGlobal: true,\n            view: {\n                root: '/path/to/template',\n                engine: 'pug',\n            },\n        })\n        // or...\n        PDFModule.registerAsync({\n            isGlobal: true,\n            useFactory: (): PDFModuleOptions =\u003e ({\n                view: {\n                    root: '/path/to/template',\n                    engine: 'pug',\n                },\n            }),\n        }),\n    ]\n})\nexport class RootModule {}\n```\n\n## Configuration\n\n### Module options\n\nThis library uses the [html-pdf](https://github.com/marcbachmann/node-html-pdf) npm package by **marcbachmann** under the hood which in turn uses [phantomjs](https://github.com/ariya/phantomjs) by **ariya** for the html-to-pdf conversion, [consolidate](https://github.com/tj/consolidate.js) by **tj** as html engine parser allowing users to specify their desired engine, as well as [juice](https://github.com/Automattic/juice) by **Automattic** for inlining resources.\n\nThe configuration object received by the `register` method is as below:\n\n```ts\nexport interface PDFModuleRegisterOptions {\n    view: ViewOptions;\n    juice?: JuiceOptions;\n}\n```\n\nThe `ViewOptions` can be further broken down into:\n\n```ts\nexport interface ViewOptions {\n    root: string;\n    engine: engine;\n    extension?: string;\n    engineOptions?: ViewEngineOptions;\n}\n```\n\nwhere:\n\n-   `root` (required) is the location of the template(s). This **MUST** be a directory.\n-   `engine` (required) **MUST** be a string name of the engines supported by the `consolidate` engine parser listed [here](https://github.com/tj/consolidate.js#supported-template-engines).\n-   `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 [here](https://github.com/node-swig/swig-templates/tree/master/examples/basic)\n-   `engineOptions` (optional) is a JavaScript object representation of the configuration options of engine used.\n\nThe `JuiceOptions` is exactly the same as required in the `juice` package specifications [here](https://github.com/Automattic/juice#options).\n\n## Guide\n\nAfter 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:\n\n-   to a file on the host machine;\n-   as a stream (i.e. `Readable`); or\n-   as a `Buffer`.\n\n```ts\nimport { Injectable } from '@nestjs/common';\nimport { PDFService } from '@t00nday/nestjs-pdf';\n\n@Injectable()\nexport class YourService {\n    constructor(private readonly pdfService: PDFService);\n\n    generatePDFToFile(\n        template: string,\n        filename: string,\n        options?: PDFOptions,\n    ) {\n        this.pdfService.toFile(template, filename, options); // returns Observable\u003cFileInfo\u003e;\n    }\n\n    generatePDFToStream(template: string, options?: PDFOptions) {\n        this.pdfService.toStream(template, options); // returns Observable\u003cReadable\u003e;\n    }\n\n    generatePDFToBuffer(template: string, options?: PDFOptions) {\n        this.pdfService.toBuffer(template, options); // returns Observable\u003cBuffer\u003e;\n    }\n}\n```\n\n## Changelog\n\n### 3.0.5 / 2022-04-11\n\n-   fix(): fix for failing jest tests\n\n### 3.0.2 / 2022-04-10\n\n-   docs(): correct documentation\n\n### 3.0.1 / 2022-04-10\n\n-   feat(): support for nestjs ^6.0.0 || ^7.0.0 || ^8.0.0\n\n### 2.0.6 / 2020-11-23\n\n-   chore(): dependencies updates\n-   docs(): installation guide update\n\n### 2.0.5 / 2020-11-14\n\n-   chore(): dependencies updates\n\n### 2.0.4 / 2020-11-13\n\n-   docs(): correction of documentation\n\n### 2.0.0 / 2020-11-12\n\n-   Removes `pdf()` as the default service provided by the module.\n-   Provides an object of class `PDFService` as its default service.\n-   Removes registeration of module by name.\n-   PDFService provides three methods for either generating `toFile`, `toStream` or `toBuffer`.\n\n## Contributing\n\nContributions are welcome. However, please read the contribution's [guide](./CONTRIBUTING.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftoondaey%2Fnestjs-pdf","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftoondaey%2Fnestjs-pdf","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftoondaey%2Fnestjs-pdf/lists"}