mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-29 17:55:45 +01:00
70 lines
2.9 KiB
Rust
70 lines
2.9 KiB
Rust
//! Printing in the browser.
|
|
|
|
use wasm_bindgen::JsCast;
|
|
|
|
/// The hidden iframe the last print loaded its PDF into.
|
|
const PRINT_FRAME_ID: &str = "craft-print-frame";
|
|
|
|
/// Prints `pdf` through one hidden iframe; the wasm `Spool` backend calls it.
|
|
///
|
|
/// The PDF opens as an object URL in a hidden iframe whose own `print()` runs once it has
|
|
/// loaded: the browser's print dialog on the PDF, without a download or a popup window the
|
|
/// browser might block. The previous print's iframe and object URL are removed first, so only
|
|
/// one copy of a document's PDF stays in the page (the last one may still be printing).
|
|
pub fn print_pdf(pdf: &[u8]) -> Result<(), String> {
|
|
let js = |e: wasm_bindgen::JsValue| format!("{e:?}");
|
|
let window = web_sys::window().ok_or("no window")?;
|
|
let document = window.document().ok_or("no document")?;
|
|
|
|
if let Some(old) = document.get_element_by_id(PRINT_FRAME_ID) {
|
|
if let Ok(frame) = old.clone().dyn_into::<web_sys::HtmlIFrameElement>() {
|
|
let src = frame.src();
|
|
if src.starts_with("blob:") {
|
|
web_sys::Url::revoke_object_url(&src).ok();
|
|
}
|
|
}
|
|
old.remove();
|
|
}
|
|
|
|
let parts = js_sys::Array::of1(&js_sys::Uint8Array::from(pdf));
|
|
let opts = web_sys::BlobPropertyBag::new();
|
|
opts.set_type("application/pdf");
|
|
let blob = web_sys::Blob::new_with_u8_array_sequence_and_options(&parts, &opts).map_err(js)?;
|
|
let url = web_sys::Url::create_object_url_with_blob(&blob).map_err(js)?;
|
|
|
|
let iframe: web_sys::HtmlIFrameElement =
|
|
match document.create_element("iframe").map_err(js).and_then(|e| e.dyn_into().map_err(|_| "not an iframe".to_string())) {
|
|
Ok(f) => f,
|
|
Err(e) => {
|
|
web_sys::Url::revoke_object_url(&url).ok();
|
|
return Err(e);
|
|
}
|
|
};
|
|
iframe.set_id(PRINT_FRAME_ID);
|
|
let attached = iframe
|
|
.style()
|
|
.set_property("display", "none")
|
|
.map_err(js)
|
|
.and_then(|()| document.body().ok_or_else(|| "no body".to_string()))
|
|
.and_then(|body| body.append_child(&iframe).map_err(js));
|
|
if let Err(e) = attached {
|
|
iframe.remove();
|
|
web_sys::Url::revoke_object_url(&url).ok();
|
|
return Err(e);
|
|
}
|
|
|
|
// `onload` fires once the PDF has rendered inside the iframe; printing before that shows a
|
|
// blank page. The handler is set before `src` so a fast or cached load can't fire unheard.
|
|
let iframe_for_load = iframe.clone();
|
|
let on_load = wasm_bindgen::closure::Closure::once_into_js(move || {
|
|
if let Some(w) = iframe_for_load.content_window() {
|
|
// Some browsers (Firefox with its built-in PDF viewer) print fine but report a
|
|
// benign cross-origin-style error back on this call.
|
|
let _ = w.print();
|
|
}
|
|
});
|
|
iframe.set_onload(Some(on_load.unchecked_ref()));
|
|
iframe.set_src(&url);
|
|
Ok(())
|
|
}
|