Skip to content

Commit

Permalink
HtmlWriter, PdfWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
phongnguyend committed Oct 11, 2024
1 parent d82e5c3 commit 41e7c35
Show file tree
Hide file tree
Showing 18 changed files with 160 additions and 116 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.IO;
using System.Threading.Tasks;

namespace ClassifiedAds.CrossCuttingConcerns.Html;

public interface IHtmlWriter<T>
where T : IHtmlRequest
{
Task WriteAsync(T data, Stream stream);

Task<string> GetStringAsync(T data);
}

public interface IHtmlRequest
{
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.IO;
using System.Threading.Tasks;

namespace ClassifiedAds.CrossCuttingConcerns.Pdf;

public interface IPdfWriter<T>
where T : IPdfRequest
{
Task WriteAsync(T data, Stream stream);

Task<byte[]> GetBytesAsync(T data);
}

public interface IPdfRequest
{
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
using ClassifiedAds.CrossCuttingConcerns.HtmlGenerator;
using ClassifiedAds.Infrastructure.HtmlGenerators;
using RazorLight;
using RazorLight;
using System;

namespace Microsoft.Extensions.DependencyInjection;

public static class HtmlGeneratorCollectionExtensions
public static class HtmlCollectionExtensions
{
public static IServiceCollection AddHtmlGenerator(this IServiceCollection services)
public static IServiceCollection AddHtmlRazorLightEngine(this IServiceCollection services)
{
var engine = new RazorLightEngineBuilder()
.UseFileSystemProject(Environment.CurrentDirectory)
.UseMemoryCachingProvider()
.Build();

services.AddSingleton<IRazorLightEngine>(engine);
services.AddSingleton<IHtmlGenerator, HtmlGenerator>();

return services;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
using ClassifiedAds.CrossCuttingConcerns.PdfConverter;
using ClassifiedAds.Infrastructure.PdfConverters.DinkToPdf;
using DinkToPdf;
using DinkToPdf;
using DinkToPdf.Contracts;

namespace Microsoft.Extensions.DependencyInjection;

public static class DinkToPdfConverterCollectionExtensions
public static class DinkToPdfCollectionExtensions
{
public static IServiceCollection AddDinkToPdfConverter(this IServiceCollection services)
{
services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));
services.AddSingleton<IPdfConverter, DinkToPdfConverter>();

return services;
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
using ClassifiedAds.CrossCuttingConcerns.PdfConverter;
using ClassifiedAds.Infrastructure.PdfConverters.PuppeteerSharp;
using PuppeteerSharp;
using PuppeteerSharp;

namespace Microsoft.Extensions.DependencyInjection;

public static class PuppeteerSharpConverterCollectionExtensions
public static class PuppeteerSharpCollectionExtensions
{
public static IServiceCollection AddPuppeteerSharpPdfConverter(this IServiceCollection services)
{
var browserFetcher = new BrowserFetcher();
browserFetcher.DownloadAsync().GetAwaiter().GetResult();

services.AddSingleton<IPdfConverter, PuppeteerSharpConverter>();

return services;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using ClassifiedAds.Application;
using ClassifiedAds.Contracts.AuditLog.DTOs;
using ClassifiedAds.CrossCuttingConcerns.Csv;
using ClassifiedAds.CrossCuttingConcerns.HtmlGenerator;
using ClassifiedAds.CrossCuttingConcerns.PdfConverter;
using ClassifiedAds.CrossCuttingConcerns.Pdf;
using ClassifiedAds.Modules.Product.Authorization;
using ClassifiedAds.Modules.Product.Commands;
using ClassifiedAds.Modules.Product.Csv;
using ClassifiedAds.Modules.Product.Models;
using ClassifiedAds.Modules.Product.Pdf;
using ClassifiedAds.Modules.Product.Queries;
using ClassifiedAds.Modules.Product.RateLimiterPolicies;
using Microsoft.AspNetCore.Authorization;
Expand All @@ -33,22 +33,19 @@ public class ProductsController : ControllerBase
{
private readonly Dispatcher _dispatcher;
private readonly ILogger _logger;
private readonly IHtmlGenerator _htmlGenerator;
private readonly IPdfConverter _pdfConverter;
private readonly IPdfWriter<ExportProductsToPdf> _pdfWriter;
private readonly ICsvWriter<ExportProductsToCsv> _productCsvWriter;
private readonly ICsvReader<ImportProductsFromCsv> _productCsvReader;

public ProductsController(Dispatcher dispatcher,
ILogger<ProductsController> logger,
IHtmlGenerator htmlGenerator,
IPdfConverter pdfConverter,
IPdfWriter<ExportProductsToPdf> pdfWriter,
ICsvWriter<ExportProductsToCsv> productCsvWriter,
ICsvReader<ImportProductsFromCsv> productCsvReader)
{
_dispatcher = dispatcher;
_logger = logger;
_htmlGenerator = htmlGenerator;
_pdfConverter = pdfConverter;
_pdfWriter = pdfWriter;
_productCsvWriter = productCsvWriter;
_productCsvReader = productCsvReader;
}
Expand Down Expand Up @@ -158,13 +155,8 @@ public async Task<ActionResult<IEnumerable<AuditLogEntryDTO>>> GetAuditLogs(Guid
public async Task<IActionResult> ExportAsPdf()
{
var products = await _dispatcher.DispatchAsync(new GetProductsQuery());
var model = products.ToModels();

var template = Path.Combine(Environment.CurrentDirectory, $"Templates/ProductList.cshtml");
var html = await _htmlGenerator.GenerateAsync(template, model);
var pdf = await _pdfConverter.ConvertAsync(html);

return File(pdf, MediaTypeNames.Application.Octet, "Products.pdf");
var bytes = await _pdfWriter.GetBytesAsync(new ExportProductsToPdf { Products = products });
return File(bytes, MediaTypeNames.Application.Octet, "Products.pdf");
}

[HttpGet("exportascsv")]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using ClassifiedAds.CrossCuttingConcerns.Html;
using System.Collections.Generic;

namespace ClassifiedAds.Modules.Product.Html;

public record ExportProductsToHtml : IHtmlRequest
{
public List<Entities.Product> Products { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using ClassifiedAds.CrossCuttingConcerns.Html;
using RazorLight;
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace ClassifiedAds.Modules.Product.Html;

public class ExportProductsToHtmlHandler : IHtmlWriter<ExportProductsToHtml>
{
private readonly IRazorLightEngine _razorLightEngine;

public ExportProductsToHtmlHandler(IRazorLightEngine razorLightEngine)
{
_razorLightEngine = razorLightEngine;
}

public async Task WriteAsync(ExportProductsToHtml data, Stream stream)
{
using var sw = new StreamWriter(stream, Encoding.UTF8);
await sw.WriteAsync(await GetStringAsync(data));
}

public async Task<string> GetStringAsync(ExportProductsToHtml data)
{
var template = Path.Combine(Environment.CurrentDirectory, $"Templates/ProductList.cshtml");
string html = await _razorLightEngine.CompileRenderAsync(template, data.Products);
return html;
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
using ClassifiedAds.CrossCuttingConcerns.PdfConverter;
using ClassifiedAds.CrossCuttingConcerns.Html;
using ClassifiedAds.CrossCuttingConcerns.Pdf;
using ClassifiedAds.Modules.Product.Html;
using DinkToPdf;
using DinkToPdf.Contracts;
using System.IO;
using System.Threading.Tasks;

namespace ClassifiedAds.Infrastructure.PdfConverters.DinkToPdf;
namespace ClassifiedAds.Modules.Product.Pdf.DinkToPdf;

public class DinkToPdfConverter : IPdfConverter
public class ExportProductsToPdfHandler : IPdfWriter<ExportProductsToPdf>
{
private readonly IConverter _converter;
private readonly IHtmlWriter<ExportProductsToHtml> _htmlWriter;

public DinkToPdfConverter(IConverter converter)
public ExportProductsToPdfHandler(IConverter converter, IHtmlWriter<ExportProductsToHtml> htmlWriter)
{
_converter = converter;
_htmlWriter = htmlWriter;
}

public Stream Convert(string html, PdfOptions pdfOptions = null)
public async Task<byte[]> GetBytesAsync(ExportProductsToPdf data)
{
var doc = new HtmlToPdfDocument()
{
Expand All @@ -31,20 +35,20 @@ public Stream Convert(string html, PdfOptions pdfOptions = null)
new ObjectSettings()
{
PagesCount = true,
HtmlContent = html,
HtmlContent = await _htmlWriter.GetStringAsync(new ExportProductsToHtml {Products = data.Products}),
WebSettings = { DefaultEncoding = "utf-8", Background = true },
HeaderSettings = { FontSize = 9, Right = "Page [page] of [toPage]", Line = true, Spacing = 2.812 },
},
},
};

byte[] pdf = _converter.Convert(doc);

return new MemoryStream(pdf);
var bytes = _converter.Convert(doc);
return bytes;
}

public Task<Stream> ConvertAsync(string html, PdfOptions pdfOptions = null)
public async Task WriteAsync(ExportProductsToPdf data, Stream stream)
{
return Task.FromResult(Convert(html, pdfOptions));
using var sw = new BinaryWriter(stream);
sw.Write(await GetBytesAsync(data));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using ClassifiedAds.CrossCuttingConcerns.Pdf;
using System.Collections.Generic;

namespace ClassifiedAds.Modules.Product.Pdf;

public record ExportProductsToPdf : IPdfRequest
{
public List<Entities.Product> Products { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using ClassifiedAds.CrossCuttingConcerns.Html;
using ClassifiedAds.CrossCuttingConcerns.Pdf;
using ClassifiedAds.Modules.Product.Html;
using PuppeteerSharp;
using System.IO;
using System.Threading.Tasks;

namespace ClassifiedAds.Modules.Product.Pdf.PuppeteerSharp;

public class ExportProductsToPdfHandler : IPdfWriter<ExportProductsToPdf>
{
private readonly IHtmlWriter<ExportProductsToHtml> _htmlWriter;

public ExportProductsToPdfHandler(IHtmlWriter<ExportProductsToHtml> htmlWriter)
{
_htmlWriter = htmlWriter;
}

public async Task<byte[]> GetBytesAsync(ExportProductsToPdf data)
{
var html = await _htmlWriter.GetStringAsync(new ExportProductsToHtml { Products = data.Products });
await using var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true });
await using var page = await browser.NewPageAsync();
await page.SetContentAsync(html);

var bytes = await page.PdfDataAsync(new PdfOptions
{
PrintBackground = true,
});

return bytes;
}

public async Task WriteAsync(ExportProductsToPdf data, Stream stream)
{
using var sw = new BinaryWriter(stream);
sw.Write(await GetBytesAsync(data));
}
}
Loading

0 comments on commit 41e7c35

Please sign in to comment.