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.
Add it to a page
Section titled “Add it to a page”<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>npm install rapipdfimport 'rapipdf';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.
Choose what goes in the PDF
Section titled “Choose what goes in the PDF”<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-securityandinclude-api-details(the operations) are on by default;falseleaves one out.include-exampleadds request and response examples, andinclude-api-lista 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(objectortable),pdf-primary-colorandpdf-alternate-color. - The element itself:
button-label,button-bg,button-color,input-bg,input-color, andhide-input="true"for a button alone.
Every attribute, with its default, is in the API reference.
Make it from a script
Section titled “Make it from a script”const pdf = document.querySelector('rapi-pdf');
await pdf.generatePdf(); // open or download, as `output` saysawait pdf.generatePdf(specObject); // a spec object or spec text instead of spec-urlconst 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.
Labels in another language
Section titled “Labels in another language”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.