从Swagger API文档生成PDF

Ama*_*med 78 pdf swagger-ui

我使用Swagger UI来显示我的REST Web服务并将其托管在服务器上.

但是,Swagger的这项服务只能在特定服务器上访问.如果我想离线工作,有人知道如何使用Swagger UI创建静态PDF并使用它吗?此外,PDF很容易与无权访问服务器的人共享.

非常感谢!

Osi*_*ify 31

方便的方法:使用浏览器打印/预览

  1. 隐藏编辑器窗格
  2. 打印预览(我使用的是firefox,其他人也很好)
  3. 更改其页面设置并打印为pdf

在此输入图像描述


Ird*_*dis 24

我创建了一个专门解决该问题的网站https://www.swdoc.org/。因此,它会swagger.json -> Asciidoc, Asciidoc -> pdf按照答案中的建议自动进行转换。这样做的好处是您无需执行安装程序。它接受 url 形式的规范文档或只是一个原始的 json。项目使用 C# 编写,页面为https://github.com/Irdis/SwDoc

编辑

如果您对 SwDoc 有任何问题,比如生成的 pdf 不完整,那么在此处验证您的 json 规范可能是个好主意:http : //editor.swagger.io/。

  • 谢谢,是的,它非常好,我将它用于我的工作项目。我正在考虑利用业余时间编写一些代码来支持openapi 3.0。 (3认同)
  • 这是一个很棒的工具!如果您遇到像我一样的问题(例如生成的 pdf 不完整),请将您的 json 粘贴到此处:http://editor.swagger.io/ 以自动验证,修复问题,然后您就可以返回 swdoc工具并这次正确生成它。 (3认同)
  • 一切荣耀归于其所依赖的工具的作者,ofc (2认同)
  • 您应该添加 3.0 规范的自动检测。 (2认同)

Her*_*ian 20

您可以修改REST项目,以便在构建项目时生成所需的静态文档(html,pdf等).

如果您有Java Maven项目,可以使用下面的pom片段.它使用一系列插件来生成pdf和html文档(项目的REST资源).

  1. rest-api - > swagger.json:swagger-maven-plugin
  2. swagger.json - > Asciidoc:swagger2markup-maven-plugin
  3. Asciidoc - > PDF:asciidoctor-maven-plugin

请注意,由于一个插件的输出成为下一个插件的输入,因此执行顺序很重要:

<plugin>
    <groupId>com.github.kongchen</groupId>
    <artifactId>swagger-maven-plugin</artifactId>
    <version>3.1.3</version>
    <configuration>
        <apiSources>
            <apiSource>
                <springmvc>false</springmvc>
                <locations>some.package</locations>
                <basePath>/api</basePath>
                <info>
                    <title>Put your REST service's name here</title>
                    <description>Add some description</description>
                    <version>v1</version>
                </info>
                <swaggerDirectory>${project.build.directory}/api</swaggerDirectory>
                <attachSwaggerArtifact>true</attachSwaggerArtifact>
            </apiSource>
        </apiSources>
    </configuration>
    <executions>
        <execution>
            <phase>${phase.generate-documentation}</phase>
            <!-- fx process-classes phase -->
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>io.github.robwin</groupId>
    <artifactId>swagger2markup-maven-plugin</artifactId>
    <version>0.9.3</version>
    <configuration>
        <inputDirectory>${project.build.directory}/api</inputDirectory>
        <outputDirectory>${generated.asciidoc.directory}</outputDirectory>
        <!-- specify location to place asciidoc files -->
        <markupLanguage>asciidoc</markupLanguage>
    </configuration>
    <executions>
        <execution>
            <phase>${phase.generate-documentation}</phase>
            <goals>
                <goal>process-swagger</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.asciidoctor</groupId>
    <artifactId>asciidoctor-maven-plugin</artifactId>
    <version>1.5.3</version>
    <dependencies>
        <dependency>
            <groupId>org.asciidoctor</groupId>
            <artifactId>asciidoctorj-pdf</artifactId>
            <version>1.5.0-alpha.11</version>
        </dependency>
        <dependency>
            <groupId>org.jruby</groupId>
            <artifactId>jruby-complete</artifactId>
            <version>1.7.21</version>
        </dependency>
    </dependencies>
    <configuration>
        <sourceDirectory>${asciidoctor.input.directory}</sourceDirectory>
        <!-- You will need to create an .adoc file. This is the input to this plugin -->
        <sourceDocumentName>swagger.adoc</sourceDocumentName>
        <attributes>
            <doctype>book</doctype>
            <toc>left</toc>
            <toclevels>2</toclevels>
            <generated>${generated.asciidoc.directory}</generated>
            <!-- this path is referenced in swagger.adoc file. The given file will simply 
                point to the previously create adoc files/assemble them. -->
        </attributes>
    </configuration>
    <executions>
        <execution>
            <id>asciidoc-to-html</id>
            <phase>${phase.generate-documentation}</phase>
            <goals>
                <goal>process-asciidoc</goal>
            </goals>
            <configuration>
                <backend>html5</backend>
                <outputDirectory>${generated.html.directory}</outputDirectory>
                <!-- specify location to place html file -->
            </configuration>
        </execution>
        <execution>
            <id>asciidoc-to-pdf</id>
            <phase>${phase.generate-documentation}</phase>
            <goals>
                <goal>process-asciidoc</goal>
            </goals>
            <configuration>
                <backend>pdf</backend>
                <outputDirectory>${generated.pdf.directory}</outputDirectory>
                <!-- specify location to place pdf file -->
            </configuration>
        </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

asciidoctor插件假定存在一个.adoc文件.您可以创建一个只收集swagger2markup插件创建的插件:

include::{generated}/overview.adoc[]
include::{generated}/paths.adoc[]
include::{generated}/definitions.adoc[]
Run Code Online (Sandbox Code Playgroud)

如果您希望生成的html文档成为war文件的一部分,则必须确保它存在于顶层 - WEB-INF文件夹中的静态文件将不会提供.你可以在maven-war-plugin中做到这一点:

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <configuration>
        <warSourceDirectory>WebContent</warSourceDirectory>
        <failOnMissingWebXml>false</failOnMissingWebXml>
        <webResources>
            <resource>
                <directory>${generated.html.directory}</directory>
            <!-- Add swagger.pdf to WAR file, so as to make it available as static content. -->
            </resource>
            <resource>
                <directory>${generated.pdf.directory}</directory>
            <!-- Add swagger.html to WAR file, so as to make it available as static content. -->
            </resource>
        </webResources>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

war插件适用于生成的文档 - 因此,您必须确保这些插件已在早期阶段执行.


Mri*_*moy 10

结帐https://mrin9.github.io/RapiPdf具有大量自定义和本地化功能的自定义元素。

免责声明:我是这个包的作者

  • 刚刚测试过,但在使用测试规范(petstore)单击“生成 PDF”后我没有得到响应? (3认同)
  • @Dabux 从未在 ubuntu 上进行过测试,但据我所知,有一种情况人们确实会面临与您所解释的相同的问题,那就是当您在浏览器上有任何活动的 as-blocker 或 popup blocker 时 (3认同)