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 { |
| 23 | 23 | <?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
| 24 | 24 | <w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" \ |
| 25 | 25 | 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()))\ |
| 27 | 27 | </w:body></w:document> |
| 28 | 28 | """ |
| 29 | 29 | let relationships = links.enumerated().map { index, url in |
| @@ -52,6 +52,13 @@ enum DocxWriter { |
| 52 | 52 | return zip.data() |
| 53 | 53 | } |
| 54 | 54 | |
| 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 | |
| 55 | 62 | /// Paragraphs in order, consecutive ones in the same table gathered into `<w:tbl>`. |
| 56 | 63 | /// A `<w:tbl>` of rows of cells, each cell its paragraphs. |
| 57 | 64 | static func table(_ rows: [[String]], columns: Int) -> String { |
Sources/OrgApp/PDFExport.swift
+6 −2
| @@ -9,6 +9,11 @@ import UIKit |
| 9 | 9 | /// PDF from the HTML export, laid out and paginated by WebKit as printing does: no Emacs or TeX. |
| 10 | 10 | @MainActor |
| 11 | 11 | public 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 | |
| 12 | 17 | public struct Failure: Error, CustomStringConvertible { |
| 13 | 18 | public let description: String |
| 14 | 19 | } |
| @@ -99,8 +104,7 @@ public enum PDFExport { |
| 99 | 104 | } |
| 100 | 105 | #else |
| 101 | 106 | 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() |
| 104 | 108 | let paper = CGRect(x: 0, y: 0, width: letter ? 612 : 595.2, height: letter ? 792 : 841.8) |
| 105 | 109 | let renderer = UIPrintPageRenderer() |
| 106 | 110 | renderer.addPrintFormatter(webView.viewPrintFormatter(), startingAtPageAt: 0) |
Tests/OrgAppTests/RichTextExportTests.swift
+8
| @@ -45,11 +45,19 @@ struct RichTextExportTests { |
| 45 | 45 | let data = DocxWriter.data(from: try RichTextExport.attributed(html: page, directory: nil), tables: tables) |
| 46 | 46 | let xml = String(decoding: data, as: UTF8.self) |
| 47 | 47 | #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=")) |
| 48 | 50 | let back = try NSAttributedString(data: data, options: [:], documentAttributes: nil).string |
| 49 | 51 | #expect(back.contains("Report") && back.contains("Count") && back.contains("second"), "\(back)") |
| 50 | 52 | #expect(RichTextExport.decode("a &lt; éA "") == "a < éA \"") |
| 51 | 53 | } |
| 52 | 54 | |
| 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 | |
| 53 | 61 | @Test func plainTextAndDarkSystemsStayReadable() throws { |
| 54 | 62 | let text = try RichTextExport.attributed(html: Self.html, directory: nil) |
| 55 | 63 | let plain = RichTextExport.plain(text) |