Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web API Swagger documentation export to PDF

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.

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?

thanks

like image 900
natnael88 Avatar asked Oct 26 '16 09:10

natnael88


People also ask

How do I Export API document from Swagger?

You can use the "Export" button in the top right corner of the Swagger UI. This will give you the option to download your API document as a JSON or YAML file. You can use the "Generate Server" or "Generate Client" buttons to generate a server or client stub in your desired language.

Can we download Swagger documentation?

Swagger UI is available for download in the GitHub repository, or can be generated for any new or existing OpenAPI-defined API in the integrated SwaggerHub platform.

How do I print Swagger?

For me the easiest solution was to import swagger (v2) into Postman and then go to the web view. There you can choose "single column" view and use the browser to print to pdf.


1 Answers

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.

enter image description here

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();
    }

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.

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();   
}

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.

like image 194
Sumit Pathak Avatar answered Sep 19 '22 22:09

Sumit Pathak