Documents/Pdf: Add progress tracker
This commit is contained in:
@ -47,7 +47,7 @@ namespace Elwig.Documents {
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
public static async Task<Document> Merge(IEnumerable<Document> docs) {
|
||||
public static Document Merge(IEnumerable<Document> docs) {
|
||||
return new MergedDocument(docs);
|
||||
}
|
||||
|
||||
@ -75,17 +75,21 @@ namespace Elwig.Documents {
|
||||
return await Html.CompileRenderAsync(name, this); ;
|
||||
}
|
||||
|
||||
public async Task Generate() {
|
||||
public async Task Generate(IProgress<double>? progress = null) {
|
||||
progress?.Report(0.0);
|
||||
if (this is MergedDocument m) {
|
||||
var pdf = new TempFile("pdf");
|
||||
var tmpHtmls = new List<TempFile>();
|
||||
var n = 0;
|
||||
foreach (var doc in m.Documents) {
|
||||
var tmpHtml = new TempFile("html");
|
||||
await doc.Render();
|
||||
await File.WriteAllTextAsync(tmpHtml.FilePath, await doc.Render(), Utils.UTF8);
|
||||
tmpHtmls.Add(tmpHtml);
|
||||
n++;
|
||||
progress?.Report(50.0 * n / m.Documents.Count());
|
||||
}
|
||||
await Pdf.Convert(tmpHtmls.Select(f => f.FilePath), pdf.FilePath);
|
||||
progress?.Report(50.0);
|
||||
await Pdf.Convert(tmpHtmls.Select(f => f.FilePath), pdf.FilePath, new Progress<double>(v => progress?.Report(50.0 + v / 2)));
|
||||
foreach (var tmp in tmpHtmls) {
|
||||
tmp.Dispose();
|
||||
}
|
||||
@ -94,10 +98,12 @@ namespace Elwig.Documents {
|
||||
var pdf = new TempFile("pdf");
|
||||
using (var tmpHtml = new TempFile("html")) {
|
||||
await File.WriteAllTextAsync(tmpHtml.FilePath, await Render(), Utils.UTF8);
|
||||
progress?.Report(50.0);
|
||||
await Pdf.Convert(tmpHtml.FilePath, pdf.FilePath);
|
||||
}
|
||||
_pdfFile = pdf;
|
||||
}
|
||||
progress?.Report(100.0);
|
||||
}
|
||||
|
||||
public void SaveTo(string pdfPath) {
|
||||
|
Reference in New Issue
Block a user