public void BuildPdf(SwaggerPdfDocumentModel swaggerDocumentModel) { Logger.Info("Building pdf document"); Logger.Info("Drawing welcome page"); DrawWelcomePage(swaggerDocumentModel); BeginNewPage(); Logger.Info("Drawing welcome page done."); Logger.Info("Drawing custom page"); DrawCustomPage(swaggerDocumentModel); Logger.Info("Drawing custom page done"); Logger.Info("Drawing authorization info page"); DrawAuthorizationInfoPage(swaggerDocumentModel); BeginNewPage(); Logger.Info("Drawing authorization info page done."); Logger.Info("Drawing drawing endpoint documentation"); DrawEndpointDocumentation(swaggerDocumentModel); Logger.Info("Drawing drawing endpoint documentation done."); Logger.Info("Rendering PDF document"); var fi = new FileInfo(swaggerDocumentModel.PdfDocumentPath); Logger.Info($"Saving PDF document to: {fi.FullName}"); SaveDocument(swaggerDocumentModel); Logger.Info("Done"); }
private void DrawCustomPage(SwaggerPdfDocumentModel swaggerDocumentModel) { var customPagePath = swaggerDocumentModel.CustomPageName; if (string.IsNullOrEmpty(customPagePath)) { Logger.Info("No custom page"); return; } FileInfo customPageFileInfo = new FileInfo(customPagePath); if (!customPageFileInfo.Exists) { throw new ArgumentException($"File {customPagePath} does not exist"); } if (customPageFileInfo.Extension != ".md") { throw new ArgumentException($"Only markdown (.md) files are supported. Current: {customPageFileInfo.Extension}"); } Logger.Info("Writing custom page"); using (var reader = new StreamReader(customPageFileInfo.FullName)) using (var writer = new StringWriter()) { CommonMark.CommonMarkConverter.Convert(reader, writer); WriteCustomPage(writer); BeginNewPage(); } }
private void DrawEndpointDocumentation(SwaggerPdfDocumentModel swaggerDocumentModel) { foreach (var docEntry in swaggerDocumentModel.DocumentationEntries) { DrawEndpointHeader(docEntry); DrawPathParameters(docEntry.PathParameters ?? new List <Parameter>()); DrawQueryParameters(docEntry.QueryParameter ?? new List <Parameter>()); DrawFormDataParameters(docEntry.FormDataParameters ?? new List <Parameter>()); DrawBodyParameters(docEntry.BodyParameters ?? new List <Parameter>()); DrawResponses(docEntry.Responses); BeginNewPage(); } }
protected override void DrawAuthorizationInfoPage(SwaggerPdfDocumentModel swaggerDocumentModel) { _document.H1().SetText("Authorization information"); _document.P(); foreach (var authorizationInfo in swaggerDocumentModel.AuthorizationInfo) { _document.H2().Bold().SetText($"Authorization option: {authorizationInfo.Key}"); _document.P(); var infoParagraph = _document.Div(); authorizationInfo.Value.WriteAuthorizationInfo(new HtmlAuthorizationWriter(infoParagraph)); _document.P(); } }
protected override void SaveDocument(SwaggerPdfDocumentModel swaggerDocumentModel) { var documentString = _document.GetDocumentString(); var pdfDocument = new PdfDocument(new PdfWriter(swaggerDocumentModel.PdfDocumentPath)); var properties = new ConverterProperties(); if (!string.IsNullOrEmpty(swaggerDocumentModel.WelcomePageImage)) { var imageFile = new FileInfo(swaggerDocumentModel.WelcomePageImage); properties.SetBaseUri(imageFile.DirectoryName + "\\"); } HtmlConverter.ConvertToDocument(documentString, pdfDocument, properties); pdfDocument.Close(); }
public SwaggerPdfDocumentModel PrepareSwaggerPdfModel(CommandLineInputParameters parameters) { Logger.Info("Started preparing swagger pdf model"); var swaggerJsonString = _swaggerJsonProvider.GetSwaggerJsonString(parameters.InputFileName); var swaggerJsonInfo = GetSwaggerInfoFromJsonString(swaggerJsonString); Logger.Info("Preparing PDF model"); var docModel = new SwaggerPdfDocumentModel(); docModel.PdfDocumentPath = parameters.OutputFileName; docModel.WelcomePageImage = parameters.WelcomePageImagePath; docModel.Title = parameters.Title ?? swaggerJsonInfo.SwaggerJsonInfo.Title; docModel.Version = parameters.Version ?? swaggerJsonInfo.SwaggerJsonInfo.Version; docModel.Author = parameters.Author ?? ""; docModel.DocumentDate = DateTime.Now; docModel.DocumentationEntries = PrepareDocumentationEntries(parameters.EndpointFilters, swaggerJsonInfo); docModel.AuthorizationInfo = PrepareAuthorizationInfos(swaggerJsonInfo); docModel.CustomPageName = parameters.CustomPageName; return(docModel); }
protected override void DrawWelcomePage(SwaggerPdfDocumentModel swaggerDocumentModel) { if (!string.IsNullOrEmpty(swaggerDocumentModel.WelcomePageImage)) { var imageFile = new FileInfo(swaggerDocumentModel.WelcomePageImage); if (imageFile.Exists) { var companyLogo = HtmlElement.Img().Src(imageFile.Name).SetStyle("margin-top", "150px"); _document.Div().AddChildElement(companyLogo).SetStyle("text-align", "center"); } } else { _document.Div().SetStyle("width", "100px").SetStyle("height", "300").SetStyle("display", "block"); } swaggerDocumentModel.Title.IfNotNull(x => _document.H1().HorizontalCenter().SetText(x)); swaggerDocumentModel.Author.IfNotNull(x => _document.H2().HorizontalCenter().SetText($"Author: {x}")); swaggerDocumentModel.Version.IfNotNull(x => _document.H3().HorizontalCenter().SetText($"Version: {x}")); swaggerDocumentModel.DocumentDate.ToShortDateString().IfNotNull(x => _document.H3().HorizontalCenter().SetText(x)); }
protected abstract void DrawAuthorizationInfoPage(SwaggerPdfDocumentModel swaggerDocumentModel);
protected abstract void SaveDocument(SwaggerPdfDocumentModel swaggerDocumentModel);
protected abstract void DrawWelcomePage(SwaggerPdfDocumentModel swaggerDocumentModel);