Skip to content

Commit

Permalink
HtmlWriter, PdfWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
phongnguyend committed Oct 10, 2024
1 parent 849fdb4 commit bde8670
Show file tree
Hide file tree
Showing 23 changed files with 205 additions and 140 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using ClassifiedAds.Domain.Entities;
using ClassifiedAds.CrossCuttingConcerns.Csv;
using ClassifiedAds.Domain.Entities;
using System.Collections.Generic;

namespace ClassifiedAds.Application.Products.DTOs;

public record ExportProductsToCsv
public record ExportProductsToCsv : ICsvRequest
{
public List<Product> Products { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using ClassifiedAds.CrossCuttingConcerns.Html;
using ClassifiedAds.Domain.Entities;
using System.Collections.Generic;

namespace ClassifiedAds.Application.Products.DTOs;

public record ExportProductsToHtml : IHtmlRequest
{
public List<Product> Products { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using ClassifiedAds.CrossCuttingConcerns.Pdf;
using ClassifiedAds.Domain.Entities;
using System.Collections.Generic;

namespace ClassifiedAds.Application.Products.DTOs;

public record ExportProductsToPdf : IPdfRequest
{
public List<Product> Products { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using ClassifiedAds.Domain.Entities;
using ClassifiedAds.CrossCuttingConcerns.Csv;
using ClassifiedAds.Domain.Entities;
using System.Collections.Generic;

namespace ClassifiedAds.Application.Products.DTOs;

public record ImportProductsFromCsv
public record ImportProductsFromCsv : ICsvResponse
{
public List<Product> Products { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
namespace ClassifiedAds.CrossCuttingConcerns.Csv;

public interface ICsvReader<T>
where T : ICsvResponse
{
Task<T> ReadAsync(Stream stream);
}

public interface ICsvResponse
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
namespace ClassifiedAds.CrossCuttingConcerns.Csv;

public interface ICsvWriter<T>
where T : ICsvRequest
{
Task WriteAsync(T data, Stream stream);
}

public interface ICsvRequest
{
}
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.

16 changes: 16 additions & 0 deletions src/Monolith/ClassifiedAds.CrossCuttingConcerns/Pdf/IPdfWriter.cs
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
@@ -0,0 +1,32 @@
using ClassifiedAds.Application.Products.DTOs;
using ClassifiedAds.CrossCuttingConcerns.Html;
using RazorLight;
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace ClassifiedAds.Infrastructure.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,22 @@
using ClassifiedAds.CrossCuttingConcerns.HtmlGenerator;
using ClassifiedAds.Infrastructure.HtmlGenerators;
using ClassifiedAds.Application.Products.DTOs;
using ClassifiedAds.CrossCuttingConcerns.Html;
using ClassifiedAds.Infrastructure.Html;
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 AddHtmlWriters(this IServiceCollection services)
{
var engine = new RazorLightEngineBuilder()
.UseFileSystemProject(Environment.CurrentDirectory)
.UseMemoryCachingProvider()
.Build();

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

return services;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using ClassifiedAds.Application.Products.DTOs;
using ClassifiedAds.CrossCuttingConcerns.Pdf;
using ClassifiedAds.Infrastructure.PdfConverters.DinkToPdf;
using DinkToPdf;
using DinkToPdf.Contracts;

namespace Microsoft.Extensions.DependencyInjection;

public static class DinkToPdfCollectionExtensions
{
public static IServiceCollection AddDinkToPdfWriters(this IServiceCollection services)
{
services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));
services.AddSingleton<IPdfWriter<ExportProductsToPdf>, ExportProductsToPdfHandler>();

return services;
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
using ClassifiedAds.CrossCuttingConcerns.PdfConverter;
using ClassifiedAds.Application.Products.DTOs;
using ClassifiedAds.CrossCuttingConcerns.Html;
using ClassifiedAds.CrossCuttingConcerns.Pdf;
using DinkToPdf;
using DinkToPdf.Contracts;
using System.IO;
using System.Threading.Tasks;

namespace ClassifiedAds.Infrastructure.PdfConverters.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,39 @@
using ClassifiedAds.Application.Products.DTOs;
using ClassifiedAds.CrossCuttingConcerns.Html;
using ClassifiedAds.CrossCuttingConcerns.Pdf;
using PuppeteerSharp;
using System.IO;
using System.Threading.Tasks;

namespace ClassifiedAds.Infrastructure.PdfConverters.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));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using ClassifiedAds.Application.Products.DTOs;
using ClassifiedAds.CrossCuttingConcerns.Pdf;
using ClassifiedAds.Infrastructure.PdfConverters.PuppeteerSharp;
using PuppeteerSharp;

namespace Microsoft.Extensions.DependencyInjection;

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

services.AddSingleton<IPdfWriter<ExportProductsToPdf>, ExportProductsToPdfHandler>();

return services;
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit bde8670

Please sign in to comment.