Skip to content

nulix-dev/adonis-pdf

Repository files navigation

Create PDFs in AdonisJS apps

Latest Version MIT Licensed run-tests

Note

This package is a AdonisJS version of the Laravel package spatie/laravel-pdf.

This package provides a simple way to create PDFs in AdonisJS apps. Under the hood it uses Chromium to generate PDFs from EdgeJS views. You can use modern CSS features like grid and flexbox to create beautiful PDFs.

npm i @nulix/adonis-pdf

node ace @nulix/adonis-pdf

Here's a quick example:

import { Pdf, Format } from "@ioc:Adonis/Addons/Pdf"

Pdf.view('pdfs.invoice', { invoice })
    .format(Format.A4)
    .save('invoice.pdf')

This will render the Edge view pdfs.invoice with the given data and save it as a PDF file.

You can also return the PDF as a response from your controller:

import { Pdf, Format } from "@ioc:Adonis/Addons/Pdf"
import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'

import Invoice from "App/Models/Invoice"

class DownloadInvoiceController
{
    public function download({ response }: HttpContextContract)
    {
        const invoice = Invoice.first()

        return Pdf.view('pdfs.invoice', { invoice })
            .name('your-invoice.pdf')
            .format(Format.A4)
            .toResponse(response);
    }
}

You can use also test your PDFs:

First you have to register it as a plugin within the entry point file, i.e. (test/bootstrap.ts)

import { assertPdf } from '@nulix/adonis-pdf'

export const plugins: Required<Config>['plugins'] = [assert(), runFailedTests(), apiClient(), assertPdf()]

Then, you can use the new assert functions inside your tests:

import { test } from "@japa/core"

import { Pdf } from "@ioc:Adonis/Addons/Pdf"

test('can render an invoice', async ({ client, assert }) => {
  const pdf = Pdf.fake();

  const response = await client.get('/')

  response.assertStatus(200)
  assert.pdfViewIs(pdf, 'home')
})

Documentation

All documentation is available on our documentation site.

License

The MIT License (MIT). Please see License File for more information.