Export route-D preview pages as PDF

This commit is contained in:
gloamxun committed 2026-10-20 21:41:46 +08:00
1 parent 8468c36daa
commit ec9ce3b765
5 files changed
+63 -5

No files matched your search

+21
View File
@@ -160,6 +160,27 @@ fn handle_request(
Err(error) => respond(request, 422, "application/json; charset=utf-8", &error),
}
}
(&Method::Post, "/api/route-d/pdf") => {
let bytes = match read_body_bytes(&mut request, MAX_SOURCE_BYTES) {
Ok(bytes) => bytes,
Err(error) => return respond(request, 413, "text/plain; charset=utf-8", &error),
};
let source = match String::from_utf8(bytes) {
Ok(source) => source,
Err(_) => {
return respond(
request,
400,
"text/plain; charset=utf-8",
"source must be UTF-8",
);
}
};
match route_d::pdf_bytes(&source) {
Ok(pdf) => respond_pdf(request, pdf),
Err(error) => respond(request, 422, "application/json; charset=utf-8", &error),
}
}
(&Method::Post, "/api/route-d/preview") => {
let bytes = match read_body_bytes(&mut request, MAX_SOURCE_BYTES) {
Ok(bytes) => bytes,
+19
View File
@@ -7,6 +7,7 @@
use std::cell::RefCell;
use std::fmt::Write as _;
use texrra_tex_core::pdf::display_lists_to_pdf;
use texrra_tex_core::{Document, Engine, ParagraphStyle, Unit};
const SCALE: i64 = 65_536;
@@ -25,6 +26,24 @@ pub fn preview_json(source: &str) -> Result<String, String> {
Ok(render_json(&engine, None))
}
/// Render the same checkpointed document as a route-D PDF.
pub fn pdf_bytes(source: &str) -> Result<Vec<u8>, String> {
let paragraphs = paragraphs(source)?;
let document = build_document(&paragraphs);
let engine = Engine::layout(&document);
let pages: Vec<_> = engine
.pages()
.iter()
.map(|page| {
let mut list = page.display_list();
list.width = list.width.max(PAGE_WIDTH_PT * SCALE);
list.height = list.height.max(PAGE_HEIGHT_PT * SCALE);
list
})
.collect();
Ok(display_lists_to_pdf(&pages, PAGE_HEIGHT_PT))
}
/// Apply one paragraph edit to the last route-D document and report page
/// checkpoints that were reused versus rebuilt.
pub fn edit_json(source: &str, unit: usize) -> Result<String, String> {