import Foundation
import OrgCore
import PDFKit
import UniformTypeIdentifiers
import WebKit
#if os(macOS)
import AppKit
#else
import UIKit
#endif
/// PDF from the HTML export, laid out and paginated by WebKit as printing does: no Emacs or TeX.
@MainActor
public enum PDFExport {
/// US Letter where it's the custom, A4 elsewhere.
nonisolated static func usesLetter(_ region: String? = Locale.current.region?.identifier) -> Bool {
["US", "CA", "MX", "PH", "CL", "CO", "VE", "PR"].contains(region ?? "")
}
public struct Failure: Error, CustomStringConvertible {
public let description: String
}
/// The page's margins in points, a font family for the text (empty: the export's own), and
/// whether pages are numbered.
public struct Options: Sendable, Equatable {
public var margin: Int
public var font: String
public var pageNumbers: Bool
public init(margin: Int = 50, font: String = "", pageNumbers: Bool = true) {
self.margin = margin
self.font = font
self.pageNumbers = pageNumbers
}
}
/// `printCSS` with the body font, when one is set.
static func css(_ options: Options) -> String {
guard !options.font.isEmpty else { return printCSS }
let family = options.font.replacingOccurrences(of: "\"", with: "")
return printCSS.replacingOccurrences(of: "", with: "body { font-family: \"\(family)\", -apple-system, sans-serif; }\n")
}
/// `html` with each `
` that names a local file given that file as a `data:` URI.
/// Relative paths are read from `directory`; files that can't be read keep their `src`.
nonisolated static func inliningImages(_ html: String, directory: URL?) -> String {
let pattern = /(
]*?\bsrc=")([^"]*)(")/
return html.replacing(pattern) { match in
let (_, before, src, after) = match.output
guard let uri = dataURI(String(src), directory: directory) else { return String(match.output.0) }
return before + uri + after
}
}
/// The local files the `
` tags of `html` name.
public nonisolated static func imageFiles(_ html: String, directory: URL?) -> [String] {
var paths: [String] = []
for match in html.matches(of: /
]*?\bsrc="([^"]*)"/) {
if let path = imageURL(String(match.output.1), directory: directory)?.path, !paths.contains(path) { paths.append(path) }
}
return paths
}
/// The file an `
` names, as a `data:` URI; nil for remote or unreadable images.
nonisolated static func dataURI(_ src: String, directory: URL?) -> String? {
guard let url = imageURL(src, directory: directory), let data = try? Data(contentsOf: url) else { return nil }
let type = UTType(filenameExtension: url.pathExtension)?.preferredMIMEType ?? "application/octet-stream"
return "data:\(type);base64,\(data.base64EncodedString())"
}
/// The local file an `
` names; nil for remote images.
nonisolated static func imageURL(_ src: String, directory: URL?) -> URL? {
let path = src.replacingOccurrences(of: """, with: "\"").replacingOccurrences(of: "<", with: "<")
.replacingOccurrences(of: ">", with: ">").replacingOccurrences(of: "&", with: "&")
let url: URL
if path.hasPrefix("file://") {
guard let parsed = URL(string: path) else { return nil }
url = parsed
} else if path.hasPrefix("/") {
url = URL(fileURLWithPath: path)
} else if path.hasPrefix("~/") {
url = URL(fileURLWithPath: OrgPaths.expandingTilde(path))
} else if !path.contains(":"), let directory {
url = directory.appendingPathComponent(path)
} else {
return nil
}
return url
}
/// Each page's number, centred in its bottom margin, as an annotation: drawing it into the
/// page would mean drawing the page again and losing WebKit's links.
static func numberPages(_ data: Data, margin: Int) -> Data {
guard let document = PDFDocument(data: data) else { return data }
for index in 0..
:root { color-scheme: light; }
* { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
body { max-width: none; margin: 0; padding: 0; font-size: 11pt; background: #fff; color: #222; }
pre, table, figure, blockquote, .figure { break-inside: avoid; }
h1, h2, h3, h4, h5, h6 { break-after: avoid; }
pre { white-space: pre-wrap; overflow-wrap: anywhere; }
"""
/// The page `html` (an `HTMLExport.page`) as a PDF. `directory` is the Org file's folder,
/// where relative image links are read from.
public static func pdf(html: String, directory: URL?, options: Options = Options()) async throws -> Data {
let page = html.replacingOccurrences(of: "", with: css(options) + "\n")
let webView = WKWebView(frame: CGRect(x: 0, y: 0, width: 800, height: 1100))
let loader = Loader()
webView.navigationDelegate = loader
#if os(macOS)
webView.appearance = NSAppearance(named: .aqua)
// A file beside nothing the user owns, readable with the images it links to. In the
// sandbox WebKit's own process can't read the user's folders, so the images go in the page.
let sandboxed = Sandbox.isActive
let folder = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-pdf-\(UUID().uuidString)")
try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
defer { try? FileManager.default.removeItem(at: folder) }
let file = folder.appendingPathComponent("page.html")
let source = sandboxed ? inliningImages(page, directory: directory) : page
let based = directory.map { source.replacingOccurrences(of: "", with: "\n") } ?? source
try based.write(to: file, atomically: true, encoding: .utf8)
webView.loadFileURL(file, allowingReadAccessTo: sandboxed ? folder : URL(fileURLWithPath: "/"))
#else
webView.overrideUserInterfaceStyle = .light
webView.loadHTMLString(page, baseURL: directory)
#endif
try await loader.loaded()
// MathJax typesets after the page loads.
_ = try? await webView.callAsyncJavaScript(
"if (window.MathJax && MathJax.startup && MathJax.startup.promise) { await MathJax.startup.promise; }",
contentWorld: .page
)
let data = try await render(webView, margin: CGFloat(max(0, options.margin)))
return options.pageNumbers ? numberPages(data, margin: options.margin) : data
}
#if os(macOS)
private static func render(_ webView: WKWebView, margin: CGFloat) async throws -> Data {
let output = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-\(UUID().uuidString).pdf")
defer { try? FileManager.default.removeItem(at: output) }
let info = (NSPrintInfo.shared.copy() as? NSPrintInfo) ?? NSPrintInfo()
info.jobDisposition = .save
info.dictionary()[NSPrintInfo.AttributeKey.jobSavingURL] = output
info.topMargin = margin
info.bottomMargin = margin
info.leftMargin = margin
info.rightMargin = margin
info.horizontalPagination = .fit
info.verticalPagination = .automatic
info.isHorizontallyCentered = false
info.isVerticallyCentered = false
let operation = webView.printOperation(with: info)
operation.showsPrintPanel = false
operation.showsProgressPanel = false
operation.view?.frame = webView.bounds
// The operation runs modally for a window; this one is never shown.
let window = NSWindow(contentRect: webView.bounds, styleMask: .borderless, backing: .buffered, defer: false)
window.contentView = webView
let done = PrintDone()
await withCheckedContinuation { continuation in
done.continuation = continuation
operation.runModal(for: window, delegate: done, didRun: #selector(PrintDone.printOperationDidRun(_:success:contextInfo:)), contextInfo: nil)
}
guard done.success, let data = try? Data(contentsOf: output), !data.isEmpty else { throw Failure(description: "The PDF couldn't be made.") }
return data
}
private final class PrintDone: NSObject {
var continuation: CheckedContinuation?
var success = false
@objc func printOperationDidRun(_ operation: NSPrintOperation, success: Bool, contextInfo: UnsafeMutableRawPointer?) {
self.success = success
continuation?.resume()
continuation = nil
}
}
#else
private static func render(_ webView: WKWebView, margin: CGFloat) async throws -> Data {
let letter = usesLetter()
let paper = CGRect(x: 0, y: 0, width: letter ? 612 : 595.2, height: letter ? 792 : 841.8)
let renderer = UIPrintPageRenderer()
renderer.addPrintFormatter(webView.viewPrintFormatter(), startingAtPageAt: 0)
renderer.setValue(NSValue(cgRect: paper), forKey: "paperRect")
renderer.setValue(NSValue(cgRect: paper.insetBy(dx: margin, dy: margin)), forKey: "printableRect")
let data = NSMutableData()
UIGraphicsBeginPDFContextToData(data, paper, nil)
renderer.prepare(forDrawingPages: NSRange(location: 0, length: renderer.numberOfPages))
for page in 0.. 0 else { throw Failure(description: "The PDF couldn't be made.") }
return data as Data
}
#endif
private final class Loader: NSObject, WKNavigationDelegate {
private var continuation: CheckedContinuation?
private var result: Result?
func loaded() async throws {
if let result { return try result.get() }
try await withCheckedThrowingContinuation { self.continuation = $0 }
}
private func finish(_ result: Result) {
if let continuation {
continuation.resume(with: result)
self.continuation = nil
} else {
self.result = result
}
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { finish(.success(())) }
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) { finish(.failure(error)) }
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) { finish(.failure(error)) }
}
}