繁体   English   中英

Web API Swagger 文档导出为 PDF

[英]Web API Swagger documentation export to PDF

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

我的问题是:是否有类似于 .NET 中的 Java Plugin 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索引页面的head部分。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM