Partner – Expected Behavior – NPI (tag=pdf)
announcement - icon

Creating PDFs is actually surprisingly hard. When we first tried, none of the existing PDF libraries met our needs. So we made DocRaptor for ourselves and later launched it as one of the first HTML-to-PDF APIs.

We think DocRaptor is the fastest and most scalable way to make PDFs, especially high-quality or complex PDFs. And as developers ourselves, we love good documentation, no-account trial keys, and an easy setup process.

>> Try DocRaptor's HTML-to-PDF Java Client (No Signup Required)

Job – Java Automation Lead (cat=REST)
We’re looking for a Java/Spring Team Lead with Integration Experience (Remote, Part-Time): Read More

1. Overview

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, 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:

swagger to pdf 1


Then, after we click the “Generate” button, we'll get the documentation in PDF format:

swagger to pdf 2


4. Conclusion

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.

Course – LS (cat=REST)

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course :

res – REST (eBook) (cat=REST)
Comments are closed on this article!