Skip to content

A fast, helpful, and open-source document parser

feat: add opt-in vector graphics extraction

Pierre-Loic doulcetb05adb9f7cca
diff --git a/README.md b/README.md
index 85887d4..c11ffda 100644
--- a/README.md
+++ b/README.md
@@ -159,6 +159,9 @@ lit parse document.pdf --target-pages "1-5,10,15-20"
# Parse without OCR
lit parse document.pdf --no-ocr
+# Include page-scoped vector path data in JSON
+lit parse document.pdf --format json --extract-vector-graphics
+
# Parse a remote PDF
curl -sL https://example.com/report.pdf | lit parse -
```
@@ -210,6 +213,25 @@ extraction; the default placeholder mode alone only discovers lightweight page r
> [LlamaParse](https://developers.llamaindex.ai/python/cloud/llamaparse/?utm_source=github&utm_medium=liteparse)
> remains the most accurate option.
+### Vector Graphics
+
+Vector path output is opt-in because path-heavy PDFs can produce large payloads.
+Enable it with `--extract-vector-graphics`, Rust/Python
+`extract_vector_graphics = true`, or JavaScript/WASM
+`extractVectorGraphics: true`. Each page then includes `vector_graphics`
+(`vectorGraphics` in JavaScript) with:
+
+- `shapes`: path bounding box, stroke/fill paint state and ARGB colors, and
+ whether the path contains a Bezier curve.
+- `lines`: compatible horizontal/vertical segments merged using stroke width
+ and paint colors, with top-left 72-DPI viewport coordinates.
+
+The representation follows LlamaParse PDFium path extraction; LiteParse calls
+the shape rectangle `bbox` rather than PDFium's `coords`, and uses `width` /
+`height` rather than `w` / `h`. The field is absent (or `None`/`undefined`) by
+default. Diagonal and curved segments are represented by their parent shape but
+are not emitted as lines.
+
### Check Complexity
Before committing to a full parse, check whether a document actually needs OCR or
@@ -274,6 +296,7 @@ Options:
--dpi <dpi> Rendering DPI [default: 150]
--image-mode <mode> Markdown image handling: off|placeholder|embed [default: placeholder]
--image-output-dir <dir> Where to write images when --image-mode embed
+ --extract-vector-graphics Include page vector shapes and merged H/V lines
--no-links Emit link anchor text as plain text (no [text](url)) in markdown
--preserve-small-text Keep very small text
--password <password> Password for encrypted documents
diff --git a/crates/liteparse-napi/src/types.rs b/crates/liteparse-napi/src/types.rs
index aebff63..bc9317b 100644
--- a/crates/liteparse-napi/src/types.rs
+++ b/crates/liteparse-napi/src/types.rs
@@ -4,7 +4,9 @@ use napi_derive::napi;
use liteparse::config::{CropBox, ImageMode, LiteParseConfig, OutputFormat};
use liteparse::parser::ParseResult;
-use liteparse::types::{GraphicPrimitive, Page, ParsedPage, Rect, TextItem, WordBox};
+use liteparse::types::{
+ GraphicPrimitive, Page, ParsedPage, Rect, TextItem, VectorGraphics, WordBox,
+};
// ---------------------------------------------------------------------------
// Config
@@ -77,6 +79,8 @@ pub struct JsLiteParseConfig {
/// page as `ParsedPage.complexity` (the same signals `isComplex` returns).
/// Default false; enabling it runs an extra vector-text detection pass.
pub include_complexity: Option<bool>,
+ /// Expose page-scoped vector path extraction. Default false.
+ pub extract_vector_graphics: Option<bool>,
}
/// A page sub-region as the fraction cropped from each side (top-left origin,
@@ -178,6 +182,9 @@ impl JsLiteParseConfig {
if let Some(v) = self.include_complexity {
cfg.include_complexity = v;
}
+ if let Some(v) = self.extract_vector_graphics {
+ cfg.extract_vector_graphics = v;
+ }
cfg
}
@@ -229,6 +236,7 @@ impl JsLiteParseConfig {
}),
skip_diagonal_text: Some(cfg.skip_diagonal_text),
include_complexity: Some(cfg.include_complexity),
+ extract_vector_graphics: Some(cfg.extract_vector_graphics),
}
}
}
@@ -466,6 +474,7 @@ impl JsPageInput {
.as_ref()
.map(|gs| gs.iter().filter_map(JsGraphic::to_rust).collect())
.unwrap_or_default(),
+ vector_graphics: None,
struct_nodes: Vec::new(),
image_refs: Vec::new(),
}
@@ -486,6 +495,87 @@ pub struct JsParsedPage {
pub markdown: String,
pub text_items: Vec<JsTextItem>,
pub complexity: Option<JsPageComplexityStats>,
+ pub vector_graphics: Option<JsVectorGraphics>,
+}
+
+#[napi(object)]
+#[derive(Clone)]
+pub struct JsVectorShape {
+ pub bbox: JsRect,
+ pub stroke: bool,
+ pub stroke_color: Option<String>,
+ pub fill: bool,
+ pub fill_color: Option<String>,
+ pub has_curve: bool,
+}
+
+#[napi(object)]
+#[derive(Clone)]
+pub struct JsRect {
+ pub x: f64,
+ pub y: f64,
+ pub width: f64,
+ pub height: f64,
+}
+
+#[napi(object)]
+#[derive(Clone)]
+pub struct JsVectorLine {
+ pub x1: f64,
+ pub y1: f64,
+ pub x2: f64,
+ pub y2: f64,
+ pub stroke: bool,
+ pub stroke_width: Option<f64>,
+ pub stroke_color: Option<String>,
+ pub fill: bool,
+ pub fill_color: Option<String>,
+}
+
+#[napi(object)]
+#[derive(Clone)]
+pub struct JsVectorGraphics {
+ pub shapes: Vec<JsVectorShape>,
+ pub lines: Vec<JsVectorLine>,
+}
+
+impl JsVectorGraphics {
+ fn from_rust(value: &VectorGraphics) -> Self {
+ Self {
+ shapes: value
+ .shapes
+ .iter()
+ .map(|s| JsVectorShape {
+ bbox: JsRect {
+ x: s.bbox.x as f64,
+ y: s.bbox.y as f64,
+ width: s.bbox.width as f64,
+ height: s.bbox.height as f64,
+ },
+ stroke: s.stroke,
+ stroke_color: s.stroke_color.clone(),
+ fill: s.fill,
+ fill_color: s.fill_color.clone(),
+ has_curve: s.has_curve,
+ })
+ .collect(),
+ lines: value
+ .lines
+ .iter()
+ .map(|l| JsVectorLine {
+ x1: l.x1 as f64,
+ y1: l.y1 as f64,
+ x2: l.x2 as f64,
+ y2: l.y2 as f64,
+ stroke: l.stroke,
+ stroke_width: l.stroke_width.map(f64::from),
+ stroke_color: l.stroke_color.clone(),
+ fill: l.fill,
+ fill_color: l.fill_color.clone(),
+ })
+ .collect(),
+ }
+ }
}
impl JsParsedPage {
@@ -505,6 +595,10 @@ impl JsParsedPage {
.complexity
.as_ref()
.map(JsPageComplexityStats::from_rust),
+ vector_graphics: page
+ .vector_graphics
+ .as_ref()
+ .map(JsVectorGraphics::from_rust),
}
}
}
@@ -728,4 +822,38 @@ mod tests {
js.include_text_metadata = Some(true);
assert!(js.into_rust().include_text_metadata);
}
+
+ #[test]
+ fn converts_vector_graphics_to_js_shape() {
+ let rust = VectorGraphics {
+ shapes: vec![liteparse::types::VectorShape {
+ bbox: Rect {
+ x: 1.0,
+ y: 2.0,
+ width: 3.0,
+ height: 4.0,
+ },
+ stroke: true,
+ stroke_color: Some("ff112233".into()),
+ fill: false,
+ fill_color: None,
+ has_curve: true,
+ }],
+ lines: vec![liteparse::types::VectorLine {
+ x1: 1.0,
+ y1: 2.0,
+ x2: 3.0,
+ y2: 2.0,
+ stroke: true,
+ stroke_width: Some(0.5),
+ stroke_color: Some("ff112233".into()),
+ fill: false,
+ fill_color: None,
+ }],
+ };
+ let js = JsVectorGraphics::from_rust(&rust);
+ assert_eq!(js.shapes[0].bbox.width, 3.0);
+ assert!(js.shapes[0].has_curve);
+ assert_eq!(js.lines[0].stroke_width, Some(0.5));
+ }
}
diff --git a/crates/liteparse-python/src/cli.rs b/crates/liteparse-python/src/cli.rs
index 5d92bea..9dab222 100644
--- a/crates/liteparse-python/src/cli.rs
+++ b/crates/liteparse-python/src/cli.rs
@@ -80,10 +80,12 @@ struct ParseCommand {
/// page of JSON output. Off by default.
#[arg(long)]
complexity: bool,
-
/// Include rich PDF text metadata in text items and JSON output.
#[arg(long)]
include_text_metadata: bool,
+ /// Include page-scoped vector shapes and merged horizontal/vertical lines.
+ #[arg(long)]
+ extract_vector_graphics: bool,
}
#[derive(Args, Debug)]
@@ -155,12 +157,14 @@ struct BatchParseCommand {
/// page of JSON output. Off by default.
#[arg(long)]
complexity: bool,
-
/// Include rich PDF text metadata in text items and JSON output.
#[arg(long)]
include_text_metadata: bool,
#[arg(long)]
extract_images: bool,
+ /// Include page-scoped vector shapes and merged horizontal/vertical lines.
+ #[arg(long)]
+ extract_vector_graphics: bool,
}
/// Parse a `Name: Value` header string into a `(name, value)` pair.
@@ -242,6 +246,7 @@ pub fn run_cli(args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
extract_links: !cmd.no_links,
include_complexity: cmd.complexity,
include_text_metadata: cmd.include_text_metadata,
+ extract_vector_graphics: cmd.extract_vector_graphics,
..Default::default()
};
if let Some(n) = cmd.num_workers {
@@ -333,6 +338,7 @@ pub fn run_cli(args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
include_complexity: cmd.complexity,
include_text_metadata: cmd.include_text_metadata,
extract_images: cmd.extract_images,
+ extract_vector_graphics: cmd.extract_vector_graphics,
..Default::default()
};
if let Some(n) = cmd.num_workers {
diff --git a/crates/liteparse-python/src/lib.rs b/crates/liteparse-python/src/lib.rs
index 0e17862..7ea5678 100644
--- a/crates/liteparse-python/src/lib.rs
+++ b/crates/liteparse-python/src/lib.rs
@@ -184,6 +184,109 @@ struct PyParsedPage {
text_items: Vec<PyTextItem>,
#[pyo3(get)]
complexity: Option<PyPageComplexityStats>,
+ #[pyo3(get)]
+ vector_graphics: Option<PyVectorGraphics>,
+}
+
+#[pyclass(frozen, from_py_object)]
+#[derive(Clone)]
+struct PyRect {
+ #[pyo3(get)]
+ x: f64,
+ #[pyo3(get)]
+ y: f64,
+ #[pyo3(get)]
+ width: f64,
+ #[pyo3(get)]
+ height: f64,
+}
+
+#[pyclass(frozen, from_py_object)]
+#[derive(Clone)]
+struct PyVectorShape {
+ #[pyo3(get)]
+ bbox: PyRect,
+ #[pyo3(get)]
+ stroke: bool,
+ #[pyo3(get)]
+ stroke_color: Option<String>,
+ #[pyo3(get)]
+ fill: bool,
+ #[pyo3(get)]
+ fill_color: Option<String>,
+ #[pyo3(get)]
+ has_curve: bool,
+}
+
+#[pyclass(frozen, from_py_object)]
+#[derive(Clone)]
+struct PyVectorLine {
+ #[pyo3(get)]
+ x1: f64,
+ #[pyo3(get)]
+ y1: f64,
+ #[pyo3(get)]
+ x2: f64,
+ #[pyo3(get)]
+ y2: f64,
+ #[pyo3(get)]
+ stroke: bool,
+ #[pyo3(get)]
+ stroke_width: Option<f64>,
+ #[pyo3(get)]
+ stroke_color: Option<String>,
+ #[pyo3(get)]
+ fill: bool,
+ #[pyo3(get)]
+ fill_color: Option<String>,
+}
+
+#[pyclass(frozen, from_py_object)]
+#[derive(Clone)]
+struct PyVectorGraphics {
+ #[pyo3(get)]
+ shapes: Vec<PyVectorShape>,
+ #[pyo3(get)]
+ lines: Vec<PyVectorLine>,
+}
+
+impl PyVectorGraphics {
+ fn from_rust(v: liteparse::types::VectorGraphics) -> Self {
+ Self {
+ shapes: v
+ .shapes
+ .into_iter()
+ .map(|s| PyVectorShape {
+ bbox: PyRect {
+ x: s.bbox.x as f64,
+ y: s.bbox.y as f64,
+ width: s.bbox.width as f64,
+ height: s.bbox.height as f64,
+ },
+ stroke: s.stroke,
+ stroke_color: s.stroke_color,
+ fill: s.fill,
+ fill_color: s.fill_color,
+ has_curve: s.has_curve,
+ })
+ .collect(),
+ lines: v
+ .lines
+ .into_iter()
+ .map(|l| PyVectorLine {
+ x1: l.x1 as f64,
+ y1: l.y1 as f64,
+ x2: l.x2 as f64,
+ y2: l.y2 as f64,
+ stroke: l.stroke,
+ stroke_width: l.stroke_width.map(f64::from),
+ stroke_color: l.stroke_color,
+ fill: l.fill,
+ fill_color: l.fill_color,
+ })
+ .collect(),
+ }
+ }
}
#[pymethods]
@@ -216,6 +319,7 @@ impl PyParsedPage {
.complexity
.as_ref()
.map(PyPageComplexityStats::from_rust),
+ vector_graphics: page.vector_graphics.map(PyVectorGraphics::from_rust),
}
}
}
@@ -556,6 +660,8 @@ struct PyLiteParseConfig {
image_output_dir: Option<String>,
#[pyo3(get)]
extract_images: bool,
+ #[pyo3(get)]
+ extract_vector_graphics: bool,
}
#[pymethods]
@@ -610,6 +716,7 @@ impl PyLiteParseConfig {
include_text_metadata: cfg.include_text_metadata,
image_output_dir: cfg.image_output_dir.clone(),
extract_images: cfg.extract_images,
+ extract_vector_graphics: cfg.extract_vector_graphics,
}
}
}
@@ -654,6 +761,7 @@ impl LiteParse {
crop_box = None,
skip_diagonal_text = None,
include_complexity = None,
+ extract_vector_graphics = None,
))]
fn new(
ocr_language: Option<String>,
@@ -680,6 +788,7 @@ impl LiteParse {
crop_box: Option<(f32, f32, f32, f32)>,
skip_diagonal_text: Option<bool>,
include_complexity: Option<bool>,
+ extract_vector_graphics: Option<bool>,
) -> PyResult<Self> {
let mut cfg = LiteParseConfig::default();
if let Some(v) = ocr_language {
@@ -767,6 +876,9 @@ impl LiteParse {
if let Some(v) = include_complexity {
cfg.include_complexity = v;
}
+ if let Some(v) = extract_vector_graphics {
+ cfg.extract_vector_graphics = v;
+ }
let inner = liteparse::parser::LiteParse::new(cfg.clone());
let runtime = tokio::runtime::Runtime::new()
@@ -944,6 +1056,10 @@ fn _liteparse(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<PyScreenshotResult>()?;
m.add_class::<PyPageComplexityStats>()?;
m.add_class::<PyLayoutComplexityStats>()?;
+ m.add_class::<PyRect>()?;
+ m.add_class::<PyVectorShape>()?;
+ m.add_class::<PyVectorLine>()?;
+ m.add_class::<PyVectorGraphics>()?;
m.add_function(wrap_pyfunction!(run_cli, m)?)?;
m.add_function(wrap_pyfunction!(search_items, m)?)?;
Ok(())
@@ -1001,4 +1117,27 @@ mod tests {
let py = PyLiteParseConfig::from_rust(&config);
assert!(py.include_text_metadata);
}
+
+ #[test]
+ fn converts_vector_graphics_to_python_shape() {
+ let rust = liteparse::types::VectorGraphics {
+ shapes: vec![liteparse::types::VectorShape {
+ bbox: liteparse::types::Rect {
+ x: 1.0,
+ y: 2.0,
+ width: 3.0,
+ height: 4.0,
+ },
+ stroke: false,
+ stroke_color: None,
+ fill: true,
+ fill_color: Some("ffffffff".into()),
+ has_curve: false,
+ }],
+ lines: vec![],
+ };
+ let py = PyVectorGraphics::from_rust(rust);
+ assert_eq!(py.shapes[0].bbox.height, 4.0);
+ assert_eq!(py.shapes[0].fill_color.as_deref(), Some("ffffffff"));
+ }
}
diff --git a/crates/liteparse-wasm/src/lib.rs b/crates/liteparse-wasm/src/lib.rs
index d841785..a137845 100644
--- a/crates/liteparse-wasm/src/lib.rs
+++ b/crates/liteparse-wasm/src/lib.rs
@@ -73,6 +73,8 @@ pub struct LiteParseConfig {
/// page as `ParsedPage.complexity` (the same signals `isComplex` returns).
/// Default false; enabling it runs an extra vector-text detection pass.
include_complexity: Option<bool>,
+ /// Expose page-scoped vector shapes and merged H/V line segments.
+ extract_vector_graphics: Option<bool>,
}
/// A page sub-region as the fraction cropped from each side (top-left origin,
@@ -172,6 +174,9 @@ impl LiteParseConfig {
if let Some(v) = self.include_complexity {
cfg.include_complexity = v;
}
+ if let Some(v) = self.extract_vector_graphics {
+ cfg.extract_vector_graphics = v;
+ }
cfg.num_workers = 1;
Ok(cfg)
}
@@ -216,6 +221,7 @@ impl LiteParseConfig {
}),
skip_diagonal_text: Some(cfg.skip_diagonal_text),
include_complexity: Some(cfg.include_complexity),
+ extract_vector_graphics: Some(cfg.extract_vector_graphics),
}
}
}
@@ -270,6 +276,53 @@ pub struct ParsedPage {
pub text_items: Vec<TextItem>,
#[serde(skip_serializing_if = "Option::is_none")]
pub complexity: Option<PageComplexityStats>,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub vector_graphics: Option<VectorGraphics>,
+}
+
+#[derive(Serialize, Tsify)]
+#[tsify(into_wasm_abi)]
+#[serde(rename_all = "camelCase")]
+pub struct VectorGraphics {
+ pub shapes: Vec<VectorShape>,
+ pub lines: Vec<VectorLine>,
+}
+
+#[derive(Serialize, Tsify)]
+#[tsify(into_wasm_abi)]
+#[serde(rename_all = "camelCase")]
+pub struct VectorShape {
+ pub bbox: VectorRect,
+ pub stroke: bool,
+ pub stroke_color: Option<String>,
+ pub fill: bool,
+ pub fill_color: Option<String>,
+ pub has_curve: bool,
+}
+
+#[derive(Serialize, Tsify)]
+#[tsify(into_wasm_abi)]
+#[serde(rename_all = "camelCase")]
+pub struct VectorRect {
+ pub x: f32,
+ pub y: f32,
+ pub width: f32,
+ pub height: f32,
+}
+
+#[derive(Serialize, Tsify)]
+#[tsify(into_wasm_abi)]
+#[serde(rename_all = "camelCase")]
+pub struct VectorLine {
+ pub x1: f32,
+ pub y1: f32,
+ pub x2: f32,
+ pub y2: f32,
+ pub stroke: bool,
+ pub stroke_width: Option<f32>,
+ pub stroke_color: Option<String>,
+ pub fill: bool,
+ pub fill_color: Option<String>,
}
#[derive(Serialize, Tsify)]
@@ -526,6 +579,40 @@ impl LiteParse {
})
.collect(),
complexity: p.complexity.as_ref().map(PageComplexityStats::from_rust),
+ vector_graphics: p.vector_graphics.as_ref().map(|v| VectorGraphics {
+ shapes: v
+ .shapes
+ .iter()
+ .map(|s| VectorShape {
+ bbox: VectorRect {
+ x: s.bbox.x,
+ y: s.bbox.y,
+ width: s.bbox.width,
+ height: s.bbox.height,
+ },
+ stroke: s.stroke,
+ stroke_color: s.stroke_color.clone(),
+ fill: s.fill,
+ fill_color: s.fill_color.clone(),
+ has_curve: s.has_curve,
+ })
+ .collect(),
+ lines: v
+ .lines
+ .iter()
+ .map(|l| VectorLine {
+ x1: l.x1,
+ y1: l.y1,
+ x2: l.x2,
+ y2: l.y2,
+ stroke: l.stroke,
+ stroke_width: l.stroke_width,
+ stroke_color: l.stroke_color.clone(),
+ fill: l.fill,
+ fill_color: l.fill_color.clone(),
+ })
+ .collect(),
+ }),
})
.collect();
diff --git a/crates/liteparse/src/config.rs b/crates/liteparse/src/config.rs
index bb90af7..d8ae2e2 100644
--- a/crates/liteparse/src/config.rs
+++ b/crates/liteparse/src/config.rs
@@ -90,6 +90,10 @@ pub struct LiteParseConfig {
/// These are the same signals the standalone `is_complex` API returns.
/// Default `false`.
pub include_complexity: bool,
+ /// Expose page-scoped vector path data (`shapes` and merged horizontal /
+ /// vertical `lines`) in parse results. Default `false`; path objects are
+ /// still inspected internally for layout detection when disabled.
+ pub extract_vector_graphics: bool,
}
/// A page sub-region expressed as the fraction cropped from each side.
@@ -167,6 +171,7 @@ impl Default for LiteParseConfig {
crop_box: None,
skip_diagonal_text: false,
include_complexity: false,
+ extract_vector_graphics: false,
}
}
}
diff --git a/crates/liteparse/src/extract.rs b/crates/liteparse/src/extract.rs
index f5ea9ed..7ea49b3 100644
--- a/crates/liteparse/src/extract.rs
+++ b/crates/liteparse/src/extract.rs
@@ -2,7 +2,7 @@ use crate::error::LiteParseError;
use crate::glyph_names::resolve_glyph_name;
use crate::types::{
ExtractedImage, GraphicPrimitive, ImageRef, OutlineTarget, Page as LitePage, PdfInput, Rect,
- StructNode, TextItem, WordBox,
+ StructNode, TextItem, VectorGraphics, VectorLine, VectorShape, WordBox,
};
use image::ImageEncoder;
use pdfium::{
@@ -49,7 +49,16 @@ pub(crate) fn extract_pages_from_document(
target_pages: Option<&[u32]>,
max_pages: usize,
) -> Result<Vec<LitePage>, LiteParseError> {
- Ok(extract_pages_and_images(document, target_pages, max_pages, false, false, None, false)?.0)
+ Ok(extract_pages_and_images(
+ document,
+ target_pages,
+ max_pages,
+ false,
+ false,
+ None,
+ ExtractionOutputOptions::default(),
+ )?
+ .0)
}
/// Same as `extract_pages_from_document` but optionally also renders every
@@ -64,7 +73,7 @@ pub(crate) fn extract_pages_and_images(
render_images: bool,
extract_links: bool,
glyph_resolver: Option<&dyn crate::GlyphResolver>,
- emit_word_boxes: bool,
+ output_options: ExtractionOutputOptions,
) -> Result<(Vec<LitePage>, Vec<ExtractedImage>, u32), LiteParseError> {
let page_count = document.page_count();
let mut pages = Vec::new();
@@ -98,12 +107,16 @@ pub(crate) fn extract_pages_and_images(
&text_page,
&view_box,
glyph_resolver,
- emit_word_boxes,
+ output_options.emit_word_boxes,
)?;
if extract_links {
assign_links(&mut text_items, &page.links(&view_box));
}
- let graphics = extract_page_graphics(&page, &view_box);
+ let paths = page.path_objects(&view_box);
+ let graphics = extract_layout_graphics(&paths);
+ let vector_graphics = output_options
+ .extract_vector_graphics
+ .then(|| build_vector_graphics(&paths));
assign_strikethrough(&mut text_items, &graphics);
let struct_nodes = extract_page_struct_nodes(&page, &view_box);
let extracted_refs = extract_page_image_refs(&page, page_number, render_images);
@@ -126,6 +139,7 @@ pub(crate) fn extract_pages_and_images(
page_height: page.height(),
text_items,
graphics,
+ vector_graphics,
struct_nodes,
image_refs,
});
@@ -134,6 +148,12 @@ pub(crate) fn extract_pages_and_images(
Ok((pages, images, image_error_count))
}
+#[derive(Debug, Clone, Copy, Default)]
+pub(crate) struct ExtractionOutputOptions {
+ pub emit_word_boxes: bool,
+ pub extract_vector_graphics: bool,
+}
+
/// Assign hyperlink URIs to text items whose bbox center falls inside a link
/// annotation's rectangle. Both the item bbox and the link rect are in
/// viewport space. First matching link wins.
@@ -535,11 +555,10 @@ fn extract_page_image_refs(
///
/// BezierTo segments don't emit strokes (we just advance the current point so
/// later LineTos start from the right place).
-fn extract_page_graphics(page: &Page, view_box: &RectF) -> Vec<GraphicPrimitive> {
- let paths: Vec<PathObject> = page.path_objects(view_box);
+fn extract_layout_graphics(paths: &[PathObject]) -> Vec<GraphicPrimitive> {
let mut out = Vec::new();
- for path in &paths {
+ for path in paths {
// Filled paths: emit one Rect for the full bbox. Cheap signal for
// cell backgrounds / figure clusters / code-block fills.
if path.is_filled {
@@ -602,6 +621,263 @@ fn extract_page_graphics(page: &Page, view_box: &RectF) -> Vec<GraphicPrimitive>
out
}
+const PATH_EPSILON: f32 = 0.001;
+const LINE_AXIS_TOLERANCE: f32 = 1.0;
+
+#[derive(Clone)]
+struct LineCandidate {
+ line: VectorLine,
+ shape_index: usize,
+}
+
+fn build_vector_graphics(paths: &[PathObject]) -> VectorGraphics {
+ let painted: Vec<(usize, &PathObject)> = paths
+ .iter()
+ .enumerate()
+ .filter(|(_, path)| path.is_stroked || path.is_filled)
+ .collect();
+ let raw_shapes: Vec<VectorShape> = painted
+ .iter()
+ .map(|(_, path)| VectorShape {
+ bbox: rectf_to_rect(&path.bbox),
+ stroke: path.is_stroked,
+ stroke_color: path
+ .is_stroked
+ .then(|| path.stroke_color.as_ref().map(color_to_argb_hex))
+ .flatten(),
+ fill: path.is_filled,
+ fill_color: path
+ .is_filled
+ .then(|| path.fill_color.as_ref().map(color_to_argb_hex))
+ .flatten(),
+ has_curve: path
+ .segments
+ .iter()
+ .any(|s| s.kind == SegmentKind::BezierTo),
+ })
+ .collect();
+
+ // LlamaParse collapses consecutively drawn, same-color solid fills when
+ // one contains the other. Preserve order because intervening paint
+ // operations intentionally stop merging.
+ let mut keep = vec![true; raw_shapes.len()];
+ for i in 0..raw_shapes.len().saturating_sub(1) {
+ if !keep[i] || !mergeable_shape(&raw_shapes[i]) {
+ continue;
+ }
+ for j in i + 1..raw_shapes.len() {
+ if !keep[j] {
+ continue;
+ }
+ if !mergeable_shape(&raw_shapes[j])
+ || raw_shapes[i].fill_color != raw_shapes[j].fill_color
+ {
+ break;
+ }
+ if rect_contains(&raw_shapes[i].bbox, &raw_shapes[j].bbox) {
+ keep[j] = false;
+ } else if rect_contains(&raw_shapes[j].bbox, &raw_shapes[i].bbox) {
+ keep[i] = false;
+ } else {
+ break;
+ }
+ }
+ }
+ let shapes = raw_shapes
+ .iter()
+ .cloned()
+ .zip(keep.iter().copied())
+ .filter_map(|(shape, retained)| retained.then_some(shape))
+ .collect();
+
+ let mut horizontal = Vec::new();
+ let mut vertical = Vec::new();
+ for (shape_index, (_, path)) in painted.iter().enumerate() {
+ let mut current = None;
+ for segment in &path.segments {
+ match segment.kind {
+ SegmentKind::MoveTo => current = Some((segment.x, segment.y)),
+ SegmentKind::BezierTo => current = Some((segment.x, segment.y)),
+ SegmentKind::LineTo => {
+ if let Some(from) = current {
+ push_axis_line(
+ path,
+ shape_index,
+ from,
+ (segment.x, segment.y),
+ &mut horizontal,
+ &mut vertical,
+ );
+ }
+ current = Some((segment.x, segment.y));
+ }
+ }
+ }
+ }
+ horizontal.sort_by(|a: &LineCandidate, b| {
+ a.line
+ .y1
+ .total_cmp(&b.line.y1)
+ .then(a.line.x1.total_cmp(&b.line.x1))
+ });
+ vertical.sort_by(|a: &LineCandidate, b| {
+ a.line
+ .x1
+ .total_cmp(&b.line.x1)
+ .then(a.line.y1.total_cmp(&b.line.y1))
+ });
+ let mut lines = merge_axis_lines(horizontal, true, &raw_shapes, &keep);
+ lines.extend(merge_axis_lines(vertical, false, &raw_shapes, &keep));
+ VectorGraphics { shapes, lines }
+}
+
+fn mergeable_shape(s: &VectorShape) -> bool {
+ s.fill && !s.stroke && s.fill_color.is_some()
+}
+fn rect_contains(a: &Rect, b: &Rect) -> bool {
+ a.x - PATH_EPSILON <= b.x + PATH_EPSILON
+ && a.y - PATH_EPSILON <= b.y + PATH_EPSILON
+ && a.x + a.width + PATH_EPSILON >= b.x + b.width - PATH_EPSILON
+ && a.y + a.height + PATH_EPSILON >= b.y + b.height - PATH_EPSILON
+}
+
+fn push_axis_line(
+ path: &PathObject,
+ shape_index: usize,
+ a: (f32, f32),
+ b: (f32, f32),
+ h: &mut Vec<LineCandidate>,
+ v: &mut Vec<LineCandidate>,
+) {
+ let (mut x1, mut y1, mut x2, mut y2) = (a.0, a.1, b.0, b.1);
+ let target = if (y2 - y1).abs() < LINE_AXIS_TOLERANCE {
+ if x2 < x1 {
+ std::mem::swap(&mut x1, &mut x2);
+ }
+ &mut *h
+ } else if (x2 - x1).abs() < LINE_AXIS_TOLERANCE {
+ if y2 < y1 {
+ std::mem::swap(&mut y1, &mut y2);
+ }
+ &mut *v
+ } else {
+ return;
+ };
+ target.push(LineCandidate {
+ shape_index,
+ line: VectorLine {
+ x1,
+ y1,
+ x2,
+ y2,
+ stroke: path.is_stroked,
+ stroke_width: path.is_stroked.then_some(path.stroke_width),
+ stroke_color: path
+ .is_stroked
+ .then(|| path.stroke_color.as_ref().map(color_to_argb_hex))
+ .flatten(),
+ fill: path.is_filled,
+ fill_color: path
+ .is_filled
+ .then(|| path.fill_color.as_ref().map(color_to_argb_hex))
+ .flatten(),
+ },
+ });
+}
+
+fn merge_axis_lines(
+ mut candidates: Vec<LineCandidate>,
+ horizontal: bool,
+ shapes: &[VectorShape],
+ shape_retained: &[bool],
+) -> Vec<VectorLine> {
+ let mut retained = vec![true; candidates.len()];
+ for i in 0..candidates.len() {
+ if !retained[i]
+ || is_merged_shape_boundary(&candidates[i], horizontal, shapes, shape_retained)
+ {
+ retained[i] = false;
+ continue;
+ }
+ for j in i + 1..candidates.len() {
+ let same_axis = if horizontal {
+ (candidates[i].line.y1 - candidates[j].line.y1).abs() < PATH_EPSILON
+ } else {
+ (candidates[i].line.x1 - candidates[j].line.x1).abs() < PATH_EPSILON
+ };
+ if !same_axis {
+ break;
+ }
+ if !retained[j]
+ || is_merged_shape_boundary(&candidates[j], horizontal, shapes, shape_retained)
+ {
+ retained[j] = false;
+ continue;
+ }
+ let a = &candidates[i].line;
+ let b = &candidates[j].line;
+ let compatible = a.stroke == b.stroke
+ && (a.stroke || (a.fill && b.fill))
+ && (!a.stroke
+ || ((a.stroke_width.unwrap_or(0.0) - b.stroke_width.unwrap_or(0.0)).abs()
+ < PATH_EPSILON
+ && (a.stroke_color.is_none()
+ || b.stroke_color.is_none()
+ || a.stroke_color == b.stroke_color)))
+ && (a.stroke || a.fill_color == b.fill_color);
+ if !compatible {
+ continue;
+ }
+ let threshold = PATH_EPSILON
+ + if a.stroke {
+ a.stroke_width.unwrap_or(0.0)
+ } else {
+ 0.0
+ };
+ let touching = if horizontal {
+ b.x1 < a.x2 + threshold && b.x2 > a.x1 - threshold
+ } else {
+ b.y1 < a.y2 + threshold && b.y2 > a.y1 - threshold
+ };
+ if touching {
+ let b = candidates[j].line.clone();
+ if horizontal {
+ candidates[i].line.x1 = candidates[i].line.x1.min(b.x1);
+ candidates[i].line.x2 = candidates[i].line.x2.max(b.x2);
+ } else {
+ candidates[i].line.y1 = candidates[i].line.y1.min(b.y1);
+ candidates[i].line.y2 = candidates[i].line.y2.max(b.y2);
+ }
+ retained[j] = false;
+ }
+ }
+ }
+ candidates
+ .into_iter()
+ .zip(retained)
+ .filter_map(|(c, keep)| keep.then_some(c.line))
+ .collect()
+}
+
+fn is_merged_shape_boundary(
+ candidate: &LineCandidate,
+ horizontal: bool,
+ shapes: &[VectorShape],
+ shape_retained: &[bool],
+) -> bool {
+ if shape_retained[candidate.shape_index] {
+ return false;
+ }
+ let shape = &shapes[candidate.shape_index].bbox;
+ if horizontal {
+ (candidate.line.y1 - shape.y).abs() < PATH_EPSILON
+ || (candidate.line.y1 - (shape.y + shape.height)).abs() < PATH_EPSILON
+ } else {
+ (candidate.line.x1 - shape.x).abs() < PATH_EPSILON
+ || (candidate.line.x1 - (shape.x + shape.width)).abs() < PATH_EPSILON
+ }
+}
+
fn rectf_to_rect(r: &RectF) -> Rect {
Rect {
x: r.left,
@@ -2185,6 +2461,7 @@ mod tests {
page_height: 100.0,
text_items: items,
graphics: Vec::new(),
+ vector_graphics: None,
struct_nodes: Vec::new(),
image_refs: Vec::new(),
}
@@ -2341,6 +2618,178 @@ mod tests {
assert_eq!(color_to_argb_hex(&z), "00000000");
}
+ fn vector_path(
+ bbox: RectF,
+ segments: Vec<pdfium::PathSegment>,
+ stroke: bool,
+ fill: bool,
+ stroke_width: f32,
+ color: pdfium::Color,
+ ) -> PathObject {
+ PathObject {
+ bbox,
+ stroke_color: stroke.then_some(color),
+ fill_color: fill.then_some(color),
+ stroke_width,
+ is_stroked: stroke,
+ is_filled: fill,
+ segments,
+ }
+ }
+
+ #[test]
+ fn vector_graphics_reports_paint_curve_and_merges_axis_lines() {
+ let black = pdfium::Color {
+ r: 0,
+ g: 0,
+ b: 0,
+ a: 255,
+ };
+ let paths = vec![
+ vector_path(
+ RectF {
+ left: 10.0,
+ top: 10.0,
+ right: 30.0,
+ bottom: 11.0,
+ },
+ vec![
+ pdfium::PathSegment {
+ kind: SegmentKind::MoveTo,
+ x: 10.0,
+ y: 10.0,
+ close: false,
+ },
+ pdfium::PathSegment {
+ kind: SegmentKind::LineTo,
+ x: 20.0,
+ y: 10.0,
+ close: false,
+ },
+ pdfium::PathSegment {
+ kind: SegmentKind::BezierTo,
+ x: 20.0,
+ y: 10.0,
+ close: false,
+ },
+ ],
+ true,
+ false,
+ 1.0,
+ black,
+ ),
+ vector_path(
+ RectF {
+ left: 20.0,
+ top: 10.2,
+ right: 30.0,
+ bottom: 11.0,
+ },
+ vec![
+ pdfium::PathSegment {
+ kind: SegmentKind::MoveTo,
+ x: 20.0,
+ y: 10.0,
+ close: false,
+ },
+ pdfium::PathSegment {
+ kind: SegmentKind::LineTo,
+ x: 30.0,
+ y: 10.0,
+ close: false,
+ },
+ ],
+ true,
+ false,
+ 1.0,
+ black,
+ ),
+ ];
+ let output = build_vector_graphics(&paths);
+ assert_eq!(output.shapes.len(), 2);
+ assert!(output.shapes[0].has_curve);
+ assert_eq!(output.shapes[0].stroke_color.as_deref(), Some("ff000000"));
+ assert_eq!(output.lines.len(), 1);
+ assert!((output.lines[0].x1 - 10.0).abs() < 0.001);
+ assert!((output.lines[0].x2 - 30.0).abs() < 0.001);
+ assert_eq!(output.lines[0].stroke_width, Some(1.0));
+ }
+
+ #[test]
+ fn vector_graphics_merges_consecutive_contained_solid_fills() {
+ let blue = pdfium::Color {
+ r: 0,
+ g: 0,
+ b: 255,
+ a: 255,
+ };
+ let paths = vec![
+ vector_path(
+ RectF {
+ left: 0.0,
+ top: 0.0,
+ right: 20.0,
+ bottom: 20.0,
+ },
+ vec![],
+ false,
+ true,
+ 0.0,
+ blue,
+ ),
+ vector_path(
+ RectF {
+ left: 2.0,
+ top: 2.0,
+ right: 10.0,
+ bottom: 10.0,
+ },
+ vec![],
+ false,
+ true,
+ 0.0,
+ blue,
+ ),
+ ];
+ let output = build_vector_graphics(&paths);
+ assert_eq!(output.shapes.len(), 1);
+ assert_eq!(output.shapes[0].bbox.width, 20.0);
+ assert_eq!(output.shapes[0].fill_color.as_deref(), Some("ff0000ff"));
+ }
+
+ #[test]
+ fn vector_graphics_ignores_unpainted_and_diagonal_paths() {
+ let path = vector_path(
+ RectF {
+ left: 0.0,
+ top: 0.0,
+ right: 10.0,
+ bottom: 10.0,
+ },
+ vec![
+ pdfium::PathSegment {
+ kind: SegmentKind::MoveTo,
+ x: 0.0,
+ y: 0.0,
+ close: false,
+ },
+ pdfium::PathSegment {
+ kind: SegmentKind::LineTo,
+ x: 10.0,
+ y: 10.0,
+ close: false,
+ },
+ ],
+ false,
+ false,
+ 0.0,
+ pdfium::Color::default(),
+ );
+ let output = build_vector_graphics(&[path]);
+ assert!(output.shapes.is_empty());
+ assert!(output.lines.is_empty());
+ }
+
#[test]
fn extract_pages_from_input_missing_file_errors() {
let res = extract_pages_from_input(
diff --git a/crates/liteparse/src/main.rs b/crates/liteparse/src/main.rs
index 3f5ef8d..a3e00b1 100644
--- a/crates/liteparse/src/main.rs
+++ b/crates/liteparse/src/main.rs
@@ -131,6 +131,9 @@ struct ParseCommand {
/// Include rich PDF text metadata in text items and JSON output.
#[arg(long)]
include_text_metadata: bool,
+ /// Include page-scoped vector shapes and merged horizontal/vertical lines.
+ #[arg(long)]
+ extract_vector_graphics: bool,
}
#[derive(Args, Debug)]
@@ -230,6 +233,9 @@ struct BatchParseCommand {
/// Extract embedded image bytes and metadata.
#[arg(long)]
extract_images: bool,
+ /// Include page-scoped vector shapes and merged horizontal/vertical lines.
+ #[arg(long)]
+ extract_vector_graphics: bool,
}
#[derive(Args, Debug)]
@@ -351,6 +357,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
extract_links: !cmd.no_links,
include_complexity: cmd.complexity,
include_text_metadata: cmd.include_text_metadata,
+ extract_vector_graphics: cmd.extract_vector_graphics,
..Default::default()
};
if let Some(n) = cmd.num_workers {
@@ -447,6 +454,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
include_complexity: cmd.complexity,
include_text_metadata: cmd.include_text_metadata,
extract_images: cmd.extract_images,
+ extract_vector_graphics: cmd.extract_vector_graphics,
..Default::default()
};
if let Some(n) = cmd.num_workers {
diff --git a/crates/liteparse/src/markdown_layout/test_helpers.rs b/crates/liteparse/src/markdown_layout/test_helpers.rs
index 8a27727..1d3e990 100644
--- a/crates/liteparse/src/markdown_layout/test_helpers.rs
+++ b/crates/liteparse/src/markdown_layout/test_helpers.rs
@@ -39,6 +39,7 @@ pub(crate) fn page(lines: Vec<ProjectedLine>) -> ParsedPage {
projected_lines: lines,
regions: crate::types::Region::default(),
graphics: vec![],
+ vector_graphics: None,
figures: vec![],
struct_nodes: vec![],
image_refs: vec![],
@@ -190,6 +191,7 @@ pub(crate) fn header_footer_page(n: usize, header: &str, footer: &str, body: &st
projected_lines: lines,
regions: crate::types::Region::default(),
graphics: vec![],
+ vector_graphics: None,
figures: vec![],
struct_nodes: vec![],
image_refs: vec![],
diff --git a/crates/liteparse/src/ocr_merge.rs b/crates/liteparse/src/ocr_merge.rs
index c994937..c17ad29 100644
--- a/crates/liteparse/src/ocr_merge.rs
+++ b/crates/liteparse/src/ocr_merge.rs
@@ -1162,6 +1162,7 @@ mod tests {
page_height: 100.0,
text_items: Vec::new(),
graphics: Vec::new(),
+ vector_graphics: None,
struct_nodes: Vec::new(),
image_refs: Vec::new(),
}
@@ -1194,6 +1195,7 @@ mod tests {
..Default::default()
}],
graphics: Vec::new(),
+ vector_graphics: None,
struct_nodes: Vec::new(),
image_refs: Vec::new(),
}
@@ -1217,6 +1219,7 @@ mod tests {
..Default::default()
}],
graphics: Vec::new(),
+ vector_graphics: None,
struct_nodes: Vec::new(),
image_refs: Vec::new(),
}
diff --git a/crates/liteparse/src/output/json.rs b/crates/liteparse/src/output/json.rs
index 73d673e..106b4c5 100644
--- a/crates/liteparse/src/output/json.rs
+++ b/crates/liteparse/src/output/json.rs
@@ -1,5 +1,5 @@
use crate::ocr_merge::PageComplexityStats;
-use crate::types::{ExtractedImage, ParsedPage, Rect};
+use crate::types::{ExtractedImage, ParsedPage, Rect, VectorGraphics};
use serde::Serialize;
#[derive(Debug, Serialize)]
@@ -50,6 +50,8 @@ pub(crate) struct JsonPage {
pub text_items: Vec<JsonTextItem>,
#[serde(skip_serializing_if = "Option::is_none")]
pub complexity: Option<PageComplexityStats>,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub vector_graphics: Option<VectorGraphics>,
}
#[derive(Debug, Serialize)]
@@ -128,6 +130,7 @@ pub(crate) fn build_json(pages: &[ParsedPage], include_text_metadata: bool) -> P
})
.collect(),
complexity: page.complexity.clone(),
+ vector_graphics: page.vector_graphics.clone(),
})
.collect(),
}
@@ -205,6 +208,7 @@ mod tests {
projected_lines: vec![],
regions: crate::types::Region::default(),
graphics: vec![],
+ vector_graphics: None,
figures: vec![],
struct_nodes: vec![],
image_refs: vec![],
@@ -324,4 +328,32 @@ mod tests {
assert!(value["images"][0].get("bytes").is_none());
assert_eq!(value["image_error_count"], 2);
}
+
+ #[test]
+ fn vector_graphics_is_absent_by_default_and_serialized_when_present() {
+ let default_json = format_json(&[page(vec![])]).unwrap();
+ assert!(!default_json.contains("vector_graphics"));
+
+ let mut with_vectors = page(vec![]);
+ with_vectors.vector_graphics = Some(VectorGraphics {
+ shapes: vec![crate::types::VectorShape {
+ bbox: crate::types::Rect {
+ x: 1.0,
+ y: 2.0,
+ width: 3.0,
+ height: 4.0,
+ },
+ stroke: true,
+ stroke_color: Some("ff000000".into()),
+ fill: false,
+ fill_color: None,
+ has_curve: true,
+ }],
+ lines: vec![],
+ });
+ let enabled_json = format_json(&[with_vectors]).unwrap();
+ assert!(enabled_json.contains("\"vector_graphics\""));
+ assert!(enabled_json.contains("\"has_curve\": true"));
+ assert!(enabled_json.contains("\"stroke_color\": \"ff000000\""));
+ }
}
diff --git a/crates/liteparse/src/output/markdown.rs b/crates/liteparse/src/output/markdown.rs
index 2bc6b94..2b1606d 100644
--- a/crates/liteparse/src/output/markdown.rs
+++ b/crates/liteparse/src/output/markdown.rs
@@ -155,6 +155,7 @@ mod tests {
projected_lines: lines,
regions: crate::types::Region::default(),
graphics: vec![],
+ vector_graphics: None,
figures: vec![],
struct_nodes: vec![],
image_refs: vec![],
@@ -204,6 +205,7 @@ mod tests {
projected_lines: vec![],
regions: crate::types::Region::default(),
graphics: vec![],
+ vector_graphics: None,
figures: vec![],
struct_nodes: vec![],
image_refs: vec![],
diff --git a/crates/liteparse/src/output/text.rs b/crates/liteparse/src/output/text.rs
index dace7af..190494e 100644
--- a/crates/liteparse/src/output/text.rs
+++ b/crates/liteparse/src/output/text.rs
@@ -24,6 +24,7 @@ mod tests {
projected_lines: vec![],
regions: crate::types::Region::default(),
graphics: vec![],
+ vector_graphics: None,
figures: vec![],
struct_nodes: vec![],
image_refs: vec![],
diff --git a/crates/liteparse/src/parser.rs b/crates/liteparse/src/parser.rs
index b299e17..ca525c9 100644
--- a/crates/liteparse/src/parser.rs
+++ b/crates/liteparse/src/parser.rs
@@ -214,7 +214,7 @@ impl LiteParse {
false, // render_images: image rasters not needed for complexity
false, // extract_links: irrelevant for complexity stats
self.glyph_resolver.as_deref(),
- false, // emit_word_boxes: word boxes not needed for complexity stats
+ extract::ExtractionOutputOptions::default(),
)?;
let t_extract = web_time::Instant::now();
log(&format!(
@@ -361,7 +361,10 @@ impl LiteParse {
self.config.extract_links
&& self.config.output_format == crate::config::OutputFormat::Markdown,
self.glyph_resolver.as_deref(),
- self.config.emit_word_boxes,
+ extract::ExtractionOutputOptions {
+ emit_word_boxes: self.config.emit_word_boxes,
+ extract_vector_graphics: self.config.extract_vector_graphics,
+ },
)?;
let t_extract = web_time::Instant::now();
log(&format!(
diff --git a/crates/liteparse/src/projection.rs b/crates/liteparse/src/projection.rs
index b570802..d627899 100644
--- a/crates/liteparse/src/projection.rs
+++ b/crates/liteparse/src/projection.rs
@@ -2910,6 +2910,7 @@ pub fn project_pages_to_grid(pages: Vec<Page>) -> Vec<ParsedPage> {
projected_lines,
regions,
graphics: page.graphics,
+ vector_graphics: page.vector_graphics,
figures,
struct_nodes: page.struct_nodes,
image_refs: page.image_refs,
@@ -5001,6 +5002,7 @@ mod tests {
page_width: 612.0,
page_height: 792.0,
graphics: Vec::new(),
+ vector_graphics: None,
text_items: Vec::new(),
struct_nodes: Vec::new(),
image_refs: Vec::new(),
@@ -5303,6 +5305,7 @@ mod tests {
page_height: 792.0,
text_items: Vec::new(),
graphics: Vec::new(),
+ vector_graphics: None,
struct_nodes: Vec::new(),
image_refs: Vec::new(),
}];
@@ -5340,6 +5343,7 @@ mod tests {
},
],
graphics: Vec::new(),
+ vector_graphics: None,
struct_nodes: Vec::new(),
image_refs: Vec::new(),
}];
@@ -5400,6 +5404,7 @@ mod tests {
},
],
graphics: Vec::new(),
+ vector_graphics: None,
struct_nodes: Vec::new(),
image_refs: Vec::new(),
}];
@@ -5440,6 +5445,7 @@ mod tests {
},
],
graphics: Vec::new(),
+ vector_graphics: None,
struct_nodes: Vec::new(),
image_refs: Vec::new(),
}];
diff --git a/crates/liteparse/src/types.rs b/crates/liteparse/src/types.rs
index 2bc6472..1609b08 100644
--- a/crates/liteparse/src/types.rs
+++ b/crates/liteparse/src/types.rs
@@ -110,6 +110,10 @@ pub struct Page {
/// Not emitted in JSON/text outputs — consumed by the markdown layout pass.
#[serde(skip)]
pub graphics: Vec<GraphicPrimitive>,
+ /// Lossless-enough, PDFium-compatible path output requested by the caller.
+ /// Kept separate from the lossy internal `graphics` layout primitives.
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub vector_graphics: Option<VectorGraphics>,
/// Structure-tree nodes for this page when the PDF is tagged. Each node
/// carries its role, marked-content ids, and the union bbox of its tagged
/// content. Empty for untagged PDFs.
@@ -173,6 +177,9 @@ pub struct ParsedPage {
/// the JSON/text output.
#[serde(skip)]
pub graphics: Vec<GraphicPrimitive>,
+ /// Public vector path extraction. Absent unless explicitly enabled.
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub vector_graphics: Option<VectorGraphics>,
/// Figure-region bounding rectangles derived from `graphics`. Pre-computed
/// in `to_parsed_pages` so the XY-cut layout pass can treat them as
/// obstacles, and reused downstream for figure classification.
@@ -249,6 +256,45 @@ pub struct Rect {
pub height: f32,
}
+/// Page-scoped vector path output. Coordinates use the same top-left,
+/// 72-DPI viewport space as text items.
+#[derive(Debug, Clone, Default, Serialize)]
+pub struct VectorGraphics {
+ pub shapes: Vec<VectorShape>,
+ pub lines: Vec<VectorLine>,
+}
+
+/// One PDF path object's paint state and viewport bounding box.
+#[derive(Debug, Clone, Serialize)]
+pub struct VectorShape {
+ pub bbox: Rect,
+ pub stroke: bool,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub stroke_color: Option<String>,
+ pub fill: bool,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub fill_color: Option<String>,
+ pub has_curve: bool,
+}
+
+/// A strict horizontal or vertical path segment after adjacent compatible
+/// segments have been merged, matching LlamaParse PDFium path semantics.
+#[derive(Debug, Clone, Serialize)]
+pub struct VectorLine {
+ pub x1: f32,
+ pub y1: f32,
+ pub x2: f32,
+ pub y2: f32,
+ pub stroke: bool,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub stroke_width: Option<f32>,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub stroke_color: Option<String>,
+ pub fill: bool,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub fill_color: Option<String>,
+}
+
/// Lightweight vector-graphic primitive derived from PDFium path objects.
/// Only the shapes useful to the markdown emitter (ruled tables, HRs, figure
/// clusters) are kept — bezier curves and complex paths are decomposed into
@@ -458,6 +504,7 @@ mod tests {
page_height: 200.0,
text_items: vec![sample_item()],
graphics: vec![],
+ vector_graphics: None,
struct_nodes: vec![],
image_refs: vec![],
};
diff --git a/crates/pdfium/src/page.rs b/crates/pdfium/src/page.rs
index cffedf0..166f588 100644
--- a/crates/pdfium/src/page.rs
+++ b/crates/pdfium/src/page.rs
@@ -1115,3 +1115,53 @@ impl Drop for Page<'_, '_> {
unsafe { ffi!(FPDF_ClosePage(self.handle)) };
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn nested_form_matrix_composition_transforms_path_points_in_order() {
+ // Outer form translates by (100, 20), inner form scales by (2, 3),
+ // and the path object translates by (4, 5). The collector composes
+ // these in outer(inner(path(point))) order.
+ let outer = pdfium_sys::FS_MATRIX {
+ a: 1.0,
+ b: 0.0,
+ c: 0.0,
+ d: 1.0,
+ e: 100.0,
+ f: 20.0,
+ };
+ let inner = pdfium_sys::FS_MATRIX {
+ a: 2.0,
+ b: 0.0,
+ c: 0.0,
+ d: 3.0,
+ e: 0.0,
+ f: 0.0,
+ };
+ let object = pdfium_sys::FS_MATRIX {
+ a: 1.0,
+ b: 0.0,
+ c: 0.0,
+ d: 1.0,
+ e: 4.0,
+ f: 5.0,
+ };
+ let nested = compose_matrix(&compose_matrix(&outer, &inner), &object);
+ let x = nested.a * 1.0 + nested.c * 2.0 + nested.e;
+ let y = nested.b * 1.0 + nested.d * 2.0 + nested.f;
+ assert_eq!((x, y), (110.0, 41.0));
+
+ let viewport = ViewportTransform {
+ a: 1.0,
+ b: 0.0,
+ c: 0.0,
+ d: -1.0,
+ e: 0.0,
+ f: 200.0,
+ };
+ assert_eq!(viewport.transform_point(x, y), (110.0, 159.0));
+ }
+}
diff --git a/packages/node/README.md b/packages/node/README.md
index 08a48dc..c699cb6 100644
--- a/packages/node/README.md
+++ b/packages/node/README.md
@@ -61,6 +61,7 @@ const parser = new LiteParse({
extractImages: true, // Extract image bytes + metadata (default: false)
imageOutputDir: './images', // Write images and return name/path metadata (optional)
extractLinks: true, // Render [text](url) links in markdown output
+ extractVectorGraphics: false, // Opt-in shapes + merged H/V lines per page
preserveVerySmallText: false, // Keep tiny text
includeTextMetadata: false, // Opt in to MCID, font metrics, colors, char codes, and tsg
password: undefined, // Password for protected documents
diff --git a/packages/node/src/cli.ts b/packages/node/src/cli.ts
index d99bbe0..bdf3fa8 100644
--- a/packages/node/src/cli.ts
+++ b/packages/node/src/cli.ts
@@ -91,6 +91,10 @@ program
"--complexity",
"Include per-page complexity signals in JSON output",
)
+ .option(
+ "--extract-vector-graphics",
+ "Include page-scoped vector shapes and merged horizontal/vertical lines",
+ )
.action(async (file: string, opts: Record<string, unknown>) => {
try {
const config: Partial<LiteParseConfig> = {};
@@ -126,6 +130,7 @@ program
if (opts.quiet) config.quiet = true;
if (opts.numWorkers) config.numWorkers = opts.numWorkers as number;
if (opts.complexity) config.includeComplexity = true;
+ if (opts.extractVectorGraphics) config.extractVectorGraphics = true;
// Default CLI output to text (library defaults to json)
if (!config.outputFormat) config.outputFormat = "text";
@@ -143,6 +148,7 @@ program
height: p.height,
text: p.text,
textItems: p.textItems,
+ ...(p.vectorGraphics ? { vectorGraphics: p.vectorGraphics } : {}),
})),
images: result.images.map(({ bytes: _bytes, ...image }) => image),
imageErrorCount: result.imageErrorCount,
@@ -317,6 +323,10 @@ program
"Include rich PDF text metadata in text items and JSON output",
)
.option("--extract-images", "Extract embedded image bytes and metadata")
+ .option(
+ "--extract-vector-graphics",
+ "Include page-scoped vector shapes and merged horizontal/vertical lines",
+ )
.action(
async (
inputDir: string,
@@ -340,6 +350,7 @@ program
if (opts.numWorkers) config.numWorkers = opts.numWorkers as number;
if (opts.includeTextMetadata) config.includeTextMetadata = true;
if (opts.extractImages) config.extractImages = true;
+ if (opts.extractVectorGraphics) config.extractVectorGraphics = true;
const parser = new LiteParse(config);
const outExt = format === "json" ? ".json" : format === "markdown" ? ".md" : ".txt";
@@ -396,6 +407,7 @@ program
height: p.height,
text: p.text,
textItems: p.textItems,
+ ...(p.vectorGraphics ? { vectorGraphics: p.vectorGraphics } : {}),
})),
images: result.images.map(({ bytes: _bytes, ...image }) => image),
imageErrorCount: result.imageErrorCount,
diff --git a/packages/node/src/lib.ts b/packages/node/src/lib.ts
index a6dfea1..7c1890d 100644
--- a/packages/node/src/lib.ts
+++ b/packages/node/src/lib.ts
@@ -84,6 +84,8 @@ export interface LiteParseConfig {
* extra vector-text detection pass.
*/
includeComplexity: boolean;
+ /** Expose page-scoped vector shapes and merged H/V line segments. Default false. */
+ extractVectorGraphics: boolean;
}
/**
@@ -198,6 +200,31 @@ export interface ParsedPage {
* otherwise.
*/
complexity?: PageComplexityStats;
+ /** Present only when parsing with `extractVectorGraphics: true`. */
+ vectorGraphics?: VectorGraphics;
+}
+
+export interface VectorGraphics {
+ shapes: VectorShape[];
+ lines: VectorLine[];
+}
+
+export interface VectorShape {
+ bbox: { x: number; y: number; width: number; height: number };
+ stroke: boolean;
+ strokeColor?: string;
+ fill: boolean;
+ fillColor?: string;
+ hasCurve: boolean;
+}
+
+export interface VectorLine {
+ x1: number; y1: number; x2: number; y2: number;
+ stroke: boolean;
+ strokeWidth?: number;
+ strokeColor?: string;
+ fill: boolean;
+ fillColor?: string;
}
export interface ExtractedImage {
@@ -347,6 +374,7 @@ export class LiteParse {
cropBox: userConfig.cropBox,
skipDiagonalText: userConfig.skipDiagonalText,
includeComplexity: userConfig.includeComplexity,
+ extractVectorGraphics: userConfig.extractVectorGraphics,
};
this._native = new native.LiteParse(nativeConfig);
@@ -378,6 +406,7 @@ export class LiteParse {
cropBox: resolved.cropBox ?? undefined,
skipDiagonalText: resolved.skipDiagonalText ?? false,
includeComplexity: resolved.includeComplexity ?? false,
+ extractVectorGraphics: resolved.extractVectorGraphics ?? false,
};
}
@@ -492,6 +521,7 @@ function toPage(p: NativeParsedPage): ParsedPage {
markdown: p.markdown,
textItems: p.textItems.map(toTextItem),
complexity: p.complexity ? toComplexity(p.complexity) : undefined,
+ vectorGraphics: p.vectorGraphics ?? undefined,
};
}
diff --git a/packages/node/src/native.ts b/packages/node/src/native.ts
index 50f21ec..86fa186 100644
--- a/packages/node/src/native.ts
+++ b/packages/node/src/native.ts
@@ -44,6 +44,7 @@ export interface LiteParseNativeConfig {
cropBox?: NativeCropBox;
skipDiagonalText?: boolean;
includeComplexity?: boolean;
+ extractVectorGraphics?: boolean;
}
export interface NativeCropBox {
@@ -118,6 +119,23 @@ export interface NativeParsedPage {
markdown: string;
textItems: NativeTextItem[];
complexity?: NativePageComplexityStats;
+ vectorGraphics?: NativeVectorGraphics;
+}
+
+export interface NativeVectorGraphics {
+ shapes: Array<{
+ bbox: { x: number; y: number; width: number; height: number };
+ stroke: boolean;
+ strokeColor?: string;
+ fill: boolean;
+ fillColor?: string;
+ hasCurve: boolean;
+ }>;
+ lines: Array<{
+ x1: number; y1: number; x2: number; y2: number;
+ stroke: boolean; strokeWidth?: number; strokeColor?: string;
+ fill: boolean; fillColor?: string;
+ }>;
}
export interface NativeExtractedImage {
diff --git a/packages/python/README.md b/packages/python/README.md
index 8d15471..5161536 100644
--- a/packages/python/README.md
+++ b/packages/python/README.md
@@ -60,6 +60,7 @@ parser = LiteParse(
extract_images=True, # Extract image bytes + metadata (default: False)
image_output_dir="./images", # Write images and return name/path metadata (optional)
extract_links=True, # Render [text](url) links in markdown output
+ extract_vector_graphics=False, # Opt-in shapes + merged H/V lines per page
preserve_very_small_text=False, # Keep tiny text
include_text_metadata=False, # Opt in to MCID, font metrics, colors, char codes, and tsg
password=None, # Password for protected documents
diff --git a/packages/python/liteparse/__init__.py b/packages/python/liteparse/__init__.py
index a5b57d6..2617d22 100644
--- a/packages/python/liteparse/__init__.py
+++ b/packages/python/liteparse/__init__.py
@@ -13,6 +13,9 @@ from .types import (
WordBox,
ScreenshotResult,
ParseError,
+ VectorGraphics,
+ VectorLine,
+ VectorShape,
)
try:
@@ -33,4 +36,7 @@ __all__ = [
"ImageRect",
"ParseError",
"search_items",
+ "VectorGraphics",
+ "VectorLine",
+ "VectorShape",
]
diff --git a/packages/python/liteparse/parser.py b/packages/python/liteparse/parser.py
index d142fdb..c8a1545 100644
--- a/packages/python/liteparse/parser.py
+++ b/packages/python/liteparse/parser.py
@@ -18,6 +18,9 @@ from .types import (
ScreenshotResult,
TextItem,
WordBox,
+ VectorGraphics,
+ VectorLine,
+ VectorShape,
)
@@ -94,6 +97,7 @@ def _convert_native_result(native_result: Any) -> ParseResult:
for item in native_page.text_items
]
native_complexity = getattr(native_page, "complexity", None)
+ native_vectors = getattr(native_page, "vector_graphics", None)
pages.append(
ParsedPage(
page_num=native_page.page_num,
@@ -107,6 +111,37 @@ def _convert_native_result(native_result: Any) -> ParseResult:
if native_complexity is not None
else None
),
+ vector_graphics=(
+ VectorGraphics(
+ shapes=[
+ VectorShape(
+ bbox=(s.bbox.x, s.bbox.y, s.bbox.width, s.bbox.height),
+ stroke=s.stroke,
+ stroke_color=s.stroke_color,
+ fill=s.fill,
+ fill_color=s.fill_color,
+ has_curve=s.has_curve,
+ )
+ for s in native_vectors.shapes
+ ],
+ lines=[
+ VectorLine(
+ x1=l.x1,
+ y1=l.y1,
+ x2=l.x2,
+ y2=l.y2,
+ stroke=l.stroke,
+ stroke_width=l.stroke_width,
+ stroke_color=l.stroke_color,
+ fill=l.fill,
+ fill_color=l.fill_color,
+ )
+ for l in native_vectors.lines
+ ],
+ )
+ if native_vectors is not None
+ else None
+ ),
)
)
images = [
@@ -178,6 +213,7 @@ class LiteParse:
crop_box: Optional[Tuple[float, float, float, float]] = None,
skip_diagonal_text: Optional[bool] = None,
include_complexity: Optional[bool] = None,
+ extract_vector_graphics: Optional[bool] = None,
):
"""
Initialize LiteParse parser.
@@ -237,6 +273,8 @@ class LiteParse:
``ParsedPage.complexity`` (the same :meth:`is_complex` returns).
Default False; enabling it runs an extra vector-text detection
pass.
+ extract_vector_graphics: Expose page-scoped vector shapes and
+ merged horizontal/vertical line segments. Default False.
"""
kwargs = {}
if ocr_enabled is not None:
@@ -287,6 +325,8 @@ class LiteParse:
kwargs["skip_diagonal_text"] = skip_diagonal_text
if include_complexity is not None:
kwargs["include_complexity"] = include_complexity
+ if extract_vector_graphics is not None:
+ kwargs["extract_vector_graphics"] = extract_vector_graphics
self._native = _NativeLiteParse(**kwargs)
@@ -430,6 +470,7 @@ class LiteParse:
include_complexity=cfg.include_complexity,
include_text_metadata=cfg.include_text_metadata,
extract_images=cfg.extract_images,
+ extract_vector_graphics=cfg.extract_vector_graphics,
)
def __repr__(self) -> str:
diff --git a/packages/python/liteparse/types.py b/packages/python/liteparse/types.py
index 5c48d03..00e8a0a 100644
--- a/packages/python/liteparse/types.py
+++ b/packages/python/liteparse/types.py
@@ -62,6 +62,37 @@ class ParsedPage:
#: returns). Populated only when parsing with ``include_complexity=True``;
#: ``None`` otherwise.
complexity: Optional[PageComplexityStats] = None
+ #: Present only when parsing with ``extract_vector_graphics=True``.
+ vector_graphics: Optional[VectorGraphics] = None
+
+
+@dataclass
+class VectorShape:
+ bbox: Tuple[float, float, float, float]
+ stroke: bool
+ stroke_color: Optional[str]
+ fill: bool
+ fill_color: Optional[str]
+ has_curve: bool
+
+
+@dataclass
+class VectorLine:
+ x1: float
+ y1: float
+ x2: float
+ y2: float
+ stroke: bool
+ stroke_width: Optional[float]
+ stroke_color: Optional[str]
+ fill: bool
+ fill_color: Optional[str]
+
+
+@dataclass
+class VectorGraphics:
+ shapes: List[VectorShape]
+ lines: List[VectorLine]
@dataclass
@@ -200,6 +231,7 @@ class LiteParseConfig:
include_complexity: bool
include_text_metadata: bool = False
extract_images: bool = False
+ extract_vector_graphics: bool = False
class ParseError(Exception):
diff --git a/packages/wasm/README.md b/packages/wasm/README.md
index 14db051..b005364 100644
--- a/packages/wasm/README.md
+++ b/packages/wasm/README.md
@@ -68,6 +68,7 @@ All optional, camelCase:
| `outputFormat` | `"json" \| "text" \| "markdown"` | `"json"` | Output format; `"markdown"` returns rendered Markdown on `result.text` |
| `imageMode` | `"off" \| "placeholder" \| "embed"` | `"placeholder"` | How raster images are surfaced in markdown output |
| `extractLinks` | `boolean` | `true` | Render hyperlink annotations as `[text](url)` in markdown output |
+| `extractVectorGraphics` | `boolean` | `false` | Include page-scoped shapes and merged horizontal/vertical lines |
| `preserveVerySmallText` | `boolean` | `false` | Keep tiny text that's normally filtered |
| `password` | `string` | — | Password for protected PDFs |
| `quiet` | `boolean` | `false` | Suppress progress logging |