Skip to content

RapiPdf in Node

rapipdf/node makes the same PDF as <rapi-pdf>, from a file, a URL, spec text or a spec object, and returns it as a Buffer. Use it in a build step that publishes the PDF next to your docs; rapidoc pdf runs it from the command line.

npm install rapipdf
make-pdf.mjs
import { writeFile } from 'node:fs/promises';
import { generatePdf } from 'rapipdf/node';
const pdf = await generatePdf('openapi.yaml', {
pdfTitle: 'Orders API',
pdfSchemaStyle: 'table',
includeExample: true,
});
await writeFile('dist/reference.pdf', pdf);

$refs to other files are followed, and Swagger 2.0 is converted to OpenAPI 3. generatePdf rejects when the spec can’t be loaded.

Each option is an attribute of the element, in camelCase: pdf-title is pdfTitle, include-example is includeExample (a boolean here, not a string).

Option Default
pdfTitle "API Reference" Title on the cover page.
pdfFooterText Text at the bottom left of every page.
pdfSchemaStyle "object" Schemas as an indented object tree, or "table".
pdfPrimaryColor #b44646 Color of headings and operation titles.
pdfAlternateColor #005b96 Color of section labels and table headers.
pdfSortTags true Sort the tags by name.
includeInfo true The cover page with the API’s title, version, description and contact.
includeToc true The table of contents.
includeSecurity true The security schemes.
includeApiDetails true The operations, with parameters, request bodies and responses.
includeExample false Request and response examples.
includeApiList false A closing list of every operation, by tag.
localize Labels to replace, keyed as in DEFAULT_LOCALIZE.

The PDF uses Roboto and Roboto Mono. For text they have no glyphs for, such as Chinese, Japanese or Korean, pass TrueType or OpenType files:

const pdf = await generatePdf('openapi.yaml', {
font: './fonts/NotoSansJP-Regular.ttf',
monoFont: './fonts/NotoSansMonoCJKjp-Regular.otf',
localize: { request: 'リクエスト', response: 'レスポンス' },
});

generatePdf is three steps, each exported: loadSpecFile(spec) loads a spec and resolves every $ref; buildDocDefinition(spec, options) turns it into a pdfmake document definition, with no file or network access; renderPdf(docDefinition, { font, monoFont }) renders that to a PDF. Change the document definition between the last two steps to add a page or a logo. The types are in rapipdf/node’s node.d.ts.