0.4.0 • Published 2 months ago

@gboutte/nestjs-pdf v0.4.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

Installation

npm install @gboutte/nestjs-pdf

Configuration

@Module({
  imports: [
    HandlebarsModule.forRoot({
      pdfOptions: {},
      hbsOptions: {}
    )
  ],
  controllers: [],
  providers: [],
})
export class AppModule {}
Description
pdfOptionsThe pdf options can be found on: https://pptr.dev/api/puppeteer.pdfoptions
hbsOptionsThe handlebars can be found on @gboutte/nestjs-hbs
browserThe browser to use for the PDF generation. By default: Browser.CHROMIUM. Allowed values Browser.CHROMIUM, Browser.CHROMEHEADLESSSHELL, Browser.CHROME, Browser.FIREFOX, Browser.CHROMEDRIVER (Official documentation of Browser)
browserTagThe version of the browser to use for the PDF generation. By default: BrowserTag.STABLE. Allowed values BrowserTag.STABLE, BrowserTag.LATEST, BrowserTag.BETA, BrowserTag.DEV, BrowserTag.CANARY
browserInstallBaseUrlThe baseUrl used for the installation of the browser. This baseUrl is passed to the install method of @puppeteer/browsers
headlessDefine if you want to use chromium headless or not, or the new version. By default it's true. Allowed values: true,false,"new"
useLockedBrowserDefine if you want to use the locked version of the browser. By default it's false. Allowed values: true,false
extraPuppeteerArgsIt passes some extra arguments to Puppeteer's launch method. You can check the default args at bellow. Should be string[]

Default Puppeteer arguments

[
  "--autoplay-policy=user-gesture-required",
  "--disable-background-networking",
  "--disable-background-timer-throttling",
  "--disable-backgrounding-occluded-windows",
  "--disable-breakpad",
  "--disable-client-side-phishing-detection",
  "--disable-component-update",
  "--disable-default-apps",
  "--disable-dev-shm-usage",
  "--disable-domain-reliability",
  "--disable-extensions",
  "--disable-features=AudioServiceOutOfProcess",
  "--disable-hang-monitor",
  "--disable-ipc-flooding-protection",
  "--disable-notifications",
  "--disable-offer-store-unmasked-wallet-cards",
  "--disable-popup-blocking",
  "--disable-print-preview",
  "--disable-prompt-on-repost",
  "--disable-renderer-backgrounding",
  "--disable-setuid-sandbox",
  "--disable-speech-api",
  "--disable-sync",
  "--hide-scrollbars",
  "--ignore-gpu-blacklist",
  "--metrics-recording-only",
  "--mute-audio",
  "--no-default-browser-check",
  "--no-first-run",
  "--no-pings",
  "--password-store=basic",
  "--use-gl=swiftshader",
  "--use-mock-keychain",
  "--disabled-setupid-sandbox",
]

Usage

You can use the PdfService, there is currently two methods

DescriptionParameters
generatePdfFromHtml- html: The html to use to generate the pdf
generatePdfFromTemplateString- template: The template string to give to the handlebars service.- parameters: The parameters to use inside the template
generatePdfFromTemplateFile- file:The template file to give to the handlebars service. - parameters: The parameters to use inside the template

Here is an example

@Controller()
export class AppController {
  constructor(private readonly pdfService: PdfService) {}

  @Get()
  @Header('Cache-Control', 'none')
  @Header('Content-Type', 'application/pdf')
  async getPdf() {
    const product = new Product();
    product.amount = 53;

    const pdf = await this.pdfService.generatePdfFromTemplateString(
      '<h1>Title</h1> Product price: {{product.amount}}€',
      { product },
    );

    return pdf;
  }
}

Command

You can use the command nestjs-pdf install to install the browser you want to use. You can specify 3 parameters:

  • --browser: The browser to use for the PDF generation. Allowed values chromium, chrome, chrome-headless-shell, firefox, chromedriver(Official documentation of Browser)
  • --browser-tag: The version of the browser to use for the PDF generation. Allowed values stable, latest, beta, dev, canary
  • --lock: Define if you want to lock version of the browser. Allowed values true,false

BaseUrl

Sometime you need to define the base url for the installation of the browser. You can use the browserInstallBaseUrl parameter to define it. For example if you want to use CHROMEHEADLESSSHELL with STABLE, you may encounter an error like this:

Error: Download failed: server returned code 404. URL: https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/121.0.6167.184/mac-x64/chrome-headless-shell-mac-x64.zip

You can find the urls here, and as you can the url are starting by https://storage.googleapis.com/chrome-for-testing-public.

So you can fix the error by adding the browserInstallBaseUrl parameter like this:

PdfModule.forRoot({
  browserInstallBaseUrl:
    'https://storage.googleapis.com/chrome-for-testing-public',
}),

Known issues

  • Currently, the pdf only works with fastify adapter
0.4.0

2 months ago

0.3.2

3 months ago

0.0.21

9 months ago

0.2.6-beta.9

8 months ago

0.2.6-beta.5

8 months ago

0.2.6-beta.6

8 months ago

0.2.6-beta.7

8 months ago

0.2.6-beta.8

8 months ago

0.2.6-beta.11

8 months ago

0.2.6-beta.1

8 months ago

0.2.6-beta.10

8 months ago

0.2.6-beta.2

8 months ago

0.2.5-beta.0

8 months ago

0.2.6-beta.3

8 months ago

0.2.6-beta.12

8 months ago

0.2.6-beta.4

8 months ago

0.2.4-beta.0

8 months ago

0.2.6-beta.0

8 months ago

0.1.0

8 months ago

0.3.0

8 months ago

0.2.1

8 months ago

0.2.0

8 months ago

0.2.3

8 months ago

0.3.1

8 months ago

0.2.2

8 months ago

0.0.20

11 months ago

0.0.12

11 months ago

0.0.13

11 months ago

0.0.14

11 months ago

0.0.15

11 months ago

0.0.16

11 months ago

0.0.17

11 months ago

0.0.18

11 months ago

0.0.19

11 months ago

0.0.10

1 year ago

0.0.11

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

2 years ago