Skip to content

A fast, helpful, and open-source document parser

fix: improve rendering for forms nesting

Logan Markewich00eacc3069f3
diff --git a/crates/liteparse/src/extract.rs b/crates/liteparse/src/extract.rs
index acd8b3a..2bbdf08 100644
--- a/crates/liteparse/src/extract.rs
+++ b/crates/liteparse/src/extract.rs
@@ -151,7 +151,9 @@ pub fn extract_pages_and_images(
let mut flattened_page_numbers: Vec<u32> = Vec::new();
// One FFI call keeps the per-page annotation walk off the hot path for
// every document without an AcroForm catalog, which is nearly all of them.
- let document_has_form = document.form_type() != 0;
+ // Read at load, not now: an earlier `extract` call on this document may have
+ // flattened a page, which drops `/AcroForm` for the whole document.
+ let document_has_form = document.form_type_at_load() != 0;
let form_environment = output_options
.extract_form_fields
.then(|| document.form_environment())
diff --git a/crates/liteparse/tests/stages_compose.rs b/crates/liteparse/tests/stages_compose.rs
index b85a819..bdf3a3f 100644
--- a/crates/liteparse/tests/stages_compose.rs
+++ b/crates/liteparse/tests/stages_compose.rs
@@ -696,3 +696,164 @@ async fn extracted_pages_round_trip_keeps_internal_fields() {
}
}
}
+
+/// `extract` over a page subset must give each page what one call over the whole
+/// document gives it, on the same open document, so a caller can extract page by
+/// page (or batch by batch) without reopening. Extraction mutates the document
+/// (widget flattening), so this is not automatic; see
+/// `widget_text_survives_extracting_an_earlier_page_first` for the case that broke.
+#[tokio::test]
+#[serial]
+async fn extract_page_by_page_equals_one_call_on_one_document() {
+ if skip_integration() {
+ return;
+ }
+ let parser = LiteParse::new(LiteParseConfig {
+ output_format: OutputFormat::Markdown,
+ ..LiteParseConfig::default()
+ });
+ let lib = Library::init();
+ let mut flattened_pages = 0;
+ for pdf in corpus_pdfs() {
+ let input = PdfInput::Path(pdf.clone());
+ let Ok(document) = stages::open(&lib, &input, None, &[]) else {
+ continue;
+ };
+ let Ok(whole) = stages::extract(&document, &parser.extract_request(None, usize::MAX))
+ else {
+ continue;
+ };
+ flattened_pages += whole.flattened_page_numbers.len();
+ drop(document);
+ let document = stages::open(&lib, &input, None, &[]).unwrap();
+ for expected in &whole.pages {
+ let target = [expected.page_number as u32];
+ let one = stages::extract(
+ &document,
+ &parser.extract_request(Some(&target), usize::MAX),
+ )
+ .unwrap();
+ assert_eq!(
+ serde_json::to_value(&one.pages[0]).unwrap(),
+ serde_json::to_value(expected).unwrap(),
+ "{pdf} page {}: extracted alone differs from the whole-document call",
+ expected.page_number
+ );
+ }
+ }
+ assert!(
+ flattened_pages > 1,
+ "the corpus should have a document with several flattened form pages"
+ );
+}
+
+/// Two pages, each with a text widget painting a running header, where the form's
+/// only listed field is a signature on page 1: the header widgets are not in
+/// `/Fields`. This is how a 2009 government gazette in the platform's corpus is
+/// built. Flattening page 1 removes the signature field, and pdfium then drops the
+/// empty `/AcroForm`, so the document's form type reads 0 from then on.
+fn signed_form_with_orphan_header_widgets_pdf() -> Vec<u8> {
+ use lopdf::{Document, Object, Stream, StringFormat, dictionary};
+ let mut doc = Document::with_version("1.7");
+ let pages_id = doc.new_object_id();
+ let font_id = doc.add_object(dictionary! {
+ "Type" => "Font", "Subtype" => "Type1", "BaseFont" => "Helvetica",
+ });
+ let mut page_ids = Vec::new();
+ let mut signature_id = None;
+ for body in ["First page body", "Second page body"] {
+ let page_id = doc.new_object_id();
+ let appearance = doc.add_object(Stream::new(
+ dictionary! {
+ "Type" => "XObject", "Subtype" => "Form",
+ "BBox" => vec![0.into(), 0.into(), 200.into(), 20.into()],
+ "Resources" => dictionary! { "Font" => dictionary! { "Helv" => font_id } },
+ },
+ b"BT /Helv 12 Tf 2 5 Td (Running header) Tj ET".to_vec(),
+ ));
+ let header_id = doc.add_object(dictionary! {
+ "Type" => "Annot", "Subtype" => "Widget", "FT" => "Tx", "P" => page_id,
+ "T" => Object::String(b"header".to_vec(), StringFormat::Literal),
+ "V" => Object::String(b"Running header".to_vec(), StringFormat::Literal),
+ "Rect" => vec![50.into(), 750.into(), 250.into(), 770.into()], "F" => 4,
+ "AP" => dictionary! { "N" => appearance },
+ });
+ let mut annots = vec![Object::Reference(header_id)];
+ if signature_id.is_none() {
+ let id = doc.add_object(dictionary! {
+ "Type" => "Annot", "Subtype" => "Widget", "FT" => "Sig", "P" => page_id,
+ "T" => Object::String(b"Signature1".to_vec(), StringFormat::Literal),
+ "Rect" => vec![0.into(), 0.into(), 0.into(), 0.into()], "F" => 132,
+ });
+ annots.push(Object::Reference(id));
+ signature_id = Some(id);
+ }
+ let content = format!("BT /Helv 11 Tf 50 600 Td ({body}) Tj ET");
+ let content_id = doc.add_object(Stream::new(dictionary! {}, content.into_bytes()));
+ doc.objects.insert(
+ page_id,
+ Object::Dictionary(dictionary! {
+ "Type" => "Page", "Parent" => pages_id,
+ "MediaBox" => vec![0.into(), 0.into(), 612.into(), 792.into()],
+ "Resources" => dictionary! { "Font" => dictionary! { "Helv" => font_id } },
+ "Annots" => annots,
+ "Contents" => content_id,
+ }),
+ );
+ page_ids.push(page_id);
+ }
+ doc.objects.insert(
+ pages_id,
+ Object::Dictionary(dictionary! {
+ "Type" => "Pages",
+ "Kids" => page_ids.iter().map(|id| Object::Reference(*id)).collect::<Vec<_>>(),
+ "Count" => 2,
+ }),
+ );
+ let acroform_id = doc.add_object(dictionary! {
+ "Fields" => vec![Object::Reference(signature_id.expect("page 1 has the signature"))],
+ "SigFlags" => 3,
+ });
+ let catalog_id = doc.add_object(dictionary! {
+ "Type" => "Catalog", "Pages" => pages_id, "AcroForm" => acroform_id,
+ });
+ doc.trailer.set("Root", catalog_id);
+ let mut bytes = Vec::new();
+ doc.save_to(&mut bytes).unwrap();
+ bytes
+}
+
+#[tokio::test]
+#[serial]
+async fn widget_text_survives_extracting_an_earlier_page_first() {
+ let parser = LiteParse::new(LiteParseConfig {
+ output_format: OutputFormat::Markdown,
+ ..LiteParseConfig::default()
+ });
+ let lib = Library::init();
+ let input = PdfInput::Bytes(signed_form_with_orphan_header_widgets_pdf());
+ let document = stages::open(&lib, &input, None, &[]).unwrap();
+ let texts: Vec<String> = [1u32, 2]
+ .iter()
+ .map(|&n| {
+ let target = [n];
+ let extracted = stages::extract(
+ &document,
+ &parser.extract_request(Some(&target), usize::MAX),
+ )
+ .unwrap();
+ assert_eq!(
+ extracted.flattened_page_numbers,
+ [n],
+ "page {n} widgets flattened"
+ );
+ extracted.pages[0]
+ .text_items
+ .iter()
+ .map(|item| item.text.as_str())
+ .collect()
+ })
+ .collect();
+ assert!(texts[0].contains("Running header"), "page 1: {}", texts[0]);
+ assert!(texts[1].contains("Running header"), "page 2: {}", texts[1]);
+}
diff --git a/crates/pdfium/src/document.rs b/crates/pdfium/src/document.rs
index 0cf5b42..94ab03c 100644
--- a/crates/pdfium/src/document.rs
+++ b/crates/pdfium/src/document.rs
@@ -15,6 +15,10 @@ pub struct Document<'lib> {
/// PDFium ignores `/UserUnit`, so pages carry this multiplier and apply
/// it to all viewport-space geometry and rendering.
pub(crate) page_user_units: Vec<f32>,
+ /// `FPDF_GetFormType` when the document was loaded. Flattening any page's
+ /// widgets drops the catalog's `/AcroForm`, after which `form_type()` reads 0
+ /// for every page, including ones still carrying widgets.
+ pub(crate) form_type_at_load: i32,
pub(crate) _lib: std::marker::PhantomData<&'lib Library>,
}
@@ -165,6 +169,14 @@ impl<'lib> Document<'lib> {
unsafe { ffi!(FPDF_GetFormType(self.handle)) }
}
+ /// [`Self::form_type`] as it was when the document was loaded, before any
+ /// [`Self::flatten_form_widgets`] removed the catalog's `/AcroForm`. Use this
+ /// to decide whether pages may carry form widgets: after one page is flattened,
+ /// `form_type()` reads 0 while every other page still has its widgets.
+ pub fn form_type_at_load(&self) -> i32 {
+ self.form_type_at_load
+ }
+
/// Whether the catalog declares the document tagged (`/MarkInfo /Marked true`).
/// A structure tree can be present without this flag — residual or stale
/// tagging — so callers that treat the tree as authoritative gate on it.
@@ -263,6 +275,7 @@ impl<'lib> Document<'lib> {
}
let copy = Document {
handle,
+ form_type_at_load: 0,
page_user_units: self
.page_user_units
.get(index as usize)
diff --git a/crates/pdfium/src/library.rs b/crates/pdfium/src/library.rs
index 0ba8b31..6b3bb23 100644
--- a/crates/pdfium/src/library.rs
+++ b/crates/pdfium/src/library.rs
@@ -133,6 +133,7 @@ impl Library {
Ok(Document {
handle,
page_user_units,
+ form_type_at_load: unsafe { ffi!(FPDF_GetFormType(handle)) },
_lib: std::marker::PhantomData,
})
}
@@ -169,6 +170,7 @@ impl Library {
} else {
Self::page_user_units(handle, data)
},
+ form_type_at_load: unsafe { ffi!(FPDF_GetFormType(handle)) },
_lib: std::marker::PhantomData,
})
}