Web API Swagger文档导出为PDF [英] Web API Swagger documentation export to PDF

查看:978
本文介绍了Web API Swagger文档导出为PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In according to the documentation (http://swagger.io/open-source-integrations/) there are plugins for Java to Export Swagger documentation to PDF, I just have a look the documentation but I can't see anything regarding .NET.

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

My question is: is there something similar to the Java Plugin swagger2markup, swagger2markup-gradle-plugin in .NET or another way to export the PDF Documentation from WEB API?

谢谢

推荐答案

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

I have implemented the same thing in my ongoing project. We can download PDF document of all the API's. I have done using Rapi-Pdf plugin through which we can generate PDF of swagger Json. Its in Dotnet core 2.1.

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

Here is my below code which we need to configure in startup.cs file.

 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索引页的开头.

In the above code what we need to understand is, we have to inject two js files one is custom js and another is plugin so it will added in head section of Swagger UI index page.

这是我下面的代码,位于Custom.js内

this is my below code which is inside 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文档.

That's it. Now you can download your API document in PDF format.

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

Hope this will help someone to integrate the same kind of functionality.

这篇关于Web API Swagger文档导出为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆