Pack print: A4 upright pages, small pictures at their size, card sides on one sheet
CI / secret scan (push) Successful in 6s
CI / fmt, clippy, tests (push) Successful in 43s
CI / release (push) Skipped

Every page of the pack is now A4 upright, so it prints as one stack. A
PDF page or a whole-page picture that lies wider than tall is turned
onto the page (a small one that fits as it is stays as it is), and a
PDF page is only made smaller, never larger.

A picture never prints larger than its resolution allows (the JFIF or
PNG dpi, else 150 dpi), nor larger than its kind's box: passport pages,
visas and stamps 180 x 128 mm, cards 130 x 90 mm, and a phone's
screenshot (the TDAC, or any picture about twice as tall as wide)
150 x 75 mm. These print at the top of the page.

Cards: a new kind "ID card", and the work permit, take two pictures in
one upload as front and back (migration 0008 documents.back_of). The
Documents tab marks them front/back, lists them together and offers
"Print both sides" (.../documents/{id}/sides.pdf); the pack prints both
on one sheet. The client link takes ID cards too. The demo seeds a TDAC
screenshot and an ID card's two sides.

Checks: fmt, clippy, 108 unit and 57 integration tests (print sizes,
dpi reading, the back following its front, the paired upload, its
audit and the sides PDF); the demo pack's pages checked as images.
The Documents tab's actions stack on a phone, so none is cut off.
This commit is contained in:
grabowski committed 2026-11-21 18:30:43 +07:00
1 parent 506221ecd0
commit 1f526f17f9
16 files changed
+724 -73

No files matched your search

+109 -4
View File
@@ -103,6 +103,8 @@ pub struct DocView {
pub sha256: String,
pub href: String,
pub edit: String,
/// A card's side: "front", "back" or empty.
pub side: &'static str,
}
/// A document next to its record (Overview, Visa and stay).
@@ -240,6 +242,7 @@ fn view(d: &Document, names: &HashMap<String, (String, String)>) -> DocView {
sha256: d.sha256.clone(),
href: format!("{}/{}/file", owner.base(), d.id),
edit: format!("{}/{}/edit", owner.base(), d.id),
side: if d.back_of.is_some() { "back" } else { "" },
}
}
@@ -248,10 +251,17 @@ pub async fn views(
owner: Owner,
names: &HashMap<String, (String, String)>,
) -> anyhow::Result<Vec<DocView>> {
Ok(documents::list(&st.db, owner)
.await?
let docs = documents::list(&st.db, owner).await?;
let fronts: HashSet<i64> = docs.iter().filter_map(|d| d.back_of).collect();
Ok(docs
.iter()
.map(|d| view(d, names))
.map(|d| {
let mut v = view(d, names);
if fronts.contains(&d.id) {
v.side = "front";
}
v
})
.collect())
}
@@ -403,6 +413,87 @@ async fn person_tab_page(
))
}
/// A card's front and back on one A4 page as a PDF, to print.
pub async fn sides(st: Arc<AppState>, user: User, owner: Owner, id: i64) -> Res {
let (Some(front), Some(name)) = (
documents::get(&st.db, owner, id).await?,
whose(&st, owner).await?,
) else {
return Ok(not_found_page(&st));
};
let Some(back) = documents::list(&st.db, owner)
.await?
.into_iter()
.find(|d| d.back_of == Some(id))
else {
return Ok(not_found_page(&st));
};
let Some(store) = st.files.clone() else {
return Ok(files_off(&st));
};
let mut loaded = Vec::new();
for d in [&front, &back] {
let bytes = match store.get(d.id).await {
Ok(b) if files::sha256_hex(&b) == d.sha256 => b,
Ok(_) | Err(_) => {
error!("document {}: missing or not as uploaded", d.id);
return Ok(unreadable(&st, owner));
}
};
loaded.push(crate::pack::pdf::Loaded {
name: d.file_name.clone(),
content_type: d.content_type.clone(),
kind: d.kind.clone(),
back_of: d.back_of,
bytes: Ok(bytes),
});
}
let (b, f) = (loaded.pop(), loaded.pop());
let (Some(f), Some(b)) = (f, b) else {
return Ok(not_found_page(&st));
};
let pdf = tokio::task::spawn_blocking(move || crate::pack::pdf::sides(f, b)).await??;
let fields = ["file".to_string()];
for d in [&front, &back] {
audit::record(
&st.db,
&Entry {
actor: &user.login,
action: Action::View,
entity: Entity::Document,
entity_id: Some(d.id),
person_id: owner.person(),
company_id: owner.company(),
fields: &fields,
},
)
.await?;
}
let file_name = format!(
"{name} - {} - both sides.pdf",
documents::kind_label(owner, &front.kind)
);
let mut resp = Response::new(Body::from(pdf));
let h = resp.headers_mut();
h.insert(
header::CONTENT_TYPE,
HeaderValue::from_static("application/pdf"),
);
h.insert(
header::CONTENT_DISPOSITION,
disposition("inline", &file_name),
);
h.insert(
header::CACHE_CONTROL,
HeaderValue::from_static("private, no-store"),
);
h.insert(
header::CONTENT_SECURITY_POLICY,
HeaderValue::from_static(FILE_CSP),
);
Ok(resp)
}
/// Documents are off: no files directory and key (local development).
fn files_off(st: &AppState) -> Response {
message(
@@ -668,6 +759,12 @@ pub(crate) async fn save(
checked: Vec<Checked>,
) -> anyhow::Result<i64> {
let fields = meta.set_fields();
// a card's two sides in one upload: the second picture is the back
let pair = documents::is_card(&meta.kind)
&& checked.len() == 2
&& checked.iter().all(|c| files::is_image(c.content_type));
let mut back_fields = fields.clone();
back_fields.push("back_of".into());
let mut tx = st.db.begin().await?;
let mut written: Vec<i64> = Vec::new();
let result: anyhow::Result<()> = async {
@@ -680,6 +777,13 @@ pub(crate) async fn save(
};
let id = documents::insert(&mut tx, owner, meta, &file, &user.login).await?;
store.put(id, f.bytes).await?;
let back = match written.first() {
Some(&front) if pair => {
documents::set_back(&mut tx, id, front).await?;
true
}
_ => false,
};
written.push(id);
audit::record(
&mut *tx,
@@ -690,7 +794,7 @@ pub(crate) async fn save(
entity_id: Some(id),
person_id: owner.person(),
company_id: owner.company(),
fields: &fields,
fields: if back { &back_fields } else { &fields },
},
)
.await?;
@@ -1089,6 +1193,7 @@ mod tests {
uploaded_by: "local".into(),
uploaded_at: at,
updated_at: at,
back_of: None,
};
// 20:00 UTC is the next day in Chiang Mai
assert_eq!(