Beispiel #1
0
        public static string GeneratePdf(string html, PdfSettings pdfSettings, IServiceProvider serviceProvider, string outputDir)
        {
            try
            {
                EnsureOutputDirExists(outputDir);
                var guid     = Guid.NewGuid();
                var fileName = $"{guid}.pdf";

                var pdfBytes = GeneratePdf(html, pdfSettings, serviceProvider);
                using (var stream = new FileStream(Path.Combine(outputDir, fileName), FileMode.Create))
                {
                    using (var writer = new BinaryWriter(stream))
                    {
                        writer.Write(pdfBytes);
                    }
                }
                return(fileName);
            }
            catch (PdfGenerationException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw new PdfGenerationException("Error while generating pdf", e);
            }
        }
Beispiel #2
0
 public static byte[] GeneratePdf(string html, PdfSettings pdfSettings, IServiceProvider serviceProvider)
 {
     try
     {
         var pdfGeneratorWk = GetWkConverter(serviceProvider, pdfSettings);
         return(pdfGeneratorWk.GetPDF(html));
     }
     catch (Exception e)
     {
         throw new PdfGenerationException("Error while generating pdf", e);
     }
 }
Beispiel #3
0
        private static GeneratePdf GetWkConverter(IServiceProvider serviceProvider, PdfSettings pdfSettings)
        {
            const int dpi            = 96;
            var       pdfGeneratorWk = serviceProvider.GetService <IGeneratePdf>() as GeneratePdf;

            if (pdfGeneratorWk != null)
            {
                var opts = new ConvertOptions()
                {
                    PageMargins = new Margins()
                    {
                        Bottom = (int)PixelsToMm(pdfSettings.MarginBottom, dpi),
                        Top    = (int)PixelsToMm(pdfSettings.MarginTop, dpi),
                        Left   = (int)PixelsToMm(pdfSettings.MarginLeft, dpi),
                        Right  = (int)PixelsToMm(pdfSettings.MarginRight, dpi)
                    },
                    PageSize = MapWkPdfPageSize(pdfSettings.PageSize)
                };
                pdfGeneratorWk.SetConvertOptions(opts);
            }
            return(pdfGeneratorWk);
        }