Web API Swagger文档导出为PDF

时间:2016-10-26 09:17:06

标签: .net asp.net-web-api asp.net-web-api2 swagger swagger-ui

根据文件(http://swagger.io/open-source-integrations/) 有一个插件 Java 将Swagger文档导出为PDF ,我只看一下文档,但我看不到任何关于 .NET 的内容。

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

感谢

1 个答案:

答案 0 :(得分:5)

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

enter image description here

这是我下面的代码,我们需要在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文档。

希望这将帮助某人集成相同类型的功能。