Skip to content

armgit / Filee

Public Imported from GitHub
0

Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.

Draw spreadsheet PDFs with rhwp's direct backend

KnifeLemoned84fd583b54
diff --git a/src/Filee.Engines/Hwp/Hwpx/SheetDocument.cs b/src/Filee.Engines/Hwp/Hwpx/SheetDocument.cs
index 8f437a7d..b5ed69e8 100644
--- a/src/Filee.Engines/Hwp/Hwpx/SheetDocument.cs
+++ b/src/Filee.Engines/Hwp/Hwpx/SheetDocument.cs
@@ -11,6 +11,10 @@ namespace Filee.Engines.Hwp.Hwpx;
internal static class SheetDocument
{
+ /// <summary>The formats DocumentReaders reads into a document through <see cref="Build"/>.</summary>
+ public static readonly IReadOnlySet<string> SourceFormats =
+ new HashSet<string>(["xlsx", "xlsm", "xltx", "csv", "tsv", "xls", "xlt", "ods"], StringComparer.OrdinalIgnoreCase);
+
/// <summary>Largest table written; bigger sheets are cut with a note (a PDF of 100 000 rows helps nobody).</summary>
private const int MaxRows = 5000;
private const int MaxColumns = 60;
diff --git a/src/Filee.Engines/Hwp/RhwpConverter.cs b/src/Filee.Engines/Hwp/RhwpConverter.cs
index 43c636ac..5cc9f8b9 100644
--- a/src/Filee.Engines/Hwp/RhwpConverter.cs
+++ b/src/Filee.Engines/Hwp/RhwpConverter.cs
@@ -10,8 +10,11 @@
// one exists, and is never refused for it.
using System.IO.Compression;
+using System.Xml;
using Filee.Core.Conversion;
+using Filee.Core.Formats;
using Filee.Engines.Ebooks;
+using Filee.Engines.Hwp.Hwpx;
using Filee.Engines.Infrastructure;
using Filee.Engines.Office;
@@ -23,13 +26,27 @@ public sealed class RhwpConverter : IConverter, ITextShapingCheck
// rhwp renders text slowly: about 1 s per KB of section XML (some 4 s per page of a novel) with rhwp 0.8.7 on an
// i7-1185G7, so a 640-page book takes some 48 minutes; export-hwpx and convert take seconds for the same book. The
// time limit grows with the document's text, and still stops a rhwp that hangs.
+ //
+ // Nearly all of that time goes into the PDF backend, not the layout: the default one draws each page as SVG with
+ // one text element per character and converts that to PDF, a few ms per character. Its "direct" backend paints the
+ // same layout straight into the PDF 30 to 100 times faster (a 65-page workbook of 180 000 characters: 24 minutes →
+ // 20-45 s), but refuses pages with what it can't paint (gradients, patterns, shadows, connectors) and takes neither
+ // the generic fallback families nor the equation font of RhwpInput.PdfOptions. Documents Filee writes from a
+ // spreadsheet (text in table cells, borders, fills; SheetDocument) use it, with the SVG backend when it fails.
private static readonly TimeSpan BaseTimeLimit = TimeSpan.FromMinutes(2);
private static readonly TimeSpan TimeLimitPerMegabyte = TimeSpan.FromMinutes(30);
private static readonly TimeSpan MaxTimeLimit = TimeSpan.FromMinutes(60);
- /// <summary>Usual rendering time per MB of section XML (drives the progress estimate of long documents).</summary>
+ /// <summary>Usual rendering time per MB of section XML, for an HWP (whose characters aren't counted).</summary>
private static readonly TimeSpan TypicalPerMegabyte = TimeSpan.FromMinutes(15);
+ /// <summary>
+ /// Usual rendering time per drawn character with the SVG and the direct backend (drives the progress estimate).
+ /// Characters, not XML, set the pace: a table cell takes some 600 bytes of XML for a few characters.
+ /// </summary>
+ private static readonly TimeSpan TypicalPerCharacter = TimeSpan.FromMilliseconds(3.5);
+ private static readonly TimeSpan TypicalPerCharacterDirect = TimeSpan.FromMilliseconds(0.2);
+
private static readonly TimeSpan TypicalDuration = TimeSpan.FromSeconds(1.5);
private string? _exe;
@@ -76,6 +93,20 @@ public sealed class RhwpConverter : IConverter, ITextShapingCheck
if (step.To == "pdf" && DocumentFonts.Missing(step.OriginalSourcePath ?? step.InputPath) is [_, ..] missing)
step.MissingFonts?.Invoke(missing);
var input = step.To == "pdf" ? await PrepareAsync(exe, step, cancellationToken) : step.InputPath;
+ var text = TextSize(input);
+ var characters = step.To == "pdf" ? Characters(input) : null;
+ progress?.Report(0.1);
+
+ if (step.To == "pdf" && UsesDirectBackend(step.OriginalSourcePath ?? step.InputPath))
+ {
+ ProcessResult direct;
+ using (ProgressEstimate.Start(progress, Typical(characters, text, direct: true)))
+ direct = await ProcessRunner.RunAsync(exe, ["export-pdf", input, "-o", temp, "--backend", "direct"], TimeLimit(text), cancellationToken);
+ if (direct.ExitCode == 0 && Written(temp))
+ return Finish(step, temp, progress);
+ File.Delete(temp); // the SVG backend draws what the direct one refused
+ }
+
string[] arguments = step.To switch
{
"pdf" => ["export-pdf", input, "-o", temp, .. RhwpInput.PdfOptions()],
@@ -83,13 +114,10 @@ public sealed class RhwpConverter : IConverter, ITextShapingCheck
"hwp" => ["convert", step.InputPath, temp],
_ => throw new NotSupportedException(step.To),
};
-
- var text = TextSize(input);
- progress?.Report(0.1);
ProcessResult result;
- using (ProgressEstimate.Start(progress, step.To == "pdf" ? Typical(text) : TypicalDuration))
+ using (ProgressEstimate.Start(progress, step.To == "pdf" ? Typical(characters, text, direct: false) : TypicalDuration))
result = await ProcessRunner.RunAsync(exe, arguments, TimeLimit(text), cancellationToken);
- if (!File.Exists(temp) || new FileInfo(temp).Length == 0)
+ if (!Written(temp))
throw new InvalidOperationException($"rhwp failed (exit {result.ExitCode}). {result.StandardError.Trim()}");
}
catch (TimeoutException ex)
@@ -99,6 +127,11 @@ public sealed class RhwpConverter : IConverter, ITextShapingCheck
CalibreConverter.Locate() is null ? "error.rhwp_timeout" : "error.rhwp_timeout_calibre", ex.Message);
}
+ return Finish(step, temp, progress);
+ }
+
+ private static IReadOnlyList<string> Finish(ConversionStep step, string temp, IProgress<double>? progress)
+ {
var target = step.Output.Allocate(step.To);
if (target is null)
return [];
@@ -107,6 +140,15 @@ public sealed class RhwpConverter : IConverter, ITextShapingCheck
return [target];
}
+ private static bool Written(string path) => File.Exists(path) && new FileInfo(path).Length > 0;
+
+ /// <summary>
+ /// Whether the PDF of a document converted from <paramref name="sourcePath"/> is drawn with rhwp's direct backend:
+ /// one Filee wrote from a spreadsheet.
+ /// </summary>
+ internal static bool UsesDirectBackend(string sourcePath) =>
+ FormatRegistry.Detect(sourcePath) is { } format && SheetDocument.SourceFormats.Contains(format.Id);
+
/// <summary>
/// The document rhwp renders: a copy with the fixes of <see cref="RhwpInput"/> (fonts, shape sizes, equation
/// scripts, tables floating in cells) when it needs any, else the original. An HWP is read through an HWPX copy for
@@ -132,12 +174,55 @@ public sealed class RhwpConverter : IConverter, ITextShapingCheck
return limit < MaxTimeLimit ? limit : MaxTimeLimit;
}
- private static TimeSpan Typical(long textBytes)
+ /// <summary>The usual time of an <c>export-pdf</c> run: by its characters, else (an HWP) by its text size.</summary>
+ internal static TimeSpan Typical(long? characters, long textBytes, bool direct)
{
- var typical = TypicalPerMegabyte * (textBytes / 1048576.0);
+ var typical = characters is { } count
+ ? (direct ? TypicalPerCharacterDirect : TypicalPerCharacter) * count
+ : TypicalPerMegabyte * (textBytes / 1048576.0);
return typical > TypicalDuration ? typical : TypicalDuration;
}
+ /// <summary>
+ /// The characters rhwp draws in an HWPX (the text of its sections without spaces: one SVG element each), or null for
+ /// a file that isn't an HWPX package.
+ /// </summary>
+ internal static long? Characters(string path)
+ {
+ try
+ {
+ using var zip = ZipFile.OpenRead(path);
+ var sections = zip.Entries.Where(e => e.FullName.StartsWith("Contents/section", StringComparison.OrdinalIgnoreCase)
+ && e.FullName.EndsWith(".xml", StringComparison.OrdinalIgnoreCase)).ToList();
+ if (sections.Count == 0)
+ return null;
+ long count = 0;
+ var settings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Prohibit, XmlResolver = null };
+ foreach (var section in sections)
+ {
+ using var stream = section.Open();
+ using var reader = XmlReader.Create(stream, settings);
+ var depth = -1; // depth of the hp:t element being read, -1 outside one
+ while (reader.Read())
+ {
+ if (reader.NodeType == XmlNodeType.Element && depth < 0 && reader.LocalName == "t" && !reader.IsEmptyElement)
+ depth = reader.Depth;
+ else if (reader.NodeType == XmlNodeType.EndElement && reader.Depth == depth)
+ depth = -1;
+ else if (depth >= 0 && reader.NodeType is XmlNodeType.Text or XmlNodeType.CDATA)
+ foreach (var c in reader.Value)
+ if (!char.IsWhiteSpace(c))
+ count++;
+ }
+ }
+ return count;
+ }
+ catch (Exception ex) when (ex is InvalidDataException or IOException or UnauthorizedAccessException or XmlException)
+ {
+ return null; // not a ZIP (an HWP) or not readable XML: the text size stands in
+ }
+ }
+
/// <summary>
/// The size of a document's text, which rendering time follows: the unpacked section XML of an HWPX (pictures left
/// out), else three times the file (an HWP deflates its body text to about a third).
diff --git a/tests/Filee.Engines.Tests/DocumentEngineTests.cs b/tests/Filee.Engines.Tests/DocumentEngineTests.cs
index 68989bf0..fc95c2d6 100644
--- a/tests/Filee.Engines.Tests/DocumentEngineTests.cs
+++ b/tests/Filee.Engines.Tests/DocumentEngineTests.cs
@@ -5,6 +5,7 @@ using System.IO.Compression;
using Filee.Core.Conversion;
using Filee.Core.Presets;
using Filee.Engines.Hwp;
+using Filee.Engines.Hwp.Hwpx;
using Filee.Engines.Office;
namespace Filee.Engines.Tests;
@@ -143,6 +144,57 @@ public class DocumentEngineTests(EngineFixture fx) : IClassFixture<EngineFixture
Assert.Equal(TimeSpan.FromMinutes(60), RhwpConverter.TimeLimit(100 * 1024 * 1024)); // a hung rhwp is still stopped
}
+ [Fact]
+ public async Task Rhwp_progress_follows_the_characters_it_draws()
+ {
+ // A table cell takes hundreds of bytes of XML for a few characters, and rhwp's time goes by characters: a
+ // 65-page workbook (12.8 MB of XML, 180 000 characters) was estimated at over three hours.
+ var dir = fx.NewFolder();
+ var csv = Path.Combine(dir, "명단.csv");
+ await File.WriteAllTextAsync(csv, "이름,금액\n홍길동,\"1,000\"\nKim Lee, 7 \n", TestContext.Current.CancellationToken);
+ var hwpx = Assert.Single((await fx.ConvertAsync([csv], new Preset { TargetFormat = "hwpx" })).Outputs);
+
+ Assert.Equal(2 + 2 + 3 + 5 + 6 + 1, RhwpConverter.Characters(hwpx)); // spaces aren't drawn
+ var hwp = Path.Combine(dir, "deflated.hwp");
+ await File.WriteAllBytesAsync(hwp, new byte[1000], TestContext.Current.CancellationToken);
+ Assert.Null(RhwpConverter.Characters(hwp));
+
+ Assert.Equal(TimeSpan.FromMinutes(10.5), RhwpConverter.Typical(180_000, 12 * 1048576, direct: false));
+ Assert.Equal(TimeSpan.FromSeconds(36), RhwpConverter.Typical(180_000, 12 * 1048576, direct: true));
+ Assert.Equal(TimeSpan.FromMinutes(15), RhwpConverter.Typical(null, 1048576, direct: false)); // an HWP: by its size
+ Assert.Equal(TimeSpan.FromSeconds(1.5), RhwpConverter.Typical(10, 1000, direct: false));
+ }
+
+ [Theory]
+ [InlineData("매출.xlsx", true)]
+ [InlineData("data.CSV", true)]
+ [InlineData("old.xls", true)]
+ [InlineData("calc.ods", true)]
+ [InlineData("report.docx", false)]
+ [InlineData("form.hwpx", false)]
+ [InlineData("form.hwp", false)]
+ public void Rhwp_draws_documents_written_from_spreadsheets_with_its_direct_backend(string source, bool direct)
+ {
+ Assert.Equal(direct, RhwpConverter.UsesDirectBackend(source));
+ Assert.All(SheetDocument.SourceFormats, f => Assert.Contains(f, DocumentReaders.BuiltInFormats));
+ }
+
+ [Fact]
+ public async Task Csv_to_pdf_with_rhwp()
+ {
+ SkipUnlessAvailable("rhwp");
+ var dir = fx.NewFolder();
+ var csv = Path.Combine(dir, "재고.csv");
+ await File.WriteAllLinesAsync(csv, ["품명,수량,단가", .. Enumerable.Range(1, 120).Select(i => $"부품 {i},{i},\"{i * 1000:N0}\"")], TestContext.Current.CancellationToken);
+ Assert.Equal(["hwpx-writer", "rhwp"], fx.Catalog.CreatePlanner(["rhwp"]).Plan("csv", "pdf")!.Steps.Select(s => s.Converter.Id));
+
+ var job = await fx.ConvertAsync([csv], new Preset { TargetFormat = "pdf" });
+
+ Assert.True(job.State == JobState.Completed, string.Join("; ", job.Files.Select(f => $"{f.ErrorKey} {f.ErrorDetail}")));
+ if (OperatingSystem.IsWindows() || OperatingSystem.IsMacOS() || OperatingSystem.IsLinux())
+ Assert.True(PDFtoImage.Conversion.GetPageCount(await File.ReadAllBytesAsync(job.Outputs.Single(), TestContext.Current.CancellationToken)) >= 2);
+ }
+
[Fact]
public async Task LibreOffice_with_H2Orestart_reads_hwpx()
{