New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More

astro-pdf

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

astro-pdf

A simple Astro integration to generate PDFs from built pages


Version published
Weekly downloads
131
increased by111.29%
Maintainers
0
Weekly downloads
 
Created

astro-pdf

CI npm version

A simple Astro integration to generate PDFs from built pages. Generate PDF versions of pages in your Astro site, or pages on external sites. Note that the PDFs will only be generated during builds and not when running the dev server.

Quickstart

Install and add astro-pdf:

npx astro add astro-pdf

and follow the CLI prompts.

Or, manually install astro-pdf:

npm i -D astro-pdf

and add it to astro.config.mjs (see the example below).

See the Astro Integrations Guide for more details.

Example

// astro.config.mjs
import { defineConfig } from 'astro/config';
import pdf from 'astro-pdf'

// https://astro.build/config
export default defineConfig({
    integrations: [
        pdf({
            // specify base options as defaults for options pages dont return
            baseOptions: {
                path: '/pdf[pathname].pdf',
                waitUntil: 'networkidle2',
                ...
            },
            // pages will receive the pathname of each page being built
            pages: {
                '/some-page': '/pages/some.pdf', // output path
                '/other-page': true,
                'https://example.com': {
                    path: 'example.pdf',
                    light: true, // set system theme to light
                    waitUntil: 'networkidle0', // for puppeteer page loading
                    pdf: { // puppeteer PDFOptions
                        format: 'A4',
                        printBackground: true
                    }
                },
                ...,
                fallback: (pathname) => ... // receives pathnames not specified above
            }
        })
    ]
});

See PagesMap for a more detailed example on how pages are generated from the config.

Reference

pdf()

export default function pdf(options: Options): AstroIntegration {}
  • options: Options

    Configure astro-pdf and specify which pages to build to pdf.

Options

export interface Options
  • pages: PagesMap | PagesFunction

    Specifies which pages in the site to convert to PDF and the options for each page.

  • install: boolean | Partial<InstallOptions> (optional)

    Specifies whether to install a browser, or options to pass to Puppeteer install. By default, it will install the latest stable build of Chrome if install is truthy and the browser to install is not specified.

    If install is false or undefined, but no browser is found, it will automatically install a browser.

    See Configuring Puppeteer for more information.

  • launch: PuppeteerLaunchOptions (optional)

    Options to pass to Puppeteer launch for launching the browser.

  • baseOptions: Partial<PageOptions> (optional)

    Default options to use for each page. Overrides the default options of PageOptions.

PageOptions

export interface PageOptions

Specifies options for generating each PDF. All options are optional when specifying pages in a PagesMap or PagesFunction. See PagesEntry for more details.

  • path: string | ((url: URL) => string)

    Default: '[pathname].pdf'

    Specify the location where the PDF will be generated. This is treated like a href in the site, so absolute paths will be resolved relative to the root of the site. For example, /path/to/file.pdf and path/to/file.pdf are equivalent.

    If the path contains [pathname], it will be substituted for the pathname of the page generated e.g. /path/to/page will be substituted into [pathname].pdf to get /path/to/page.pdf. If there are any redirects, the pathname will be the final location that is used to generate the PDF.

    path can also be a function which receives the URL of the page used to generate the PDF and returns the path where the PDF will be generated.

    If there is already a file with the same name, a counter suffix will be added to prevent overwriting the file. For example: example.pdf then example-1.pdf then example-2.pdf.

  • screen: boolean

    Default: false

    Use the CSS screen media type instead of the default print. This is set before callback.

  • waitUntil: PuppeteerLifeCycleEvent | PuppeteerLifeCycleEvent[]

    Default: 'networkidle2'

    Used when Puppeteer is loading the page in Page.goto

  • pdf: Omit<PDFOptions, 'path'>

    Default: {}

    Options to be passed to Page.pdf to specify how the PDF is generated.

  • callback: (page: Page) => void | Promise<void> (optional)

    Receives a Puppeteer Page after the page has loaded. This callback is run before the PDF is generated.

PagesEntry

export type PagesEntry = Partial<PageOptions> | string | boolean | null | undefined | void

If PagesEntry is truthy, astro-pdf will try to resolve options and generate the page. Any missing options will inherit from the baseOptions if given, otherwise the default options of PageOptions will be used.

If PagesEntry is a string, it will be used as the path value. If it is true, then the page will be generated completely using the base or default options.

PagesFunction

export type PagesFunction = (pathname: string) => PageEntry

Will be called with pathnames of the pages generated by astro. The pathnames are normalised to have a leading slash. Whether the pathnames have a trailing slash will depend on build.format in Astro config.

Example:

{
    pages: (pathname) => {
        if (pathname === '/specific/page') {
            return {
                path: 'specific-page.pdf',
                light: true
            }
        }
        if (pathname.startsWith('/documents/')) {
            return pathname.replace(/^\/documents/, '/generated')
        }
    }
}

PagesMap

export type PagesMap = Record<string, PagesEntry> & {
    fallback?: PagesFunction
}

Specify locations. Locations should be an absolute pathname or a full url.

Optionally provide a fallback function which will be called with the pathnames of pages generated by astro which are not already in the map.

If the pathname is in the map, but the PagesEntry for that pathname is null or undefined, it will still be passed to the fallback function. Only routes generated by astro will be passed into fallback. If PagesEntry for a pathname is false, then the page is skipped.

If any error is encountered when loading the location, the page will be skipped.

Example:

{
    pages: {
        'https://example.com': 'example.pdf',
        '/specific/page': {
            path: 'specific-page.pdf',
            light: true
        },
        '/documents/dynamic': false, // will not be passed to fallback
        fallback: (pathname) => {
            if (pathname.startsWith('/documents/')) {
                return pathname.replace(/^\/documents/, '/generated')
            }
        }
    }
}

Given the following project structure:

pages/
├── documents/
│   ├── index.astro
│   ├── static1.astro
│   ├── static2.astro
│   └── dynamic.astro
├── specific/
│   └── page.astro
└── other/
    └── page.astro

The above config will generate:

example.pdf
specific-page.pdf
generated/
├── static1.pdf
└── static2.pdf

with the fallback function being called with:

/documents
/documents/static1
/documents/static2
/other/page

Configuring Puppeteer

astro-pdf relies on Puppeteer to generate PDFs. By default, installing astro-pdf will install puppeteer, which will automatically install a recent version of Chrome for Testing. To prevent this, add a Puppeteer Configuration File and set skipDownload to true. Then, you can set Options.install to specify a specific browser version to install.

If Puppeteer times out after calling Page.pdf on Windows, it may be due to sandbox errors.

To address this, you can run the following command in command prompt if you are using the default installation of Chrome.

icacls "%USERPROFILE%/.cache/puppeteer/chrome" /grant *S-1-15-2-1:(OI)(CI)(RX)

Or, if you have set Options.install, run:

icacls "<cacheDir>/chrome" /grant *S-1-15-2-1:(OI)(CI)(RX)

with the specified cacheDir (defaults to Astro's cacheDir of node_modules/.astro).

Refer to the Puppeteer Troubleshooting Guide if there are any other issues.

FAQs

Package last updated on 20 Oct 2024

Did you know?

Socket

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.

Install

Related posts