public async Task Can_Create_Simple_Pdf() { var bytes = await _pdfer.HtmlToPdf("This is <b>my</b> <u>HTML</u> test.", new PdfOptions()); //File.WriteAllBytes(@"C:\Users\Hannes\Downloads\" + Guid.NewGuid() + ".pdf", bytes); Assert.True(bytes.Length > 7_000 && bytes.Length < 14_000, $"Bytes were not in expected range ({bytes.Length})"); }
public async Task Can_Create_Simple_Pdf() { var bytes = await _pdfer.HtmlToPdf("This is <b>my</b> <u>HTML</u> test.", new PdfOptions()); await StoreForInspection(bytes); Assert.InRange(bytes.Length, 10_000, 28_000); }
public async Task <HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default(CancellationToken)) { var healthy = false; try { var bytes = await _pdfer.HtmlToPdf("This is a <b>Health</b> check", new PdfOptions()); if (bytes.Length > 100) { healthy = true; } } catch (Exception ex) { _logger.LogError(ex, "PDFer health check failed."); } if (healthy) { return(await Task.FromResult(HealthCheckResult.Healthy("PDFer is healthy."))); } return(await Task.FromResult(HealthCheckResult.Unhealthy("PDFer is not healthy."))); }
/// <inheritdoc /> public async Task <PdfRendererResult> Render(string templateDirectoryName, string pdfTemplateName, string filenameTemplateName, string headerTemplateName, string footerTemplateName, PdfOptions?options, object viewModel) { var stopwatch = new Stopwatch(); stopwatch.Start(); options ??= new PdfOptions(); // fall back to default options if (options.HeaderHtml == null && !string.IsNullOrWhiteSpace(headerTemplateName)) { try { options.HeaderHtml = (await _templater.Render(templateDirectoryName, headerTemplateName, viewModel)).Trim(); _logger.LogTrace($"Using header from template '{headerTemplateName}'."); } catch { // do nothing here. Happens usually when template does not exist, and we don't want anything to happen in this case } } if (options.FooterHtml == null && !string.IsNullOrWhiteSpace(footerTemplateName)) { try { options.FooterHtml = (await _templater.Render(templateDirectoryName, footerTemplateName, viewModel)).Trim(); _logger.LogTrace($"Using footer from template '{footerTemplateName}'."); } catch { // do nothing here. Happens usually when template does not exist, and we don't want anything to happen in this case } } var result = new PdfRendererResult { Html = (await _templater.Render(templateDirectoryName, pdfTemplateName, viewModel)).Trim(), Filename = (await _templater.Render(templateDirectoryName, filenameTemplateName, viewModel)).Trim(), }; stopwatch.Stop(); _logger.LogTrace($"Rendering of HTML templates took {stopwatch.ElapsedMilliseconds}ms."); stopwatch.Restart(); result.Bytes = await _pdfer.HtmlToPdf(result.Html, options); _logger.LogTrace($"Conversion to PDF took {stopwatch.ElapsedMilliseconds}ms, the result is {result.Bytes.Length:N0} bytes."); return(result); }