Commit 72f809fcd7

72f809fcd7b183db92ea669dc4c3b8bab44e2651

parent: 4c5cffea2e

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-13 00:58 UTC

Word export: a page size, Letter or A4 as PDF export has it

Without one, Quick Look and Pages laid the document out on a narrow page.

Layout: unified · split

Sources/OrgApp/DocxWriter.swift +8 −1
@@ -23,7 +23,7 @@ enum DocxWriter {
2323 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2424 <w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" \
2525 xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><w:body>\(body)\
26 <w:sectPr><w:pgMar w:top="1134" w:right="1134" w:bottom="1134" w:left="1134" w:header="0" w:footer="0" w:gutter="0"/></w:sectPr>\
26 \(section(letter: PDFExport.usesLetter()))\
2727 </w:body></w:document>
2828 """
2929 let relationships = links.enumerated().map { index, url in
@@ -52,6 +52,13 @@ enum DocxWriter {
5252 return zip.data()
5353 }
5454
55 /// The page: Letter or A4, as PDF export has it. Without a size, readers pick their own, and
56 /// Quick Look and Pages pick a narrow one.
57 static func section(letter: Bool) -> String {
58 let size = letter ? "<w:pgSz w:w=\"12240\" w:h=\"15840\"/>" : "<w:pgSz w:w=\"11906\" w:h=\"16838\"/>"
59 return "<w:sectPr>\(size)<w:pgMar w:top=\"1134\" w:right=\"1134\" w:bottom=\"1134\" w:left=\"1134\" w:header=\"0\" w:footer=\"0\" w:gutter=\"0\"/></w:sectPr>"
60 }
61
5562 /// Paragraphs in order, consecutive ones in the same table gathered into `<w:tbl>`.
5663 /// A `<w:tbl>` of rows of cells, each cell its paragraphs.
5764 static func table(_ rows: [[String]], columns: Int) -> String {
Sources/OrgApp/PDFExport.swift +6 −2
@@ -9,6 +9,11 @@ import UIKit
99/// PDF from the HTML export, laid out and paginated by WebKit as printing does: no Emacs or TeX.
1010@MainActor
1111public enum PDFExport {
12 /// US Letter where it's the custom, A4 elsewhere.
13 nonisolated static func usesLetter(_ region: String? = Locale.current.region?.identifier) -> Bool {
14 ["US", "CA", "MX", "PH", "CL", "CO", "VE", "PR"].contains(region ?? "")
15 }
16
1217 public struct Failure: Error, CustomStringConvertible {
1318 public let description: String
1419 }
@@ -99,8 +104,7 @@ public enum PDFExport {
99104 }
100105 #else
101106 private static func render(_ webView: WKWebView) async throws -> Data {
102 // US Letter where it's the custom, A4 elsewhere.
103 let letter = ["US", "CA", "MX", "PH", "CL", "CO", "VE", "PR"].contains(Locale.current.region?.identifier ?? "")
107 let letter = usesLetter()
104108 let paper = CGRect(x: 0, y: 0, width: letter ? 612 : 595.2, height: letter ? 792 : 841.8)
105109 let renderer = UIPrintPageRenderer()
106110 renderer.addPrintFormatter(webView.viewPrintFormatter(), startingAtPageAt: 0)
Tests/OrgAppTests/RichTextExportTests.swift +8
@@ -45,11 +45,19 @@ struct RichTextExportTests {
4545 let data = DocxWriter.data(from: try RichTextExport.attributed(html: page, directory: nil), tables: tables)
4646 let xml = String(decoding: data, as: UTF8.self)
4747 #expect(xml.contains("<w:tbl>") && !xml.contains("\u{E000}"))
48 // A page size of its own, or readers choose a narrow one.
49 #expect(xml.contains("<w:pgSz w:w="))
4850 let back = try NSAttributedString(data: data, options: [:], documentAttributes: nil).string
4951 #expect(back.contains("Report") && back.contains("Count") && back.contains("second"), "\(back)")
5052 #expect(RichTextExport.decode("a &amp;lt; &#233;&#x41; &quot;") == "a &lt; éA \"")
5153 }
5254
55 @Test func wordPagesAreLetterOrA4() {
56 #expect(PDFExport.usesLetter("US") && !PDFExport.usesLetter("DE") && !PDFExport.usesLetter(nil))
57 #expect(DocxWriter.section(letter: true).contains("<w:pgSz w:w=\"12240\" w:h=\"15840\"/>"))
58 #expect(DocxWriter.section(letter: false).contains("<w:pgSz w:w=\"11906\" w:h=\"16838\"/>"))
59 }
60
5361 @Test func plainTextAndDarkSystemsStayReadable() throws {
5462 let text = try RichTextExport.attributed(html: Self.html, directory: nil)
5563 let plain = RichTextExport.plain(text)