Reputation: 1128
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
Upvotes: 13
Views: 6304
Reputation: 661
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.
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.
Upvotes: 8