Web API Swagger 文档导出为 PDF

Web API Swagger documentation export to PDF

根据文档 (http://swagger.io/open-source-integrations/) 有 Java 的插件 Export Swagger documentation to PDF,我只是看看文档,但我什么也看不到关于 .NET

我的问题是:.NET 中是否有类似于 Java 插件 swagger2markup、swagger2markup-gradle-plugin 或其他导出方式的东西来自 WEB API 的 PDF 文档?

谢谢

我在正在进行的项目中实施了同样的事情。我们可以下载所有API的PDF文档。我已经完成使用 Rapi-Pdf 插件,通过它我们可以生成 swagger Json 的 PDF。它在 Dotnet 核心 2.1 中。

这是我下面的代码,我们需要在 startup.cs 文件中进行配置。

 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseStaticFiles();
        app.UseSwagger();
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint(Configuration["Swagger:swaggerurl"], Configuration["Swagger:swaggertitle"]);
            c.InjectJavascript("/swagger-ui/custom.js");
            c.InjectJavascript("/swagger-ui/rapipdf-min.js");
        });

        if (env.IsDevelopment())
        {
            _logger.LogInformation("Configuring for Development environment");
            app.UseDeveloperExceptionPage();
        }
        else
        {
            _logger.LogInformation("Configuring for Production environment");
            app.UseHsts();
        }
        app.UseAuthentication();
        app.UseMiddleware<ErrorHandlerMiddleware>();
        //removed middleware to handle token, now changed with policies and auth schemes
        //app.UseMiddleware<TokenManagerMiddleware>();
        app.UseHttpsRedirection();
        app.UseMvc();
    }

在上面的代码中,我们需要了解的是,我们必须注入两个 js 文件,一个是自定义 js,另一个是插件,因此它将添加到 Swagger UI 索引页面的头部。

这是我下面的代码 Custom.js

window.addEventListener("load", function () {
        customizeSwaggerUI();
    });
function customizeSwaggerUI() {
    setTimeout(function () {        
        var tag = '<rapi-pdf style="display:none" id="thedoc"> </rapi-pdf>';
        var btn = '<button id="btn" style="font-size:16px;padding: 6px 16px;text-align: center;white-space: nowrap;background-color: orangered;color: white;border: 0px solid #333;cursor: pointer;" type="button" onclick="downloadPDF()">Download API Document </button>';
        var oldhtml = document.getElementsByClassName('info')[0].innerHTML;
        document.getElementsByClassName('info')[0].innerHTML = oldhtml + '</br>' + tag + '</br>' + btn;
    }, 1200);
}
function downloadPDF() {
    var client = new XMLHttpRequest();
    client.overrideMimeType("application/json");
    client.open('GET', 'v1/swagger.json');
    var jsonAPI = "";
    client.onreadystatechange = function () {
        if (client.responseText != 'undefined' && client.responseText != "") {
            jsonAPI = client.responseText;
            if (jsonAPI != "") {
                let docEl = document.getElementById("thedoc");
                var key = jsonAPI.replace('\"Authorization: Bearer {token}\"', "");
                let objSpec = JSON.parse(key);
                docEl.generatePdf(objSpec);
            }
        }
    }
    client.send();   
}

就是这样。现在您可以下载 PDF 格式的 API 文档。

希望这会帮助其他人集成同类功能。