.net-corepdf-generationdinktopdf

PDF Implementation in dotnet Core - DinkTOPdf


Implementation of dinktopdf nugget Nugget link: https://www.nuget.org/packages/Haukcode.DinkToPdf/1.1.2?_src=template

Can anyone share the steps to implement for pdf file generation in .netCore? How to generate template before sending a file as stream to genrate a pdf?

How to inject dependency in Startup.cs?


Solution

  • Steps for implementation:

    public class PdfExportTemplateBuilder : BaseTemplate
        {
    public static string BuildTemplate(EmployeeDetails employessDetails, string id, string name)
            {
                var html = File.ReadAllText(Path.Combine(GetCurrentFolder(), @"EMPLOYEE\Template.Pdf.Employee.html")); //create the folder in current Assembly
                html = html.Replace(ID_PLACEHOLDER, string.IsNullOrWhiteSpace(id) ? "---" : id);
                html = html.Replace(NAME_PLACEHOLDER, string.IsNullOrWhiteSpace(name) ? "---" : name);
                html = html.Replace(REPORT_DATE, DateTime.UtcNow.ToString());
                html = html.Replace(REPORT_YEAR, DateTime.UtcNow.Year.ToString());
                var template = Handlebars.Compile(html);
                return template(employessDetails);
            }
    }
    
       public class BaseTemplate
        {
    
            protected const string ID_PLACEHOLDER= "{{ID}}";
            protected const string NAME_PLACEHOLDER= "{{NAME}}";
         }
    
     public async Task<Stream> DownloadPdfAsync(User loggedInUser, string name)
            {
                
                        var html = PdfExportTemplateBuilder .BuildTemplate(employeeDetails,id,name);
                        var conversionOptions = new ConversionOptions
                        {
                            Margins = new MarginSettings(10, 5, 10, 5),
                        };
    
                        var pdfStream = new MemoryStream(_htmlToPdfConverter.ConvertHtml(html, conversionOptions));
                        pdfStream.Position = 0;
    
                        return pdfStream; //returns file as stream 
    //Access as  return File(result, "application/pdf", "EmployeeDetails.pdf") from calling function.
                    }
               
            }
    
    public class HtmlToPdfConverter : IHtmlToPdfConverter
        {
            private readonly IConverter _converter;
    
            private const string FontName = "Arimo, Arial, Helvetica, sans-serif";
            private const string CenterFooterText = "Page [page] of [toPage]";
            private const string RightFooterTextFormat = "Report Date: {0}";
            private const int FontSize = 6;
            private const double Spacing = 7;
    
            public HtmlToPdfConverter(IConverter converter) => _converter = converter;
    
            public byte[] ConvertHtml(string html, ConversionOptions conversionOptions)
            {
                var doc = new HtmlToPdfDocument()
                {
                    GlobalSettings = {
                        ColorMode = ColorMode.Color,
                        Orientation = Orientation.Portrait,
                        PaperSize = PaperKind.A4,
                        Margins = conversionOptions.Margins,
                    },
                    Objects = {
                        new ObjectSettings() {
                            PagesCount = true,
                            HtmlContent = html,
                            WebSettings = { EnableIntelligentShrinking = false },
                            FooterSettings = {
                                FontSize = FontSize,
                                FontName = FontName,
                                Right = string.Format(RightFooterTextFormat, DateTime.UtcNow.ToString()),
                                Center = CenterFooterText,
                                Spacing = Spacing
                            }
                        }
                    }
                };
    
                return _converter.Convert(doc);
            }
        }
    
                services.AddSingleton<IHtmlToPdfConverter, HtmlToPdfConverter>();
                services.AddSingleton(typeof(IConverter), new 
                SynchronizedConverter(new PdfTools()));