//! An HTML folder set as a PDF (MARK-11). use std::path::Path; use ely_pdf_engine::Worker; fn worker() -> Worker { Worker::spawn_from(Path::new(env!("CARGO_BIN_EXE_ely-pdf-worker"))).expect("the worker") } /// A 1×1 red PNG. const DOT: &[u8] = &[ 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x02, 0x00, 0x00, 0x00, 0x90, 0x77, 0x53, 0xde, 0x00, 0x00, 0x00, 0x0c, 0x49, 0x44, 0x41, 0x54, 0x08, 0xd7, 0x63, 0xf8, 0xcf, 0xc0, 0x00, 0x00, 0x03, 0x01, 0x01, 0x00, 0x18, 0xdd, 0x8d, 0xb0, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82, ]; fn page(dir: &Path, body: &str) { let html = format!("
{body}"); std::fs::write(dir.join("page.html"), html).expect("writes"); } /// The PDF's pages and its first page's text. fn read(worker: &mut Worker, pdf: &Path) -> (usize, String) { let doc = worker.open(pdf).expect("opens").doc; let pages = worker.pages(doc).expect("pages"); let text: String = worker.text(doc, 0).expect("text").iter().map(|ch| ch.c).collect(); worker.close(doc).expect("closes"); (pages, text) } #[test] fn a_page_with_pictures_and_cjk_sets_as_a_pdf() { let dir = tempfile::tempdir().expect("a temp dir"); std::fs::create_dir(dir.path().join("files")).expect("makes"); std::fs::write(dir.path().join("files/dot.png"), DOT).expect("writes"); let svg = ""; page(dir.path(), &format!("中文段落 {svg}

Paragraph {n}.
")).collect(); page(dir.path(), &body); let pdf = dir.path().join("out.pdf"); let mut worker = worker(); worker.html_pdf(dir.path(), "page.html", &pdf).expect("sets"); assert!(read(&mut worker, &pdf).0 > 3); } #[test] fn a_missing_picture_fails_and_writes_nothing() { let dir = tempfile::tempdir().expect("a temp dir"); page(dir.path(), "
Before
{svg}After
")); let pdf = dir.path().join("out.pdf"); let mut worker = worker(); worker.html_pdf(dir.path(), "page.html", &pdf).expect("sets"); assert!(read(&mut worker, &pdf).0 >= 1); }