ctb_formats_pdf/pdf.rs
1// SPDX-License-Identifier: AGPL-3.0-or-later
2/*
3This file is part of Collective Toolbox, a database and document workspace and utilities.
4Copyright (C) 2026 Collective Toolbox Developers
5Contact: info@collectivetoolbox.com
6
7This program is free software: you can redistribute it and/or modify it under
8the terms of the GNU Affero General Public License as published by the Free
9Software Foundation, either version 3 of the License, or (at your option) any
10later version.
11
12This program is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
15
16You should have received a copy of the GNU Affero General Public License along
17with this program. If not, see <https://www.gnu.org/licenses/>.
18*/
19
20//! PDF conversion helpers.
21//! FIXME: TODO!
22
23#[allow(
24 unused_imports,
25 clippy::wildcard_imports,
26 reason = "Standard workspace crate prelude"
27)]
28pub(crate) use ctb_utilities::*;
29
30use anyhow::Result;
31#[cfg(test)]
32// use unpdf::render::{RenderOptions, JsonFormat, to_text, to_json, to_markdown};
33#[cfg(test)]
34pub(crate) fn get_pdf_data(key: &str) -> Option<Vec<u8>> {
35 ctb_utilities::load_manifest_fixture(env!("CARGO_MANIFEST_DIR"), key)
36}
37
38/// Extract raw text from a PDF byte array.
39pub fn pdf2txt(_input: &[u8]) -> Result<String> {
40 Ok(String::new())
41 // let doc = unpdf::parse_bytes(input)?;
42 // let options = RenderOptions::default();
43 // let txt = to_text(&doc, &options)?;
44 // Ok(txt)
45}
46
47/// Convert PDF byte array structure into JSON format.
48pub fn pdf2json(_input: &[u8]) -> Result<String> {
49 Ok(String::new())
50 // let doc = unpdf::parse_bytes(input)?;
51 // let json = to_json(&doc, JsonFormat::Pretty)?;
52 // Ok(json)
53}
54
55/// Convert PDF byte array content to Markdown format.
56pub fn pdf2md(_input: &[u8]) -> Result<String> {
57 Ok(String::new())
58 // let doc = unpdf::parse_bytes(input)?;
59 // let options = RenderOptions::default();
60 // let md = to_markdown(&doc, &options)?;
61 // Ok(md)
62}
63
64#[cfg(test)]
65#[allow(
66 clippy::panic,
67 clippy::expect_used,
68 clippy::unwrap_used,
69 clippy::unwrap_in_result,
70 clippy::panic_in_result_fn,
71 clippy::indexing_slicing,
72 clippy::arithmetic_side_effects,
73 reason = "Standard repository test boilerplate"
74)]
75mod tests {
76
77 // #[crate::ctb_test]
78 // fn test_pdf2txt_invalid() {
79 // assert!(pdf2txt(b"invalid pdf").is_err());
80 // }
81
82 // #[crate::ctb_test]
83 // fn test_pdf2json_invalid() {
84 // assert!(pdf2json(b"invalid pdf").is_err());
85 // }
86
87 // #[crate::ctb_test]
88 // fn test_pdf2md_invalid() {
89 // assert!(pdf2md(b"invalid pdf").is_err());
90 // }
91
92 // #[crate::ctb_test]
93 // fn test_pdf2txt_valid() {
94 // let pdf_data = get_pdf_data("fixtures/sample.pdf").unwrap();
95 // let txt = pdf2txt(&pdf_data).unwrap();
96
97 // // unpdf extracts raw text with some null characters and spacing due to font encoding.
98 // // We clean up nulls, spaces, and form-feed characters to compare the semantic text content.
99 // let txt_clean = txt.replace('\0', "").replace(' ', "").replace("\r", "");
100
101 // let expected_txt_bytes = get_pdf_data("fixtures/sample.txt").unwrap();
102 // let expected_txt = String::from_utf8(expected_txt_bytes).unwrap();
103 // let expected_clean = expected_txt.replace(' ', "").replace('\u{c}', "").replace("\r", "");
104
105 // assert_eq!(txt_clean.trim(), expected_clean.trim());
106 // }
107
108 // #[crate::ctb_test]
109 // fn test_pdf2json_valid() {
110 // let pdf_data = get_pdf_data("fixtures/sample.pdf").unwrap();
111 // let json = pdf2json(&pdf_data).unwrap();
112 // let json_clean = json.replace("\\u0000", "").replace(' ', "");
113 // assert!(json_clean.contains("6/29/26"));
114 // }
115
116 // #[crate::ctb_test]
117 // fn test_pdf2md_valid() {
118 // let pdf_data = get_pdf_data("fixtures/sample.pdf").unwrap();
119 // let md = pdf2md(&pdf_data).unwrap();
120 // let md_clean = md.replace('\0', "").replace(' ', "");
121 // assert!(md_clean.contains("6/29/26"));
122 // }
123
124 // TODO: unpdf is not currently capable of handling sample-invalid.pdf (extracts empty string instead of spacing/form-feed).
125 // #[crate::ctb_test]
126 // fn test_pdf2txt_sample_invalid() {
127 // let pdf_data = get_pdf_data("fixtures/sample-invalid.pdf").unwrap();
128 // let txt = pdf2txt(&pdf_data).unwrap();
129 // let expected_txt_bytes = get_pdf_data("fixtures/sample-invalid.txt").unwrap();
130 // let expected_txt = String::from_utf8(expected_txt_bytes).unwrap();
131 // assert_eq!(txt, expected_txt);
132 // }
133}