Skip to content

RapiPdf

<rapi-pdf> is a spec URL field and a button that turns an OpenAPI 3.x or Swagger 2.0 spec into a PDF: a cover page, a table of contents, the security schemes, and every operation with its parameters, request bodies and responses. It runs in the browser; rapipdf/node and rapidoc pdf make the same PDF in Node and on the command line. Try it.

<script src="https://unpkg.com/rapipdf/dist/rapipdf-min.js"></script>
<rapi-pdf spec-url="https://petstore3.swagger.io/api/v3/openapi.json"></rapi-pdf>

rapipdf-min.js is a classic script with everything in it, so <script src> works with or without type="module". With a bundler, import 'rapipdf' loads dist/rapipdf.js: a few kilobytes that fetch the PDF generator (spec parser, pdfmake and its fonts, about two megabytes) the first time a PDF is asked for.

The reader presses the button, or Enter in the field. The PDF opens in a new tab; with output="download" it is saved instead, as file-name. If the browser blocks the new tab, the PDF is downloaded. While a PDF is being made the element has the generating attribute and its button is disabled.

<rapi-pdf
spec-url="/openapi.yaml"
pdf-title="Orders API"
pdf-footer-text="© Example Corp"
pdf-schema-style="table"
include-example="true"
include-api-list="true"
pdf-primary-color="#0b5394"
></rapi-pdf>
  • Sections: include-info (the cover page), include-toc, include-security and include-api-details (the operations) are on by default; false leaves one out. include-example adds request and response examples, and include-api-list a closing list of every operation.
  • Order: tags are sorted by name; pdf-sort-tags="false" keeps the spec’s order.
  • Look: pdf-title, pdf-footer-text, pdf-schema-style (object or table), pdf-primary-color and pdf-alternate-color.
  • The element itself: button-label, button-bg, button-color, input-bg, input-color, and hide-input="true" for a button alone.

Every attribute, with its default, is in the API reference.

const pdf = document.querySelector('rapi-pdf');
await pdf.generatePdf(); // open or download, as `output` says
await pdf.generatePdf(specObject); // a spec object or spec text instead of spec-url
const blob = await pdf.getPdfBlob(); // the PDF itself: show it, upload it
pdf.addEventListener('pdf-generated', (e) => console.log(e.detail.output));
pdf.addEventListener('pdf-error', (e) => console.error(e.detail.error));

getPdfBlob() opens and downloads nothing, for a page that shows the PDF itself or where new tabs are not allowed.

The words printed in the PDF (“REQUEST”, “RESPONSE”, “Parameters”, …) come from a table of labels. Replace any of them with JSON in a <template>, the element’s first child:

<rapi-pdf spec-url="/openapi.yaml">
<template>
{ "request": "ANFRAGE", "response": "ANTWORT", "parameters": "Parameter" }
</template>
</rapi-pdf>

The keys are those of DEFAULT_LOCALIZE, exported by rapipdf/node. For text the built-in Roboto font has no glyphs for, such as Chinese, Japanese or Korean, make the PDF in Node or with the CLI with your own font.