In this tutorial, we'll get to know different ways of generating a PDF file from Swagger API documentation. To get familiar with Swagger, refer to our tutorial for setting up Swagger 2 with a Spring REST API.
2. Generate PDF with Maven Plugins
The first solution for generating a PDF file from Swagger API documentation is based on a set of Maven plugins. With this approach, we'll get the PDF file upon building a Java project.
The steps for producing the desired PDF file include applying several plugins in a specific order during a Maven build. The plugins should be configured to pick the resources and propagate the outputs from the previous phases as the inputs of the next phase. So, let's see how each of them works.
2.1. The swagger-maven-plugin Plugin
The first plugin we'll use is swagger-maven-plugin. This plugin produces the swagger.json file for our REST API:
<title>DEMO REST API</title>
<description>A simple DEMO project for REST API documentation</description>
We need to point to the locations of our API and define the phase in the build process during which the plugin produces the swagger.json file. Here, in the execution tag, we've specified that it should do this during the package phase.
2.2. The swagger2markup-maven-plugin Plugin
The second plugin we need is the swagger2markup-maven-plugin. It takes the swagger.json output of the previous plugin as its input to produce Asciidoc:
Here, we specify the inputDirectory and the outputDirectory tags. Again, we'll define package as the build phase for generating the Asciidoc for our REST API.
2.3. The asciidoctor-maven-plugin Plugin
The third and final plugin we'll use is the asciidoctor-maven-plugin. As the last of the three plugins, this one produces a PDF file from Asciidoc:
Basically, we provide the location where the Asciidoc was generated during the previous phase. Then, we define a location for it to generate the PDF documentation and specify the phase during which it should generate the PDF. Once again, we're using the package phase.
3. Generate PDF using SwDoc
Swagger to PDF is an online tool, available at swdoc.org, that generates the API documentation in a PDF file using the provided swagger.json specification. It relies on the Swagger2Markup converter and AsciiDoctor.
The principles are similar to those in the previous solution. First, Swagger2Markup converts swagger.json to AsciiDoc files. Then, Asciidoctor parses those files into a document model and converts them to the PDF file.
The solution is easy to use and is a good choice if we already have our Swagger 2 API document.
We can generate the PDF in two ways:
- providing a URL to our swagger.json file
- pasting the contents of our swagger.json file into the tool's text box
We'll use the PetStore API doc publicly available on Swagger.
For our purpose, we'll copy the JSON file and paste it into the text box:
Then, after we click the “Generate” button, we'll get the documentation in PDF format:
In this short tutorial, we've discussed two ways to generate PDF from Swagger API documentation.
The first approach relies on Maven plugins. We can use three plugins and generate the REST API documentation while building the application.
The second solution describes how to generate PDF documents using the SwDoc online tool. We can either generate the documentation from the link to our swagger.json or paste the JSON file contents into the tool.
As always, the code for these examples is available over on GitHub.
res – REST (eBook) (cat=REST)