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.

Preserve PDF/A metadata in Calc exports

Perricheno29072d5d27fc
diff --git a/src/Filee.Engines/Office/LibreOfficeConverter.cs b/src/Filee.Engines/Office/LibreOfficeConverter.cs
index 3cda8010..05fe4b3f 100644
--- a/src/Filee.Engines/Office/LibreOfficeConverter.cs
+++ b/src/Filee.Engines/Office/LibreOfficeConverter.cs
@@ -123,25 +123,23 @@ public sealed class LibreOfficeConverter : IConverter
throw new InvalidOperationException(
$"LibreOffice produced no output (exit {result.ExitCode}). {result.StandardError.Trim()}");
- // Calc sometimes turns stale Excel print tiles into completely empty trailing PDF pages. Only remove pages
- // with no painting operation at all; pages containing text, borders, charts, pictures or annotations stay.
- if (step.To == "pdf" && FamilyOf(step.From, FormatRegistry.ExtensionOf(step.InputPath)) == "calc")
+ // Calc sometimes turns stale Excel print tiles into completely empty PDF pages anywhere in the output.
+ // Only remove pages with no painting operation at all; pages containing text, borders, charts, pictures
+ // or annotations stay. PDF/A is never rewritten: doing so would invalidate LibreOffice's XMP metadata.
+ try
{
- try
- {
- var removed = PdfBlankPages.Remove(produced);
- if (removed > 0)
- {
- _logger.LogInformation("{File}: removed {Count} empty spreadsheet PDF pages", step.InputPath, removed);
- step.Notice?.Invoke("notice.empty_spreadsheet_pages_removed");
- }
- }
- catch (Exception e) when (e is not OperationCanceledException)
+ var removed = RemoveEmptySpreadsheetPages(step, produced);
+ if (removed > 0)
{
- // Post-processing is an improvement, not a reason to discard LibreOffice's otherwise valid PDF.
- _logger.LogInformation("{File}: could not check empty spreadsheet PDF pages: {Message}", step.InputPath, e.Message);
+ _logger.LogInformation("{File}: removed {Count} empty spreadsheet PDF pages", step.InputPath, removed);
+ step.Notice?.Invoke("notice.empty_spreadsheet_pages_removed");
}
}
+ catch (Exception e) when (e is not OperationCanceledException)
+ {
+ // Post-processing is an improvement, not a reason to discard LibreOffice's otherwise valid PDF.
+ _logger.LogInformation("{File}: could not check empty spreadsheet PDF pages: {Message}", step.InputPath, e.Message);
+ }
var target = step.Output.Allocate(step.To);
if (target is null)
@@ -156,6 +154,15 @@ public sealed class LibreOfficeConverter : IConverter
}
}
+ /// <summary>
+ /// Removes empty Calc PDF pages when that post-processing is safe. PDF/A stays byte-for-byte as LibreOffice wrote
+ /// it because PDFsharp changes the Info dictionary without updating the archival XMP metadata.
+ /// </summary>
+ internal static int RemoveEmptySpreadsheetPages(ConversionStep step, string path) =>
+ step.To == "pdf" && !step.Preset.Document.PdfA
+ && FamilyOf(step.From, FormatRegistry.ExtensionOf(step.InputPath)) == "calc"
+ ? PdfBlankPages.Remove(path) : 0;
+
/// <summary>Runs LibreOffice into a fresh folder; returns the file it wrote (null if none) and how it ended.</summary>
private async Task<(string? Produced, ProcessResult Result)> ConvertOnceAsync(string soffice, int slot, string filter, string workDirectory,
string input, IProgress<double>? progress, CancellationToken cancellationToken)
diff --git a/tests/Filee.Engines.Tests/PdfBlankPagesTests.cs b/tests/Filee.Engines.Tests/PdfBlankPagesTests.cs
index 1efd0920..e06ffce9 100644
--- a/tests/Filee.Engines.Tests/PdfBlankPagesTests.cs
+++ b/tests/Filee.Engines.Tests/PdfBlankPagesTests.cs
@@ -1,3 +1,6 @@
+using Filee.Core.Conversion;
+using Filee.Core.Presets;
+using Filee.Engines.Office;
using Filee.Engines.Pdf;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
@@ -60,4 +63,40 @@ public class PdfBlankPagesTests
File.Delete(path);
}
}
+
+ [Fact]
+ public void Keeps_PdfA_spreadsheet_output_byte_for_byte()
+ {
+ var folder = Directory.CreateTempSubdirectory("filee-pdfa-pages-").FullName;
+ var path = Path.Combine(folder, "archive.pdf");
+ try
+ {
+ using (var document = new PdfDocument())
+ {
+ document.AddPage(); // would be removed from an ordinary Calc PDF
+ var painted = document.AddPage();
+ using var graphics = XGraphics.FromPdfPage(painted);
+ graphics.DrawLine(XPens.Black, 10, 10, 100, 100);
+ document.Save(path);
+ }
+ var before = File.ReadAllBytes(path);
+ var preset = new Preset { TargetFormat = "pdf", Document = { PdfA = true } };
+ var step = new ConversionStep(Path.Combine(folder, "sheet.xlsx"), "xlsx", "pdf", preset,
+ new FixedOutput(path), folder);
+
+ Assert.Equal(0, LibreOfficeConverter.RemoveEmptySpreadsheetPages(step, path));
+ Assert.Equal(before, File.ReadAllBytes(path));
+ using var result = PdfReader.Open(path, PdfDocumentOpenMode.Import);
+ Assert.Equal(2, result.PageCount);
+ }
+ finally
+ {
+ Directory.Delete(folder, recursive: true);
+ }
+ }
+
+ private sealed class FixedOutput(string path) : IOutputAllocator
+ {
+ public string? Allocate(string extension, string? suffix = null) => path;
+ }
}