All formats

caj2pdf-core

LanguageRust (1.88+)
LicenseMIT
Repositorygithub.com/rwv/caj2pdf-rust
Documentationdocs.rs/caj2pdf-core, CLI reference
Depends onpure Rust; C8 and HN text pages need a font pack (bundled with the CLI)
Read / writereads CAJ, KDH, PDF; experimental HN, C8, NH / writes PDF only

caj2pdf-core converts CNKI documents to PDF. convert detects the variant from the first bytes and writes a PDF to any std::io::Write; inspect returns the format, page count and outline without converting. Input is any RangedSource, such as a file wrapped in SeekableSource or a byte slice, and is read in bounded chunks, so it handles large files and runs in WebAssembly.

The project is at 0.x and its API may still change. The support matrix lists which variants convert: CAJ and KDH are supported; HN-A converts as image pages; C8 and HN-B are experimental; TEB files can’t be converted. Unsupported content fails with an error instead of silently dropping pages.

Installation#

Shell
cargo add caj2pdf-core
cargo install --locked caj2pdf-cli     # the `caj2pdf` command-line tool

Prebuilt caj2pdf binaries for Windows, macOS and Linux are on the releases page.

Basic usage: convert CAJ to PDF#

Rust
use caj2pdf_core::{convert, native::SeekableSource, ConversionOptions, Limits, NeverCancel};
use std::{fs::File, io::BufWriter};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut input = SeekableSource::new(File::open("paper.caj")?)?;
    let mut output = BufWriter::new(File::create("paper.pdf")?);
    let report = convert(
        &mut input,
        &mut output,
        ConversionOptions::default(),
        &Limits::default(),
        &mut NeverCancel,
    )?;
    println!("{} pages, {} bookmarks", report.pages_converted, report.bookmarks_written);
    Ok(())
}

How to get the format and page count of a CAJ file#

inspect reads only what it needs:

Rust
use caj2pdf_core::{inspect, native::SeekableSource, InspectOptions, Limits, NeverCancel};
use std::fs::File;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut input = SeekableSource::new(File::open("paper.caj")?)?;
    let options = InspectOptions { bookmarks: true, ..Default::default() };
    let info = inspect(&mut input, &options, &Limits::default(), &mut NeverCancel)?;
    println!("format: {:?}", info.format);       // Pdf, Caj, Kdh, Nh, Hn, C8, Teb, Caa
    println!("pages: {:?}", info.page_count);
    println!("bookmarks: {:?}", info.bookmark_count);
    Ok(())
}

How to convert from the command line#

Shell
caj2pdf paper.caj                   # writes paper.pdf next to the input
caj2pdf paper.caj -o out.pdf        # --force replaces an existing file
caj2pdf inspect paper.caj --json    # format, page count, bookmarks

Tips#