caj2pdf-core
| Language | Rust (1.88+) |
|---|---|
| License | MIT |
| Repository | github.com/rwv/caj2pdf-rust |
| Documentation | docs.rs/caj2pdf-core, CLI reference |
| Depends on | pure Rust; C8 and HN text pages need a font pack (bundled with the CLI) |
| Read / write | reads 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#
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#
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:
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#
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#
NeverCancelis the no-op progress and cancellation handler. Implement theProgresstrait to report progress or cancel long conversions.Limitscaps memory and I/O for untrusted files; the defaults suit normal theses.ConversionOptionscan disable bookmarks and allow damaged CAJ files (blank pages are substituted and listed inreport.omitted_pages).- HN-A pages are images: run OCR (for example
ocrmypdf) on the PDF to make it searchable.