Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Use as a library

#![allow(unused)]
fn main() {
use kakemono::{HtmlOptions, Mode, Paper};

let paper = Paper::from_bytes(std::fs::read("paper.pdf")?)?;
let analysis = paper.analyze();
let out = paper.to_html(&analysis, &HtmlOptions { mode: Mode::Friendly, ..Default::default() });
std::fs::write("paper.html", out.html)?;
}

paper.to_html can be called once per mode with the same analysis, paper.to_docx(&analysis, &DocxOptions::default()) returns the bytes of a Word document (to_docx_with_images also returns its pictures as files), and paper.to_markdown(&analysis, &MarkdownOptions::default()) returns Markdown with its picture files, paper.to_latex(&analysis, &LatexOptions::default()) a LaTeX document with its picture files, paper.to_typst(&analysis, &TypstOptions::default()) a Typst document with its picture files, paper.to_epub(&analysis, &EpubOptions::default()) the bytes of an EPUB book, and paper.to_json(&analysis, &JsonOptions::default()) the editable JSON with its picture files, and paper.to_chunks(&analysis, &ChunkOptions::default()) the chunks for retrieval (kakemono::chunks_jsonl writes them as JSON Lines; TokenCounter::load reads a tokenizer, TokenCounter::custom takes any counting function). The real tokenizers are the default cargo features tokenizers (Hugging Face) and openai-tokenizers; without them, chunks are sized by the estimate. paper.apply_content(&mut analysis, &json) applies an edited JSON file to the analysis, and supported outputs written from it afterwards show the edited text. Word page-layout outputs do not use edited text. analysis.doc is the document structure that --json writes. The API reference is on docs.rs; how the pipeline works is described in ARCHITECTURE.md.

Any of these calls can be stopped from another thread, or after a time limit, with a CancelToken: run the work with token.run(...), which returns Err(Cancelled::Requested) soon after token.cancel() (normally within a page; the pages and the pictures are drawn on worker threads, which stop too), or Err(Cancelled::TimedOut) with CancelToken::with_timeout. The Paper stays usable afterwards, and a token never cancelled changes nothing in the output.

#![allow(unused)]
fn main() {
use kakemono::{CancelToken, Cancelled, Paper};
use std::time::Duration;

let token = CancelToken::with_timeout(Duration::from_secs(30));
let stop = token.clone(); // stop.cancel() from another thread
match token.run(|| Paper::from_path("paper.pdf").map(|paper| paper.analyze())) {
    Ok(read) => { let analysis = read?; /* ... */ }
    Err(Cancelled::TimedOut) => eprintln!("too long"),
    Err(Cancelled::Requested) => eprintln!("stopped"),
}
}

Python

pip install kakemono
import kakemono

html = kakemono.convert("paper.pdf")                   # friendly mode, one self-contained page
html = kakemono.convert("1706.03762", mode="medium")   # an arXiv id or URL works too
pages = kakemono.convert_all("paper.pdf")              # every mode, from one analysis
kakemono.to_docx("paper.pdf", "paper.docx")           # a Word document (also returns its bytes)
kakemono.to_markdown("paper.pdf", "paper.md")          # Markdown, pictures in paper_assets/
kakemono.to_latex("paper.pdf", "paper.tex")            # LaTeX, pictures in paper_assets/
kakemono.to_epub("paper.pdf", "paper.epub")            # an EPUB book (also returns its bytes)
content = kakemono.to_json("paper.pdf", "paper.json")  # the editable JSON (a dict), pictures in paper_assets/
html = kakemono.convert("paper.pdf", "hard-95", content=content)  # any output with the edited text
doc = kakemono.analyze("paper.pdf")                    # the structure, as with --json
kakemono.to_pdf("report.docx", "report.pdf")           # a Word document laid out in pages
kakemono.render_svg("figure.svg", "figure.png")        # an SVG drawn as a PNG, as render-svg does
doc = kakemono.Document("paper.pdf", content=content)  # analysed once, for several outputs:
doc.to_docx("paper.vi.docx"); doc.to_epub("paper.vi.epub")

The wheels (Linux, macOS, Windows; Python 3.9+) contain the Rust library: nothing else to install. The options are those of the command line (tables, equations, page_image, embed_fonts, scale, jobs, png_compression, content: an edited JSON as a dict, its text or its path); see python/README.md.