Skip to content

Commit fb5ac84

Browse files
authored
Merge pull request #2013 from lightscape-jm/fix/pdf-dates-calendar
fix(pdf): write PDF dates in the Gregorian calendar under every regional format
2 parents 3716238 + bf6e0d4 commit fb5ac84

12 files changed

Lines changed: 530 additions & 26 deletions

‎CHANGELOG.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ ko: HEIC 사진을 PDF로 바꾸거나 PDF 합치기로 묶으면 JPEG로 들어
8686
en: HEIC photos merged or converted into a PDF keep their small size
8787
ko: ExifTool이 있으면 카메라 RAW 사진을 JPG, PNG, WebP, AVIF, JPEG XL, HEIC로 바꿀 때 노출, 렌즈, 촬영 날짜, GPS 같은 카메라 정보(EXIF)가 남아요
8888
en: With ExifTool, camera RAW photos converted to JPG, PNG, WebP, AVIF, JPEG XL or HEIC keep their camera details (EXIF): exposure, lens, dates, GPS
89+
ko: 태국, 사우디아라비아, 이란 지역 형식에서도 PDF 날짜가 올해로 저장되고, PDF 도구가 날짜 때문에 실패하지 않아요
90+
en: PDFs saved under a Thai, Saudi or Iranian regional format get this year's date, and the PDF tools no longer fail there over dates
8991
ko: 전체 화면 게임이나 동영상, 프레젠테이션 중에는 업데이트 알림이 뜨지 않고, 다음에 Filee를 쓸 때 보여요
9092
en: The update notice doesn't appear over full-screen games, videos or presentations: it waits until you next use Filee
9193
ko: 업데이트 알림에 '나중에'(3일 뒤 다시 알림)와 '이 버전 건너뛰기'가 생겼고, 처음 실행할 때 알림을 끄는 방법도 알려 줘요
@@ -513,6 +515,13 @@ en: The progress ring's time at the cursor (5, 10, 15 seconds or Always) can be
513515
cancelled instead of failing the job, and an error during it (a full disk, a locked output file) now shows on each
514516
file instead of leaving them without a message.
515517

518+
- **PDF dates under other calendars** (by @lightscape-jm, #1972). Under a regional format whose calendar isn't
519+
Gregorian (Thailand, Saudi Arabia, Iran), every PDF Filee saved was dated with that calendar's year read as a
520+
Gregorian one (2569, 1448, 1405 in 2026), and in Saudi Arabia and Iran watermark, page numbers, password, rotate,
521+
crop, remove blank pages, compression and Remove metadata could fail with "Specified time is not supported in this
522+
calendar". Every PDF tool now writes the dates in the Gregorian calendar, as the PDF format wants. A PDF keeps the
523+
dates it already has.
524+
516525
- **Camera RAW photos keep their EXIF** (by @lightscape-jm, #1545). With ExifTool installed (the optional download,
517526
or one on the computer) and Keep metadata (EXIF) on, a RAW converted to JPG, PNG, WebP, AVIF, JPEG XL or HEIC, or
518527
upscaled, now carries the camera's EXIF as a JPEG source does: exposure, lens, dates, GPS location, serial numbers,

‎src/Filee.Engines/CompressionEstimator.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ internal static MagickImage ReadTile(string path, ImageOptions options, out doub
264264
using var document = PdfSharpConverter.OpenForChanges(entry.Path, cancellationToken);
265265
PdfCompressor.Compress(document, level, keepMetadata: true, cancellationToken);
266266
using var counter = new CountingStream();
267-
document.Save(counter, closeStream: false);
267+
PdfSharpConverter.SaveDated(document, counter, closeStream: false);
268268
compressed = counter.Length;
269269
entry.PdfCache[level] = compressed;
270270
}

‎src/Filee.Engines/Ebooks/ComicBook.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public static void ToPdf(IReadOnlyList<string> pages, string output, string work
168168
}
169169
if (document.PageCount == 0)
170170
throw new InvalidDataException("The comic contains no readable pictures.");
171-
document.Save(output);
171+
PdfSharpConverter.SaveDated(document, output);
172172
}
173173

174174
/// <summary>Writes pages into a CBZ (stored: pictures do not compress further), numbered in order.</summary>

‎src/Filee.Engines/Ocr/OcrTextLayer.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Globalization;
1111
using System.IO.Compression;
1212
using System.Text;
13+
using Filee.Engines.Pdf;
1314
using PdfSharp.Drawing;
1415
using PdfSharp.Pdf;
1516
using PdfSharp.Pdf.Advanced;
@@ -107,7 +108,7 @@ public void AddPage(string picture, double width, double height, IReadOnlyList<O
107108
page.Contents.AppendContent().CreateStream(Encoding.ASCII.GetBytes(text));
108109
}
109110

110-
public void Save(string path) => _document.Save(path);
111+
public void Save(string path) => PdfSharpConverter.SaveDated(_document, path);
111112

112113
public void Dispose() => _document.Dispose();
113114

‎src/Filee.Engines/Ocr/TesseractConverter.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ internal static async Task RecognizePdfAsync(ConversionStep step, string scratch
338338
recognized.Add(scan);
339339
result.AddPage(scan.Pages[0]);
340340
}
341-
result.Save(output);
341+
PdfSharpConverter.SaveDated(result, output);
342342
}
343343
finally
344344
{
@@ -504,7 +504,7 @@ internal static void PutTextOnPicture(string textPdf, string picture, double wid
504504
gfx.DrawImage(image, 0, 0, width, height);
505505
gfx.DrawImage(text, 0, 0, width, height);
506506
}
507-
document.Save(output);
507+
PdfSharpConverter.SaveDated(document, output);
508508
}
509509

510510
/// <summary>

‎src/Filee.Engines/Office/LibreOfficeConverter.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ internal static Func<bool> OutputComplete(string outDir)
256256
/// The first page of the preset's page range as a PDF of its own: Draw's SVG export writes every page of a PDF on
257257
/// top of each other, so it gets one page (like the other drawings, whose SVG holds the first page).
258258
/// </summary>
259-
private static string FirstPage(ConversionStep step)
259+
internal static string FirstPage(ConversionStep step)
260260
{
261261
using var source = PdfReader.Open(step.InputPath, PdfDocumentOpenMode.Import);
262262
var pages = PageRange.Parse(step.Preset.Pdf.PageRange, source.PageCount);
@@ -267,7 +267,7 @@ private static string FirstPage(ConversionStep step)
267267
var folder = Path.Combine(step.WorkDirectory, "lo-page-" + Guid.NewGuid().ToString("N")[..8]);
268268
Directory.CreateDirectory(folder);
269269
var path = Path.Combine(folder, FormatRegistry.NameWithoutExtension(step.InputPath) + ".pdf");
270-
single.Save(path);
270+
PdfSharpConverter.SaveDated(single, path);
271271
return path;
272272
}
273273

‎src/Filee.Engines/Pdf/PdfBlankPages.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static int Remove(string path)
4545
for (var i = blank.Count - 1; i >= 0; i--)
4646
document.Pages.RemoveAt(blank[i]);
4747
removed = blank.Count;
48-
document.Save(temp);
48+
PdfSharpConverter.SaveDated(document, temp);
4949
}
5050
File.Move(temp, fullPath, overwrite: true);
5151
return removed;

‎src/Filee.Engines/Pdf/PdfPageTools.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ internal static void Save(IReadOnlyList<PdfPageReference> pages, string outputPa
151151
structure.Separate(output.Pages, cancellationToken);
152152
structure.Write(cancellationToken);
153153
cancellationToken.ThrowIfCancellationRequested();
154-
output.Save(outputPath);
154+
PdfSharpConverter.SaveDated(output, outputPath);
155155
}
156156
finally
157157
{
@@ -206,7 +206,7 @@ private static void ModifyCopy(string inputPath, string outputPath, Action<PdfDo
206206
PdfSharpConverter.CopyWritable(inputPath, outputPath);
207207
using var document = PdfSharpConverter.OpenForChanges(outputPath);
208208
change(document);
209-
document.Save(outputPath);
209+
PdfSharpConverter.SaveDated(document, outputPath);
210210
}
211211

212212
private static int NormaliseRotation(int rotation) => ((rotation % 360) + 360) % 360;

‎src/Filee.Engines/Pdf/PdfPrintableAnnotations.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ internal static PdfAPreparation Prepare(string input, string folder, Cancellatio
9494
if (!document.Info.Elements.ContainsKey("/Creator"))
9595
document.Info.Elements["/Creator"] = new PdfString("");
9696
cancellationToken.ThrowIfCancellationRequested();
97-
document.Save(target);
97+
PdfSharpConverter.SaveDated(document, target);
9898
return new(target, hasFormFields, withoutAppearance);
9999
}
100100
catch (Exception ex) when (ex is not OperationCanceledException)

‎src/Filee.Engines/Pdf/PdfSharpConverter.cs‎

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ private static void GrayNoteColours(string path, CancellationToken ct)
8585
}
8686
}
8787
if (changed)
88-
document.Save(path);
88+
SaveDated(document, path);
8989
}
9090

9191
/// <summary>Reports a step's progress as the part <paramref name="start"/> … start + share of the whole.</summary>
@@ -241,18 +241,25 @@ private static IReadOnlyList<string> ImageToPdf(ConversionStep step, IProgress<d
241241
}
242242

243243
/// <summary>
244-
/// Saves <paramref name="document"/> with its dates written as the PDF format writes them. PDFsharp writes them in
245-
/// the thread's calendar: under a regional format with another calendar (Umm al-Qura, Thai Buddhist, Persian) the
246-
/// file got that calendar's year as a Gregorian one, and the next save of it threw where that year is outside the
247-
/// calendar (1448 under Saudi Arabia's).
244+
/// Saves <paramref name="document"/> with its dates in the Gregorian calendar, as the PDF format wants them.
245+
/// PDFsharp writes them in the thread's calendar: under a regional format with another calendar (Umm al-Qura, Thai
246+
/// Buddhist, Persian) the file got that calendar's year as a Gregorian one (1448 in Saudi Arabia in 2026), and a
247+
/// save threw where a date is outside that calendar: such a year from an earlier save, or the year 1 PDFsharp
248+
/// writes for a PDF without a creation date, under Saudi Arabia's and Iran's calendars.
248249
/// </summary>
249-
internal static void SaveDated(PdfDocument document, string path)
250+
internal static void SaveDated(PdfDocument document, string path) => InvariantDates(() => document.Save(path));
251+
252+
/// <inheritdoc cref="SaveDated(PdfDocument, string)"/>
253+
internal static void SaveDated(PdfDocument document, Stream stream, bool closeStream) =>
254+
InvariantDates(() => document.Save(stream, closeStream));
255+
256+
private static void InvariantDates(Action save)
250257
{
251258
var culture = CultureInfo.CurrentCulture;
252259
try
253260
{
254261
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
255-
document.Save(path);
262+
save();
256263
}
257264
finally
258265
{
@@ -283,13 +290,14 @@ private static IReadOnlyList<string> WithoutMetadata(ConversionStep step, IProgr
283290
var output = step.Output.Allocate("pdf");
284291
if (output is null)
285292
return [];
286-
document.Save(output);
293+
SaveDated(document, output);
287294
return [output];
288295
}
289296

290297
/// <summary>
291298
/// Drops the document information and every XMP packet (document, pages, pictures, forms) and the private data of
292-
/// the apps that made the file. PDFsharp writes a new XMP packet with only its own name and the save date.
299+
/// the apps that made the file. PDFsharp writes a new XMP packet with only its own name and, as there is no
300+
/// creation date left, the year 1 for its dates.
293301
/// </summary>
294302
internal static void StripMetadata(PdfDocument document)
295303
{
@@ -328,7 +336,7 @@ private static IReadOnlyList<string> SplitOrExtract(ConversionStep step, IProgre
328336
var path = step.Output.Allocate("pdf", PageGroups.Suffix(groups[i]));
329337
if (path is null)
330338
continue;
331-
part.Save(path);
339+
SaveDated(part, path);
332340
written.Add(path);
333341
progress?.Report((i + 1.0) / groups.Count);
334342
}
@@ -356,7 +364,7 @@ private static IReadOnlyList<string> SplitOrExtract(ConversionStep step, IProgre
356364
var output = step.Output.Allocate("pdf");
357365
if (output is null)
358366
return [];
359-
extract.Save(output);
367+
SaveDated(extract, output);
360368
return [output];
361369
}
362370

@@ -403,7 +411,7 @@ private static async Task<IReadOnlyList<string>> CompressAsync(ConversionStep st
403411
foreach (var index in groups[i])
404412
Rotate(part.AddPage(source.Pages[index]), options.Rotation);
405413
var path = Path.Combine(scratch, $"part-{i + 1}.pdf");
406-
part.Save(path);
414+
SaveDated(part, path);
407415
parts.Add((path, PageGroups.Suffix(groups[i])));
408416
}
409417
}
@@ -426,7 +434,7 @@ private static async Task<IReadOnlyList<string>> CompressAsync(ConversionStep st
426434
foreach (var index in pages)
427435
Rotate(extract.AddPage(source.Pages[index]), options.Rotation);
428436
var path = Path.Combine(scratch, "pages.pdf");
429-
extract.Save(path);
437+
SaveDated(extract, path);
430438
parts.Add((path, null));
431439
}
432440
}
@@ -461,7 +469,7 @@ private static async Task<bool> CompressFileAsync(string plain, string output, P
461469
{
462470
using var document = OpenForChanges(plain, ct);
463471
PdfCompressor.Compress(document, level, preset.Image.KeepMetadata, ct);
464-
document.Save(builtIn);
472+
SaveDated(document, builtIn);
465473
candidates.Add(builtIn);
466474
}
467475
catch (Exception ex) when (gs is not null && ex is not OperationCanceledException)

0 commit comments

Comments
 (0)