Commit a9ec94b75c

a9ec94b75caa22bf47461bc180158d2b961bc747

parent: 2eb6989a2f

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-11 02:35 UTC

Native PDF export on the Mac and iOS

WebKit lays out the HTML export and paginates it as printing does: light colors, no breaks inside code, tables and figures or after headings, MathJax typeset first. Export to PDF on the Mac (the LaTeX PDF through Emacs stays as Export to PDF with Emacs) and PDF in the iOS Export menu.

Layout: unified · split

Sources/OrgApp/PDFExport.swift added +144
@@ -0,0 +1,144 @@
1import Foundation
2import WebKit
3#if os(macOS)
4import AppKit
5#else
6import UIKit
7#endif
8
9/// PDF from the HTML export, laid out and paginated by WebKit as printing does: no Emacs or TeX.
10@MainActor
11public enum PDFExport {
12 public struct Failure: Error, CustomStringConvertible {
13 public let description: String
14 }
15
16 /// Rules added for paper: the page's own width, light colors, and no breaks inside code,
17 /// tables and figures or right after a heading.
18 static let printCSS = """
19 <style media="all">
20 :root { color-scheme: light; }
21 * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
22 body { max-width: none; margin: 0; padding: 0; font-size: 11pt; background: #fff; color: #222; }
23 pre, table, figure, blockquote, .figure { break-inside: avoid; }
24 h1, h2, h3, h4, h5, h6 { break-after: avoid; }
25 pre { white-space: pre-wrap; overflow-wrap: anywhere; }
26 </style>
27 """
28
29 /// The page `html` (an `HTMLExport.page`) as a PDF. `directory` is the Org file's folder,
30 /// where relative image links are read from.
31 public static func pdf(html: String, directory: URL?) async throws -> Data {
32 let page = html.replacingOccurrences(of: "</head>", with: printCSS + "\n</head>")
33 let webView = WKWebView(frame: CGRect(x: 0, y: 0, width: 800, height: 1100))
34 let loader = Loader()
35 webView.navigationDelegate = loader
36 #if os(macOS)
37 webView.appearance = NSAppearance(named: .aqua)
38 // A file beside nothing the user owns, readable with the images it links to.
39 let folder = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-pdf-\(UUID().uuidString)")
40 try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
41 defer { try? FileManager.default.removeItem(at: folder) }
42 let file = folder.appendingPathComponent("page.html")
43 let based = directory.map { page.replacingOccurrences(of: "<head>", with: "<head>\n<base href=\"\($0.absoluteString)\">") } ?? page
44 try based.write(to: file, atomically: true, encoding: .utf8)
45 webView.loadFileURL(file, allowingReadAccessTo: URL(fileURLWithPath: "/"))
46 #else
47 webView.overrideUserInterfaceStyle = .light
48 webView.loadHTMLString(page, baseURL: directory)
49 #endif
50 try await loader.loaded()
51 // MathJax typesets after the page loads.
52 _ = try? await webView.callAsyncJavaScript(
53 "if (window.MathJax && MathJax.startup && MathJax.startup.promise) { await MathJax.startup.promise; }",
54 contentWorld: .page
55 )
56 return try await render(webView)
57 }
58
59 #if os(macOS)
60 private static func render(_ webView: WKWebView) async throws -> Data {
61 let output = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-\(UUID().uuidString).pdf")
62 defer { try? FileManager.default.removeItem(at: output) }
63 let info = (NSPrintInfo.shared.copy() as? NSPrintInfo) ?? NSPrintInfo()
64 info.jobDisposition = .save
65 info.dictionary()[NSPrintInfo.AttributeKey.jobSavingURL] = output
66 info.topMargin = 50
67 info.bottomMargin = 50
68 info.leftMargin = 50
69 info.rightMargin = 50
70 info.horizontalPagination = .fit
71 info.verticalPagination = .automatic
72 info.isHorizontallyCentered = false
73 info.isVerticallyCentered = false
74 let operation = webView.printOperation(with: info)
75 operation.showsPrintPanel = false
76 operation.showsProgressPanel = false
77 operation.view?.frame = webView.bounds
78 // The operation runs modally for a window; this one is never shown.
79 let window = NSWindow(contentRect: webView.bounds, styleMask: .borderless, backing: .buffered, defer: false)
80 window.contentView = webView
81 let done = PrintDone()
82 await withCheckedContinuation { continuation in
83 done.continuation = continuation
84 operation.runModal(for: window, delegate: done, didRun: #selector(PrintDone.printOperationDidRun(_:success:contextInfo:)), contextInfo: nil)
85 }
86 guard done.success, let data = try? Data(contentsOf: output), !data.isEmpty else { throw Failure(description: "The PDF couldn't be made.") }
87 return data
88 }
89
90 private final class PrintDone: NSObject {
91 var continuation: CheckedContinuation<Void, Never>?
92 var success = false
93
94 @objc func printOperationDidRun(_ operation: NSPrintOperation, success: Bool, contextInfo: UnsafeMutableRawPointer?) {
95 self.success = success
96 continuation?.resume()
97 continuation = nil
98 }
99 }
100 #else
101 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 ?? "")
104 let paper = CGRect(x: 0, y: 0, width: letter ? 612 : 595.2, height: letter ? 792 : 841.8)
105 let renderer = UIPrintPageRenderer()
106 renderer.addPrintFormatter(webView.viewPrintFormatter(), startingAtPageAt: 0)
107 renderer.setValue(NSValue(cgRect: paper), forKey: "paperRect")
108 renderer.setValue(NSValue(cgRect: paper.insetBy(dx: 50, dy: 50)), forKey: "printableRect")
109 let data = NSMutableData()
110 UIGraphicsBeginPDFContextToData(data, paper, nil)
111 renderer.prepare(forDrawingPages: NSRange(location: 0, length: renderer.numberOfPages))
112 for page in 0..<renderer.numberOfPages {
113 UIGraphicsBeginPDFPage()
114 renderer.drawPage(at: page, in: UIGraphicsGetPDFContextBounds())
115 }
116 UIGraphicsEndPDFContext()
117 guard renderer.numberOfPages > 0 else { throw Failure(description: "The PDF couldn't be made.") }
118 return data as Data
119 }
120 #endif
121
122 private final class Loader: NSObject, WKNavigationDelegate {
123 private var continuation: CheckedContinuation<Void, Error>?
124 private var result: Result<Void, Error>?
125
126 func loaded() async throws {
127 if let result { return try result.get() }
128 try await withCheckedThrowingContinuation { self.continuation = $0 }
129 }
130
131 private func finish(_ result: Result<Void, Error>) {
132 if let continuation {
133 continuation.resume(with: result)
134 self.continuation = nil
135 } else {
136 self.result = result
137 }
138 }
139
140 func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { finish(.success(())) }
141 func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) { finish(.failure(error)) }
142 func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) { finish(.failure(error)) }
143 }
144}
Sources/OrgEditorAppKit/EditorCommands.swift +1
@@ -105,6 +105,7 @@ public enum EditorCommands {
105 EditorCommand(id: "app.export.html", title: "Export to HTML", action: .app), 105 EditorCommand(id: "app.export.html", title: "Export to HTML", action: .app),
106 EditorCommand(id: "app.export.html-open", title: "Export to HTML and Open", action: .app), 106 EditorCommand(id: "app.export.html-open", title: "Export to HTML and Open", action: .app),
107 EditorCommand(id: "app.export.markdown", title: "Export to Markdown", action: .app), 107 EditorCommand(id: "app.export.markdown", title: "Export to Markdown", action: .app),
108 EditorCommand(id: "app.export.print-pdf", title: "Export to PDF", action: .app),
108 EditorCommand(id: "app.export.pdf", title: "Export to PDF with Emacs", action: .app), 109 EditorCommand(id: "app.export.pdf", title: "Export to PDF with Emacs", action: .app),
109 EditorCommand(id: "app.export.odt", title: "Export to ODT with Emacs", action: .app), 110 EditorCommand(id: "app.export.odt", title: "Export to ODT with Emacs", action: .app),
110 EditorCommand(id: "app.export.latex", title: "Export to LaTeX with Emacs", action: .app), 111 EditorCommand(id: "app.export.latex", title: "Export to LaTeX with Emacs", action: .app),
Sources/Orgstar/ContentView.swift +19
@@ -446,6 +446,25 @@ struct ContentView: View {
446 } catch { 446 } catch {
447 commands.show("Can't write \(target.lastPathComponent): \(error.localizedDescription)") 447 commands.show("Can't write \(target.lastPathComponent): \(error.localizedDescription)")
448 } 448 }
449 case "print-pdf":
450 let name = ExportScope.fileName(whole, subtreeAt: subtree ? caret : nil, settings: defaults)
451 let target = destination ?? ExportScope.output(for: url, name: name, extension: "pdf")
452 let html = HTMLExport.page(
453 text, fallbackTitle: base.lastPathComponent, settings: defaults, path: path,
454 codeStyle: HTMLCodeStyle(highlighter: EditorView.highlighter, theme: theme)
455 )
456 commands.show("Exporting to PDF…")
457 Task {
458 do {
459 let data = try await PDFExport.pdf(html: html, directory: url.deletingLastPathComponent())
460 try FileManager.default.createDirectory(at: target.deletingLastPathComponent(), withIntermediateDirectories: true)
461 try data.write(to: target, options: .atomic)
462 commands.show("Exported to \(target.lastPathComponent)")
463 if open { NSWorkspace.shared.open(target) }
464 } catch {
465 commands.show("Can't export \(target.lastPathComponent): \(error)")
466 }
467 }
449 default: 468 default:
450 let functions = ["pdf": "org-latex-export-to-pdf", "odt": "org-odt-export-to-odt", "latex": "org-latex-export-to-latex", "text": "org-ascii-export-to-ascii"] 469 let functions = ["pdf": "org-latex-export-to-pdf", "odt": "org-odt-export-to-odt", "latex": "org-latex-export-to-latex", "text": "org-ascii-export-to-ascii"]
451 guard let function = functions[format] else { return } 470 guard let function = functions[format] else { return }
Sources/Orgstar/ExportSheet.swift +2 −2
@@ -17,14 +17,14 @@ struct ExportSheet: View {
17 @Environment(\.dismiss) private var dismiss 17 @Environment(\.dismiss) private var dismiss
18 18
19 static let formats: [(id: String, title: String, ext: String)] = [ 19 static let formats: [(id: String, title: String, ext: String)] = [
20 ("html", "HTML", "html"), ("markdown", "Markdown", "md"), ("pdf", "PDF (Emacs)", "pdf"), 20 ("html", "HTML", "html"), ("markdown", "Markdown", "md"), ("print-pdf", "PDF", "pdf"), ("pdf", "PDF via LaTeX (Emacs)", "pdf"),
21 ("odt", "ODT (Emacs)", "odt"), ("latex", "LaTeX (Emacs)", "tex"), ("text", "Plain text (Emacs)", "txt"), 21 ("odt", "ODT (Emacs)", "odt"), ("latex", "LaTeX (Emacs)", "tex"), ("text", "Plain text (Emacs)", "txt"),
22 ] 22 ]
23 23
24 private var ext: String { Self.formats.first { $0.id == format }?.ext ?? "html" } 24 private var ext: String { Self.formats.first { $0.id == format }?.ext ?? "html" }
25 private var chosen: URL { destination ?? target(ext, subtree) } 25 private var chosen: URL { destination ?? target(ext, subtree) }
26 /// PDF and ODT are whole documents. 26 /// PDF and ODT are whole documents.
27 private var allowsBodyOnly: Bool { !["pdf", "odt"].contains(format) } 27 private var allowsBodyOnly: Bool { !["pdf", "print-pdf", "odt"].contains(format) }
28 28
29 var body: some View { 29 var body: some View {
30 Form { 30 Form {
Sources/OrgstarMobile/ExportMenu.swift +42 −22
@@ -6,9 +6,27 @@ import OrgPresentation
6import SwiftUI 6import SwiftUI
7import UniformTypeIdentifiers 7import UniformTypeIdentifiers
8 8
9/// A file exported as HTML or Markdown, written when the share sheet takes it. 9/// A file exported as HTML, Markdown or PDF, written when the share sheet takes it.
10struct OrgExport: Transferable { 10struct OrgExport: Transferable {
11 enum Format { case html, markdown } 11 enum Format: CaseIterable {
12 case html, markdown, pdf
13
14 var name: String {
15 switch self {
16 case .html: "HTML"
17 case .markdown: "Markdown"
18 case .pdf: "PDF"
19 }
20 }
21
22 var fileExtension: String {
23 switch self {
24 case .html: "html"
25 case .markdown: "md"
26 case .pdf: "pdf"
27 }
28 }
29 }
12 30
13 let text: String 31 let text: String
14 let path: String 32 let path: String
@@ -19,39 +37,41 @@ struct OrgExport: Transferable {
19 37
20 /// `#+EXPORT_FILE_NAME`'s name, or the file's, with the format's extension. 38 /// `#+EXPORT_FILE_NAME`'s name, or the file's, with the format's extension.
21 var fileName: String { 39 var fileName: String {
22 ExportScope.output(for: URL(fileURLWithPath: path), name: ExportScope.fileName(text, settings: settings), extension: format == .html ? "html" : "md") 40 ExportScope.output(for: URL(fileURLWithPath: path), name: ExportScope.fileName(text, settings: settings), extension: format.fileExtension)
23 .lastPathComponent 41 .lastPathComponent
24 } 42 }
25 43
26 static var transferRepresentation: some TransferRepresentation { 44 static var transferRepresentation: some TransferRepresentation {
27 FileRepresentation(exportedContentType: .html) { export in 45 FileRepresentation(exportedContentType: .html) { try await $0.write() }
28 try export.write() 46 .exportingCondition { $0.format == .html }
29 } 47 FileRepresentation(exportedContentType: UTType(filenameExtension: "md") ?? .plainText) { try await $0.write() }
30 .exportingCondition { $0.format == .html } 48 .exportingCondition { $0.format == .markdown }
31 FileRepresentation(exportedContentType: UTType(filenameExtension: "md") ?? .plainText) { export in 49 FileRepresentation(exportedContentType: .pdf) { try await $0.write() }
32 try export.write() 50 .exportingCondition { $0.format == .pdf }
33 }
34 .exportingCondition { $0.format == .markdown }
35 } 51 }
36 52
37 private func write() throws -> SentTransferredFile { 53 private func write() async throws -> SentTransferredFile {
38 let base = ((path as NSString).lastPathComponent as NSString).deletingPathExtension 54 let base = ((path as NSString).lastPathComponent as NSString).deletingPathExtension
39 let settings = settings.resolvingSetupFiles(in: text, path: path, read: OrgSettings.readFile) 55 let settings = settings.resolvingSetupFiles(in: text, path: path, read: OrgSettings.readFile)
40 let content = switch format {
41 case .html: HTMLExport.page(
42 text, fallbackTitle: base, settings: settings, path: path, codeStyle: codeStyle
43 )
44 case .markdown: MarkdownExport.document(text, settings: settings, path: path)
45 }
46 let folder = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) 56 let folder = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
47 try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true) 57 try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
48 let url = folder.appendingPathComponent(fileName) 58 let url = folder.appendingPathComponent(fileName)
49 try content.write(to: url, atomically: true, encoding: .utf8) 59 switch format {
60 case .html:
61 try HTMLExport.page(text, fallbackTitle: base, settings: settings, path: path, codeStyle: codeStyle)
62 .write(to: url, atomically: true, encoding: .utf8)
63 case .markdown:
64 try MarkdownExport.document(text, settings: settings, path: path).write(to: url, atomically: true, encoding: .utf8)
65 case .pdf:
66 let html = HTMLExport.page(text, fallbackTitle: base, settings: settings, path: path, codeStyle: codeStyle)
67 let directory = URL(fileURLWithPath: path).deletingLastPathComponent()
68 try await PDFExport.pdf(html: html, directory: directory).write(to: url, options: .atomic)
69 }
50 return SentTransferredFile(url) 70 return SentTransferredFile(url)
51 } 71 }
52} 72}
53 73
54/// Sharing the file as HTML or Markdown: to another app, or saved with Save to Files. 74/// Sharing the file as HTML, Markdown or PDF: to another app, or saved with Save to Files.
55struct ExportMenu: View { 75struct ExportMenu: View {
56 let path: String 76 let path: String
57 let text: String 77 let text: String
@@ -59,10 +79,10 @@ struct ExportMenu: View {
59 79
60 var body: some View { 80 var body: some View {
61 Menu("Export", systemImage: "square.and.arrow.up") { 81 Menu("Export", systemImage: "square.and.arrow.up") {
62 ForEach([(OrgExport.Format.html, "HTML"), (.markdown, "Markdown")], id: \.1) { format, name in 82 ForEach(OrgExport.Format.allCases, id: \.self) { format in
63 let export = OrgExport(text: text, path: path, format: format, settings: OrgPreferences.orgSettings(), 83 let export = OrgExport(text: text, path: path, format: format, settings: OrgPreferences.orgSettings(),
64 codeStyle: HTMLCodeStyle(highlighter: MobileEditor.highlighter, theme: model.theme)) 84 codeStyle: HTMLCodeStyle(highlighter: MobileEditor.highlighter, theme: model.theme))
65 ShareLink(item: export, preview: SharePreview(export.fileName)) { Text(name) } 85 ShareLink(item: export, preview: SharePreview(export.fileName)) { Text(format.name) }
66 } 86 }
67 } 87 }
68 } 88 }
Tests/OrgAppTests/PDFExportTests.swift added +21
@@ -0,0 +1,21 @@
1import Foundation
2import OrgCore
3import PDFKit
4import Testing
5@testable import OrgApp
6
7@MainActor
8struct PDFExportTests {
9 /// A long document comes out as several pages with its text, without Emacs.
10 @Test func paginatesTheHTMLExport() async throws {
11 let sections = (1...40).map { "* Section \($0)\n" + String(repeating: "Some words in a paragraph. ", count: 30) + "\n" }.joined()
12 let html = HTMLExport.page("#+TITLE: Long\n" + sections + "#+begin_src sh\necho done\n#+end_src\n", fallbackTitle: "t")
13 let data = try await PDFExport.pdf(html: html, directory: nil)
14 let document = try #require(PDFDocument(data: data))
15 #expect(document.pageCount > 3)
16 let text = document.string ?? ""
17 #expect(text.contains("Long") && text.contains("Section 40") && text.contains("echo done"))
18 let page = try #require(document.page(at: 0)).bounds(for: .mediaBox)
19 #expect(page.width < page.height)
20 }
21}
docs/manual/guide/03-keys.org +1
@@ -937,6 +937,7 @@ When a key appears in several rows, it runs the command whose condition holds at
937| Export to HTML (=app.export.html=) | =C-c C-e h h= | — | =SPC m e h h= (N) | Exports to HTML (=org-html-export-to-html=). | 937| Export to HTML (=app.export.html=) | =C-c C-e h h= | — | =SPC m e h h= (N) | Exports to HTML (=org-html-export-to-html=). |
938| Export to HTML and Open (=app.export.html-open=) | =C-c C-e h o= | — | =SPC m e h o= (N) | Exports to HTML and opens the result. | 938| Export to HTML and Open (=app.export.html-open=) | =C-c C-e h o= | — | =SPC m e h o= (N) | Exports to HTML and opens the result. |
939| Export to Markdown (=app.export.markdown=) | =C-c C-e m m= | — | =SPC m e m m= (N) | Exports to Markdown (=org-md-export-to-markdown=). | 939| Export to Markdown (=app.export.markdown=) | =C-c C-e m m= | — | =SPC m e m m= (N) | Exports to Markdown (=org-md-export-to-markdown=). |
940| Export to PDF (=app.export.print-pdf=) | — | — | — | Exports to PDF without Emacs, laid out as the HTML export. |
940| Export to PDF with Emacs (=app.export.pdf=) | =C-c C-e l p= | — | =SPC m e l p= (N) | Exports to PDF through Emacs (=org-latex-export-to-pdf=). | 941| Export to PDF with Emacs (=app.export.pdf=) | =C-c C-e l p= | — | =SPC m e l p= (N) | Exports to PDF through Emacs (=org-latex-export-to-pdf=). |
941| Export to LaTeX with Emacs (=app.export.latex=) | =C-c C-e l l= | — | — | Exports to LaTeX through Emacs (=org-latex-export-to-latex=). | 942| Export to LaTeX with Emacs (=app.export.latex=) | =C-c C-e l l= | — | — | Exports to LaTeX through Emacs (=org-latex-export-to-latex=). |
942| Export to ODT with Emacs (=app.export.odt=) | =C-c C-e o o= | — | — | Exports to ODT through Emacs (=org-odt-export-to-odt=). | 943| Export to ODT with Emacs (=app.export.odt=) | =C-c C-e o o= | — | — | Exports to ODT through Emacs (=org-odt-export-to-odt=). |
docs/manual/guide/12-export.org +17 −6
@@ -1,6 +1,6 @@
1#+TITLE: Export 1#+TITLE: Export
2#+DESCRIPTION: Exporting Org files from orgstar to HTML and Markdown, and to PDF, LaTeX, ODT and plain text through Emacs. 2#+DESCRIPTION: Exporting Org files from orgstar to HTML, Markdown and PDF, and to LaTeX, ODT and plain text through Emacs.
3#+LEDE: orgstar exports HTML and Markdown itself, and hands PDF, LaTeX, ODT and plain text to Emacs on the Mac. 3#+LEDE: orgstar exports HTML, Markdown and PDF itself, on the Mac and iOS, and hands LaTeX, ODT, plain text and LaTeX-made PDF to Emacs on the Mac.
4 4
5* Exporting a file 5* Exporting a file
6 6
@@ -13,20 +13,21 @@ Export works on the file in the current editor, using its text as it is now, sav
13| HTML | Export to HTML | =C-c C-e h h= | =SPC m e h h=, =C-c C-e h h= | no | 13| HTML | Export to HTML | =C-c C-e h h= | =SPC m e h h=, =C-c C-e h h= | no |
14| HTML, then open it | Export to HTML and Open | =C-c C-e h o= | =SPC m e h o=, =C-c C-e h o= | no | 14| HTML, then open it | Export to HTML and Open | =C-c C-e h o= | =SPC m e h o=, =C-c C-e h o= | no |
15| Markdown | Export to Markdown | =C-c C-e m m= | =SPC m e m m=, =C-c C-e m m= | no | 15| Markdown | Export to Markdown | =C-c C-e m m= | =SPC m e m m=, =C-c C-e m m= | no |
16| PDF | Export to PDF with Emacs | =C-c C-e l p= | =SPC m e l p=, =C-c C-e l p= | yes | 16| PDF | Export to PDF | — | — | no |
17| PDF through LaTeX | Export to PDF with Emacs | =C-c C-e l p= | =SPC m e l p=, =C-c C-e l p= | yes |
17| LaTeX | Export to LaTeX with Emacs | =C-c C-e l l= | =C-c C-e l l= | yes | 18| LaTeX | Export to LaTeX with Emacs | =C-c C-e l l= | =C-c C-e l l= | yes |
18| ODT | Export to ODT with Emacs | =C-c C-e o o= | =C-c C-e o o= | yes | 19| ODT | Export to ODT with Emacs | =C-c C-e o o= | =C-c C-e o o= | yes |
19| Plain text | Export to Plain Text with Emacs | =C-c C-e t u= | =C-c C-e t u= | yes | 20| Plain text | Export to Plain Text with Emacs | =C-c C-e t u= | =C-c C-e t u= | yes |
20 21
21The keys follow Emacs's export dispatcher (=org-export-dispatch=). =C-c C-e= on its own is only a prefix: pause after it and the key hints list the formats. The =SPC m e= keys work in normal state; the =C-c C-e= keys work in every Doom state. The Mac preset has no keys for the single formats; its =⇧⌘E= opens the export dialog. 22The keys follow Emacs's export dispatcher (=org-export-dispatch=). =C-c C-e= on its own is only a prefix: pause after it and the key hints list the formats. The =SPC m e= keys work in normal state; the =C-c C-e= keys work in every Doom state. The Mac preset has no keys for the single formats; its =⇧⌘E= opens the export dialog.
22 23
23All seven commands are in File ▸ Export and in the command palette. They write the export beside the Org file, with the same name and the format's extension: =.html=, =.md=, =.pdf=, =.tex=, =.odt= or =.txt=. =#+EXPORT_FILE_NAME:= names it instead, as in Org: relative to the Org file's folder (or absolute, or starting with =~=), with its extension replaced by the format's, so =#+EXPORT_FILE_NAME: out/report= writes =out/report.html=. A file already there is replaced. The message area shows "Exported to /name/". 24All eight commands are in File ▸ Export and in the command palette. They write the export beside the Org file, with the same name and the format's extension: =.html=, =.md=, =.pdf=, =.tex=, =.odt= or =.txt=. =#+EXPORT_FILE_NAME:= names it instead, as in Org: relative to the Org file's folder (or absolute, or starting with =~=), with its extension replaced by the format's, so =#+EXPORT_FILE_NAME: out/report= writes =out/report.html=. A file already there is replaced. The message area shows "Exported to /name/".
24 25
25** The export dialog 26** The export dialog
26 27
27*Export…* in the command palette, or =⇧⌘E= in the Mac preset, opens a dialog. The Emacs and Doom presets have no key for it; bind =app.export-dialog= in =keymap.toml= if you want one (see [[file:03-keys.org][Keys and commands]]). The dialog has: 28*Export…* in the command palette, or =⇧⌘E= in the Mac preset, opens a dialog. The Emacs and Doom presets have no key for it; bind =app.export-dialog= in =keymap.toml= if you want one (see [[file:03-keys.org][Keys and commands]]). The dialog has:
28 29
29- *Format*: HTML, Markdown, PDF (Emacs), ODT (Emacs), LaTeX (Emacs) or Plain text (Emacs). 30- *Format*: HTML, Markdown, PDF, PDF via LaTeX (Emacs), ODT (Emacs), LaTeX (Emacs) or Plain text (Emacs).
30- *Scope*: the whole file, or the subtree at the caret (Org's =C-s= in the dispatcher). A subtree is exported as a document of its own: its heading is the title, its text the body, and its child headings move up a level. The file's =#+= keywords still apply, and the heading's =EXPORT_TITLE=, =EXPORT_AUTHOR=, =EXPORT_DATE=, =EXPORT_EMAIL=, =EXPORT_OPTIONS=, =EXPORT_SELECT_TAGS=, =EXPORT_EXCLUDE_TAGS=, =EXPORT_HTML_HEAD= and =EXPORT_HTML_HEAD_EXTRA= properties override them. Its planning line and property drawer are left out. The choice is available when the caret is under a heading. 31- *Scope*: the whole file, or the subtree at the caret (Org's =C-s= in the dispatcher). A subtree is exported as a document of its own: its heading is the title, its text the body, and its child headings move up a level. The file's =#+= keywords still apply, and the heading's =EXPORT_TITLE=, =EXPORT_AUTHOR=, =EXPORT_DATE=, =EXPORT_EMAIL=, =EXPORT_OPTIONS=, =EXPORT_SELECT_TAGS=, =EXPORT_EXCLUDE_TAGS=, =EXPORT_HTML_HEAD= and =EXPORT_HTML_HEAD_EXTRA= properties override them. Its planning line and property drawer are left out. The choice is available when the caret is under a heading.
31- *Body only*: leave out the title and the page around the text (Org's =C-b=). HTML is then the table of contents, the text and the footnotes, without =<html>=, =<head>= or the title; Markdown has no title heading. Not available for PDF and ODT. 32- *Body only*: leave out the title and the page around the text (Org's =C-b=). HTML is then the table of contents, the text and the footnotes, without =<html>=, =<head>= or the title; Markdown has no title heading. Not available for PDF and ODT.
32- *Destination*: where =#+EXPORT_FILE_NAME:= says, or the subtree's =EXPORT_FILE_NAME= property for a subtree, else beside the Org file. *Choose…* picks another place and name. Changing the format changes the extension. 33- *Destination*: where =#+EXPORT_FILE_NAME:= says, or the subtree's =EXPORT_FILE_NAME= property for a subtree, else beside the Org file. *Choose…* picks another place and name. Changing the format changes the extension.
@@ -36,7 +37,7 @@ The dialog remembers the format and the *Open after export* setting.
36 37
37** On iOS 38** On iOS
38 39
39The Export menu offers HTML and Markdown. It is under More in the editor, and in the toolbar in the reader. Choosing one opens the share sheet with the exported file, named after the Org file or its =#+EXPORT_FILE_NAME:=. Send it to another app, or use Save to Files to keep it. The export is the same as on the Mac. PDF, LaTeX, ODT and plain text are not available on iOS. 40The Export menu offers HTML, Markdown and PDF. It is under More in the editor, and in the toolbar in the reader. Choosing one opens the share sheet with the exported file, named after the Org file or its =#+EXPORT_FILE_NAME:=. Send it to another app, or use Save to Files to keep it. The export is the same as on the Mac. LaTeX, ODT, plain text and PDF through LaTeX are not available on iOS.
40 41
41* HTML export 42* HTML export
42 43
@@ -193,6 +194,16 @@ Comments, drawers, property drawers, planning lines, clock lines and keywords ar
193 194
194HTML and Markdown export handle citations the same way; see Citations below. 195HTML and Markdown export handle citations the same way; see Citations below.
195 196
197* PDF export
198
199Export to PDF makes the HTML export into a PDF, laid out and split into pages by the system's web engine as printing does. It needs neither Emacs nor TeX, and works on iOS. The PDF has what the HTML export has: the title, the byline, the table of contents, colored code, tables, images and math (MathJax, which needs a network connection). For the page:
200
201- Colors are always the light ones, whatever the system's appearance.
202- Pages are US Letter where that is the usual size and A4 elsewhere; on the Mac, the paper size set for printing.
203- Code blocks, tables, figures and quotes are kept on one page when they fit, and a heading is not left at the bottom of a page.
204
205For typeset output with LaTeX's classes and packages, use Export to PDF with Emacs instead (see /Exporting through Emacs/).
206
196* Markdown export 207* Markdown export
197 208
198Markdown export writes GitHub-flavored Markdown: 209Markdown export writes GitHub-flavored Markdown: