Merge pull request #182 from MarekSpodBiedry/issue-141

Deliver Export as PDF as a browser download (#141)
This commit is contained in:
Brandon Thomas 2026-10-10 15:46:30 -04:00
commit cda29c5b73
2 changed files with 33 additions and 0 deletions

View File

@ -238,6 +238,11 @@ impl SheetApp {
}
let r = self.session.run(id, params);
self.after_engine();
if id == "file.exportPdf"
&& let Ok(r) = &r
{
self.deliver_pdf(r);
}
r
}
@ -258,6 +263,19 @@ impl SheetApp {
ctx.copy_text(text.to_string());
}
/// Web: `file.exportPdf` without a path returns the PDF as base64; hand it to the browser as a download.
fn deliver_pdf(&self, result: &Json) {
if result.get("path").is_some() {
return;
}
if let Some(dl) = &self.services.download
&& let Some(b) = result.get("base64").and_then(Json::as_str).and_then(gridcraft_engine::io::base64_decode)
{
let title = self.session.active().map(|d| d.display_title()).unwrap_or_else(|| "Book1".into());
dl(&std::path::Path::new(&title).with_extension("pdf").to_string_lossy(), &b);
}
}
/// Runs a command and shows its error in a message box (for menu/ribbon clicks).
pub fn run_or_alert(&mut self, id: &str, params: Json) {
if let Err(e) = self.run(id, params) {

View File

@ -74,3 +74,18 @@ fn save_actions_include_the_cell_draft_and_close_keeps_the_unsaved_prompt() {
assert_eq!(h.state().dialog.as_ref().map(|d| d.name()), Some("saveChanges"));
assert_eq!(h.state().session.active().unwrap().wb.active().unwrap().value(CellRef::new(0, 0)), Value::Number(123.0));
}
#[test]
fn export_as_pdf_is_delivered_as_a_download() {
let files = Arc::new(Mutex::new(Vec::<(String, Vec<u8>)>::new()));
let capture = files.clone();
let mut h = harness(Services {
download: Some(Box::new(move |name, bytes| capture.lock().unwrap().push((name.to_string(), bytes.to_vec())))),
..Default::default()
});
h.state_mut().run("file.exportPdf", serde_json::json!({})).unwrap();
let got = files.lock().unwrap();
assert_eq!(got.len(), 1, "Export as PDF hands a file to the browser");
assert!(got[0].0.ends_with(".pdf"));
assert!(got[0].1.starts_with(b"%PDF-"));
}