22

I have an OpenAPI 3.0 spec and I want to generate a PDF out of it so that it can be given to the end users.

Currently, tools like swagger-spec-to-pdf or swagger2markup only support Swagger 2.0 but not OpenAPI 3.0. Is it possible to generate a PDF from an OpenAPI 3.0 spec without converting it to Swagger 2.0?

Helen
  • 87,344
  • 17
  • 243
  • 314
Niranjan
  • 517
  • 2
  • 4
  • 21

6 Answers6

29

A possible solution is to convert your OpenAPI 3.0 definition to an HTML doc, then use a browser's "Save to PDF" feature to convert HTML to PDF.

Follow these steps:

  1. Go to https://editor.swagger.io.
  2. Paste your OpenAPI 3.0 YAML/JSON definition.
  3. Select Generate Client > html.
  4. Download & unzip the file.
  5. Open the index.html page in a browser, e.g. Chrome.
  6. Select File > Print, change the Destination to Save as PDF, and save the page.
Helen
  • 87,344
  • 17
  • 243
  • 314
  • 1
    Fyi, there's the 'openapi-generator-gradle-plugin' which makes steps 1 to 4 incl redundant. – Alex Sep 24 '20 at 14:53
  • There's a lot of generators in openapitools/openapi-generator-cli (docker image), but none of them are PDF. – Aaron Wright May 24 '22 at 20:16
10

I just found RapiPDF which is able to generate PDF from OpenAPI 3.0 definition.

But it still isn't an ideal tool I'm looking for. I found these limitations so far:

  • No CLI, runs only in browser. So I can't use it in an automate pipeline.
  • Callback is not supported
  • No example in generated document
aleung
  • 9,848
  • 3
  • 55
  • 69
4

You can use this site and post your OpenAPI 3.0 spec (in json) directly into it. It's the easiest way, I think and the generated PDF looks pretty.

Boommeister
  • 1,591
  • 2
  • 15
  • 54
3

ApiBake - OpenAPI to PDF: https://www.npmjs.com/package/apibake I'm the developer of the tool.

2023 UPDATE: The tool has been converted from Dart to Node.js, added yaml support and fixed a few issues.

The focus of the project is to generate visually simple, yet useful document, with bookmarks and type links for easy navigation. I.e. object definitions in the PDF are easy to copy and use in TypeScript/JavaScript code. Now working on custom styling of the output document. Feedback is welcome.

Illya S
  • 161
  • 1
  • 12
  • 1
    This looked useful for my usecase, but only supports JSON. Any plans to add YAML support? – tddmonkey May 17 '22 at 07:23
  • I converted an openapi yaml spec to json from swagger editor to try this tool but getting the error "invalid openapi json file". It'll be good to add an example on the homepage itself for more clarity on expected input. – Akshay May 18 '22 at 06:08
  • 1
    No source, just an executable download? No good. – Aaron Wright May 24 '22 at 19:46
  • Is it 3.x.x openapi json? The tool only supports 3.x.x version. Actually, you are right. Will open the sources. – Illya S May 25 '22 at 20:11
  • Source Code is available: https://github.com/curvednebula/docbaker-cli – Illya S May 28 '22 at 16:10
  • Showed "invalid openapi json file" for valid files and the output is visually quite poor unfortunately – zed Jun 16 '22 at 12:03
  • @zed this issue should be fixed in the latest update – Illya S Sep 02 '23 at 14:57
2

The following 2 packages helped me generate a PDF from OpenAPI json file:

  • org.openapitools:openapi-generator-gradle-plugin:5.0.0-beta2
  • org.asciidoctor:asciidoctor-gradle-jvm-pdf:3.2.0

Apply the relevant Plugin classes and the rest is pretty straight-forward task configuration. This is my groovy plugin but it shouldn't be difficult to find the corresponding gradle DSL extensions should you need to.

project.plugins.apply OpenApiGeneratorPlugin
GenerateTask adoc = project.tasks.withType(GenerateTask).iterator().next()
adoc.with {
    it.input = swagger.outputDir.path + '/' + swagger.outputFileName + '.json'
    it.generatorName.set 'asciidoc'
    it.outputDir.set swagger.outputDir.path

    // Leaving the below option empty can cause rendering issues
    it.configOptions.putAll([
        'infoUrl'  : 'https://example.com',
        'infoEmail': 'inbox@example.com',
    ])
}

project.plugins.apply AsciidoctorJPdfPlugin
project.tasks.withType(AsciidoctorPdfTask).iterator().next().with {
    it.sourceDir = adoc.outputDir
    it.outputDir = it.sourceDir
}

Let me know if there are questions about (or syntax errors in) this snippet.

Alex
  • 1,313
  • 14
  • 28
1

The easiest and nicest solution I found it is just using the usual https://editor.swagger.io/. Once you are happy with the generated outcome (on the right), just drag the central cursor on the left (to cover your source code) in order to visualise only the final documentation. Then if you use Chrome (similarly for Firefox), just (right button in the screen or from menu) choose "Print" and for "Destination" select "Save As PDF".

Randomize
  • 8,651
  • 18
  • 78
  • 133