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