Skip to content

Getting started

Pardown converts a Markdown string into PDF bytes. This guide shows you how to install the library, create a PDF in Node.js, and resolve images relative to a Markdown file.

Prerequisites

You need a JavaScript project that uses ECMAScript modules. Pardown doesn't publish a CommonJS entry point, and the package metadata doesn't declare a minimum Node.js version.

Install the library

Install @pardown/core with your package manager. Its dependencies include the Markdown parser and PDF renderer, so you don't need to install them separately.

sh
npm install @pardown/core
sh
pnpm add @pardown/core
sh
yarn add @pardown/core

Convert Markdown

Pass a Markdown string to markdownToPdf. The function returns a promise that resolves to a Uint8Array containing the complete PDF.

ts
import { writeFile } from 'node:fs/promises';
import { markdownToPdf } from '@pardown/core';

const markdown = `
# Site inspection

The inspection found **no blocking issues**.
`;

const pdf = await markdownToPdf(markdown);
await writeFile('site-inspection.pdf', pdf);

Pardown returns bytes rather than writing a file, so you can also send the result in an HTTP response, store it in object storage, or create a browser Blob.

Resolve local images

Set cwd to the directory that contains your Markdown file. Pardown resolves relative image sources from this directory.

ts
import { readFile, writeFile } from 'node:fs/promises';
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { markdownToPdf } from '@pardown/core';

const markdownUrl = new URL('./reports/site-inspection.md', import.meta.url);
const markdownPath = fileURLToPath(markdownUrl);
const markdown = await readFile(markdownPath, 'utf8');

const pdf = await markdownToPdf(markdown, {
  cwd: dirname(markdownPath),
});

await writeFile(new URL('./reports/site-inspection.pdf', import.meta.url), pdf);

A source such as ![Crack in the wall](images/wall.jpg) now resolves from the reports/images directory. HTTP and HTTPS image URLs are fetched automatically.

NOTE

Local image loading uses Node.js file-system APIs. In a browser, provide a custom image loader as described in Load images in a browser.

Use the CLI instead

Install @pardown/cli globally when you want to convert Markdown without writing JavaScript.

sh
npm install --global @pardown/cli
pardown site-inspection.md

The command writes site-inspection.pdf beside the input file. Read the CLI reference for output paths, standard input, warnings, and exit statuses.

Next steps

Continue with the guide that matches your task:

Released under the MIT License.