feat(export): PDF and EPUB export with save dialog

This commit is contained in:
pj committed 2026-06-20 13:02:30 -04:00
1 parent da94acc0ec
commit e424008d56
2 files changed
+38

No files matched your search

+19
View File
@@ -0,0 +1,19 @@
import type { Book } from "../model/book";
import { bookToPdfInputs } from "./typst";
import { buildEpub } from "./epub";
import { compilePdf } from "../ipc";
import { saveBytes } from "../project";
const PDF = [{ name: "PDF", extensions: ["pdf"] }];
const EPUB = [{ name: "EPUB", extensions: ["epub"] }];
export async function exportPdf(book: Book): Promise<void> {
const { source, images } = bookToPdfInputs(book);
const bytes = await compilePdf(source, images);
await saveBytes(bytes, `${book.metadata.title || "Untitled"}.pdf`, PDF);
}
export async function exportEpub(book: Book): Promise<void> {
const bytes = await buildEpub(book);
await saveBytes(bytes, `${book.metadata.title || "Untitled"}.epub`, EPUB);
}
+19
View File
@@ -18,3 +18,22 @@ export async function openBook(): Promise<{ book: Book; path: string } | null> {
const contents = await invoke<string>("read_file", { path: selected });
return { book: JSON.parse(contents) as Book, path: selected };
}
function toBase64(bytes: Uint8Array): string {
let binary = "";
const chunk = 0x8000;
for (let i = 0; i < bytes.length; i += chunk) {
binary += String.fromCharCode(...bytes.subarray(i, i + chunk));
}
return btoa(binary);
}
export async function saveBytes(
bytes: Uint8Array,
defaultName: string,
filters: { name: string; extensions: string[] }[]
): Promise<void> {
const path = await save({ filters, defaultPath: defaultName });
if (!path) return;
await invoke("write_bytes", { path, data: toBase64(bytes) });
}