Skip to content

A fast, helpful, and open-source document parser

download proper binary for builds

Logan Markewich0e757d360eaf
diff --git a/packages/rust/Cargo.lock b/packages/rust/Cargo.lock
index 2c9cb44..3a40404 100644
--- a/packages/rust/Cargo.lock
+++ b/packages/rust/Cargo.lock
@@ -2,6 +2,12 @@
# It is not intended for manual editing.
version = 4
+[[package]]
+name = "adler2"
+version = "2.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa"
+
[[package]]
name = "aho-corasick"
version = "1.1.4"
@@ -47,7 +53,7 @@ version = "1.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc"
dependencies = [
- "windows-sys",
+ "windows-sys 0.61.2",
]
[[package]]
@@ -58,9 +64,15 @@ checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d"
dependencies = [
"anstyle",
"once_cell_polyfill",
- "windows-sys",
+ "windows-sys 0.61.2",
]
+[[package]]
+name = "base64"
+version = "0.22.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
+
[[package]]
name = "bindgen"
version = "0.71.1"
@@ -87,6 +99,22 @@ version = "2.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "843867be96c8daad0d758b57df9392b6d8d271134fce549de6ce169ff98a92af"
+[[package]]
+name = "bytes"
+version = "1.11.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1e748733b7cbc798e1434b6ac524f0c1ff2ab456fe201501e6497c8417a4fc33"
+
+[[package]]
+name = "cc"
+version = "1.2.61"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d16d90359e986641506914ba71350897565610e87ce0ad9e6f28569db3dd5c6d"
+dependencies = [
+ "find-msvc-tools",
+ "shlex",
+]
+
[[package]]
name = "cexpr"
version = "0.6.0"
@@ -159,12 +187,69 @@ version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570"
+[[package]]
+name = "crc32fast"
+version = "1.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9481c1c90cbf2ac953f07c8d4a58aa3945c425b7185c9154d67a65e4230da511"
+dependencies = [
+ "cfg-if",
+]
+
[[package]]
name = "either"
version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719"
+[[package]]
+name = "errno"
+version = "0.3.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
+dependencies = [
+ "libc",
+ "windows-sys 0.61.2",
+]
+
+[[package]]
+name = "filetime"
+version = "0.2.27"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f98844151eee8917efc50bd9e8318cb963ae8b297431495d3f758616ea5c57db"
+dependencies = [
+ "cfg-if",
+ "libc",
+ "libredox",
+]
+
+[[package]]
+name = "find-msvc-tools"
+version = "0.1.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582"
+
+[[package]]
+name = "flate2"
+version = "1.1.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "843fba2746e448b37e26a819579957415c8cef339bf08564fe8b7ddbd959573c"
+dependencies = [
+ "crc32fast",
+ "miniz_oxide",
+]
+
+[[package]]
+name = "getrandom"
+version = "0.2.17"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0"
+dependencies = [
+ "cfg-if",
+ "libc",
+ "wasi",
+]
+
[[package]]
name = "glob"
version = "0.3.3"
@@ -177,6 +262,22 @@ version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
+[[package]]
+name = "http"
+version = "1.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e3ba2a386d7f85a81f119ad7498ebe444d2e22c2af0b86b069416ace48b3311a"
+dependencies = [
+ "bytes",
+ "itoa",
+]
+
+[[package]]
+name = "httparse"
+version = "1.10.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87"
+
[[package]]
name = "is_terminal_polyfill"
version = "1.70.2"
@@ -214,12 +315,31 @@ dependencies = [
"windows-link",
]
+[[package]]
+name = "libredox"
+version = "0.1.16"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e02f3bb43d335493c96bf3fd3a321600bf6bd07ed34bc64118e9293bdffea46c"
+dependencies = [
+ "bitflags",
+ "libc",
+ "plain",
+ "redox_syscall",
+]
+
+[[package]]
+name = "linux-raw-sys"
+version = "0.12.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53"
+
[[package]]
name = "liteparse-rs"
version = "0.1.0"
dependencies = [
"clap",
"pdfium",
+ "pdfium-sys",
"serde",
"serde_json",
]
@@ -242,6 +362,16 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
+[[package]]
+name = "miniz_oxide"
+version = "0.8.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316"
+dependencies = [
+ "adler2",
+ "simd-adler32",
+]
+
[[package]]
name = "nom"
version = "7.1.3"
@@ -252,6 +382,12 @@ dependencies = [
"minimal-lexical",
]
+[[package]]
+name = "once_cell"
+version = "1.21.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
+
[[package]]
name = "once_cell_polyfill"
version = "1.70.2"
@@ -270,8 +406,23 @@ name = "pdfium-sys"
version = "0.1.0"
dependencies = [
"bindgen",
+ "flate2",
+ "tar",
+ "ureq",
]
+[[package]]
+name = "percent-encoding"
+version = "2.3.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220"
+
+[[package]]
+name = "plain"
+version = "0.2.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6"
+
[[package]]
name = "prettyplease"
version = "0.2.37"
@@ -300,6 +451,15 @@ dependencies = [
"proc-macro2",
]
+[[package]]
+name = "redox_syscall"
+version = "0.7.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4666a1a60d8412eab19d94f6d13dcc9cea0a5ef4fdf6a5db306537413c661b1b"
+dependencies = [
+ "bitflags",
+]
+
[[package]]
name = "regex"
version = "1.12.3"
@@ -329,12 +489,74 @@ version = "0.8.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc897dd8d9e8bd1ed8cdad82b5966c3e0ecae09fb1907d58efaa013543185d0a"
+[[package]]
+name = "ring"
+version = "0.17.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a4689e6c2294d81e88dc6261c768b63bc4fcdb852be6d1352498b114f61383b7"
+dependencies = [
+ "cc",
+ "cfg-if",
+ "getrandom",
+ "libc",
+ "untrusted",
+ "windows-sys 0.52.0",
+]
+
[[package]]
name = "rustc-hash"
version = "2.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94300abf3f1ae2e2b8ffb7b58043de3d399c73fa6f4b73826402a5c457614dbe"
+[[package]]
+name = "rustix"
+version = "1.1.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b6fe4565b9518b83ef4f91bb47ce29620ca828bd32cb7e408f0062e9930ba190"
+dependencies = [
+ "bitflags",
+ "errno",
+ "libc",
+ "linux-raw-sys",
+ "windows-sys 0.61.2",
+]
+
+[[package]]
+name = "rustls"
+version = "0.23.40"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ef86cd5876211988985292b91c96a8f2d298df24e75989a43a3c73f2d4d8168b"
+dependencies = [
+ "log",
+ "once_cell",
+ "ring",
+ "rustls-pki-types",
+ "rustls-webpki",
+ "subtle",
+ "zeroize",
+]
+
+[[package]]
+name = "rustls-pki-types"
+version = "1.14.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "30a7197ae7eb376e574fe940d068c30fe0462554a3ddbe4eca7838e049c937a9"
+dependencies = [
+ "zeroize",
+]
+
+[[package]]
+name = "rustls-webpki"
+version = "0.103.13"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "61c429a8649f110dddef65e2a5ad240f747e85f7758a6bccc7e5777bd33f756e"
+dependencies = [
+ "ring",
+ "rustls-pki-types",
+ "untrusted",
+]
+
[[package]]
name = "serde"
version = "1.0.228"
@@ -384,12 +606,24 @@ version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
+[[package]]
+name = "simd-adler32"
+version = "0.3.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "703d5c7ef118737c72f1af64ad2f6f8c5e1921f818cdcb97b8fe6fc69bf66214"
+
[[package]]
name = "strsim"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
+[[package]]
+name = "subtle"
+version = "2.6.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
+
[[package]]
name = "syn"
version = "2.0.117"
@@ -401,24 +635,100 @@ dependencies = [
"unicode-ident",
]
+[[package]]
+name = "tar"
+version = "0.4.45"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "22692a6476a21fa75fdfc11d452fda482af402c008cdbaf3476414e122040973"
+dependencies = [
+ "filetime",
+ "libc",
+ "xattr",
+]
+
[[package]]
name = "unicode-ident"
version = "1.0.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
+[[package]]
+name = "untrusted"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1"
+
+[[package]]
+name = "ureq"
+version = "3.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "dea7109cdcd5864d4eeb1b58a1648dc9bf520360d7af16ec26d0a9354bafcfc0"
+dependencies = [
+ "base64",
+ "flate2",
+ "log",
+ "percent-encoding",
+ "rustls",
+ "rustls-pki-types",
+ "ureq-proto",
+ "utf8-zero",
+ "webpki-roots",
+]
+
+[[package]]
+name = "ureq-proto"
+version = "0.6.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e994ba84b0bd1b1b0cf92878b7ef898a5c1760108fe7b6010327e274917a808c"
+dependencies = [
+ "base64",
+ "http",
+ "httparse",
+ "log",
+]
+
+[[package]]
+name = "utf8-zero"
+version = "0.8.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b8c0a043c9540bae7c578c88f91dda8bd82e59ae27c21baca69c8b191aaf5a6e"
+
[[package]]
name = "utf8parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
+[[package]]
+name = "wasi"
+version = "0.11.1+wasi-snapshot-preview1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b"
+
+[[package]]
+name = "webpki-roots"
+version = "1.0.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "52f5ee44c96cf55f1b349600768e3ece3a8f26010c05265ab73f945bb1a2eb9d"
+dependencies = [
+ "rustls-pki-types",
+]
+
[[package]]
name = "windows-link"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
+[[package]]
+name = "windows-sys"
+version = "0.52.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
+dependencies = [
+ "windows-targets",
+]
+
[[package]]
name = "windows-sys"
version = "0.61.2"
@@ -428,6 +738,86 @@ dependencies = [
"windows-link",
]
+[[package]]
+name = "windows-targets"
+version = "0.52.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
+dependencies = [
+ "windows_aarch64_gnullvm",
+ "windows_aarch64_msvc",
+ "windows_i686_gnu",
+ "windows_i686_gnullvm",
+ "windows_i686_msvc",
+ "windows_x86_64_gnu",
+ "windows_x86_64_gnullvm",
+ "windows_x86_64_msvc",
+]
+
+[[package]]
+name = "windows_aarch64_gnullvm"
+version = "0.52.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
+
+[[package]]
+name = "windows_aarch64_msvc"
+version = "0.52.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
+
+[[package]]
+name = "windows_i686_gnu"
+version = "0.52.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
+
+[[package]]
+name = "windows_i686_gnullvm"
+version = "0.52.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
+
+[[package]]
+name = "windows_i686_msvc"
+version = "0.52.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
+
+[[package]]
+name = "windows_x86_64_gnu"
+version = "0.52.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
+
+[[package]]
+name = "windows_x86_64_gnullvm"
+version = "0.52.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
+
+[[package]]
+name = "windows_x86_64_msvc"
+version = "0.52.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
+
+[[package]]
+name = "xattr"
+version = "1.6.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "32e45ad4206f6d2479085147f02bc2ef834ac85886624a23575ae137c8aa8156"
+dependencies = [
+ "libc",
+ "rustix",
+]
+
+[[package]]
+name = "zeroize"
+version = "1.8.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b97154e67e32c85465826e8bcc1c59429aaaf107c1e4a9e53c8d8ccd5eff88d0"
+
[[package]]
name = "zmij"
version = "1.0.21"
diff --git a/packages/rust/crates/liteparse/Cargo.toml b/packages/rust/crates/liteparse/Cargo.toml
index 86dfcdd..051f909 100644
--- a/packages/rust/crates/liteparse/Cargo.toml
+++ b/packages/rust/crates/liteparse/Cargo.toml
@@ -6,5 +6,6 @@ edition = "2024"
[dependencies]
clap = { version = "4.5.55", features = ["derive"] }
pdfium = { path = "../pdfium" }
+pdfium-sys = { path = "../pdfium-sys" }
serde = { version = "1.0.228", features = ["derive"] }
serde_json = "1.0.149"
diff --git a/packages/rust/crates/liteparse/build.rs b/packages/rust/crates/liteparse/build.rs
index 32e8221..c297276 100644
--- a/packages/rust/crates/liteparse/build.rs
+++ b/packages/rust/crates/liteparse/build.rs
@@ -1,16 +1,9 @@
use std::env;
-use std::path::PathBuf;
fn main() {
- // Resolve pdfium dylib path the same way pdfium-sys does
- let lib_path = if let Ok(p) = env::var("PDFIUM_LIB_PATH") {
- PathBuf::from(p)
- } else {
- let manifest = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
- manifest.join("../../vendor/pdfium/release/lib")
- };
-
- if let Ok(lib_path) = lib_path.canonicalize() {
- println!("cargo:rustc-link-arg=-Wl,-rpath,{}", lib_path.display());
+ // DEP_PDFIUM_LIB_PATH is exported by pdfium-sys's build.rs via `cargo:lib_path=...`
+ // Available because liteparse directly depends on pdfium-sys (links = "pdfium").
+ if let Ok(lib_path) = env::var("DEP_PDFIUM_LIB_PATH") {
+ println!("cargo:rustc-link-arg=-Wl,-rpath,{lib_path}");
}
}
diff --git a/packages/rust/crates/pdfium-sys/Cargo.toml b/packages/rust/crates/pdfium-sys/Cargo.toml
index 7521ebd..18600ff 100644
--- a/packages/rust/crates/pdfium-sys/Cargo.toml
+++ b/packages/rust/crates/pdfium-sys/Cargo.toml
@@ -6,3 +6,6 @@ links = "pdfium"
[build-dependencies]
bindgen = "0.71"
+flate2 = "1"
+tar = "0.4"
+ureq = "3"
diff --git a/packages/rust/crates/pdfium-sys/build.rs b/packages/rust/crates/pdfium-sys/build.rs
index 8c41146..5b997ac 100644
--- a/packages/rust/crates/pdfium-sys/build.rs
+++ b/packages/rust/crates/pdfium-sys/build.rs
@@ -1,32 +1,164 @@
use std::env;
-use std::path::PathBuf;
+use std::fs;
+use std::path::{Path, PathBuf};
+use std::process::Command;
+
+const PDFIUM_RELEASE_TAG: &str = "chromium/7827";
+const PDFIUM_RELEASE_URL: &str =
+ "https://github.com/run-llama/pdfium-binaries/releases/download";
fn main() {
- let lib_path = if let Ok(p) = env::var("PDFIUM_LIB_PATH") {
- PathBuf::from(p)
- } else {
- let manifest = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
- manifest.join("../../vendor/pdfium/release/lib")
- };
+ let (lib_dir, include_dir) = resolve_pdfium_dirs();
- let include_path = if let Ok(p) = env::var("PDFIUM_INCLUDE_PATH") {
- PathBuf::from(p)
- } else {
- let manifest = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
- manifest.join("../../vendor/pdfium/release/include")
- };
+ let lib_dir = lib_dir.canonicalize().unwrap_or_else(|e| {
+ panic!("pdfium lib dir does not exist at {}: {e}", lib_dir.display())
+ });
+ let include_dir = include_dir.canonicalize().unwrap_or_else(|e| {
+ panic!("pdfium include dir does not exist at {}: {e}", include_dir.display())
+ });
- let lib_path = lib_path.canonicalize().expect("pdfium lib path does not exist");
- println!("cargo:rustc-link-search=native={}", lib_path.display());
+ println!("cargo:rustc-link-search=native={}", lib_dir.display());
println!("cargo:rustc-link-lib=dylib=pdfium");
+ println!("cargo:lib_path={}", lib_dir.display());
+
+ run_bindgen(&include_dir);
+}
+
+/// Determine where pdfium lib and include dirs are.
+/// Priority: env vars > vendor/ dir > auto-download to cache.
+fn resolve_pdfium_dirs() -> (PathBuf, PathBuf) {
+ // 1. Explicit env var override
+ if let (Ok(lib), Ok(inc)) = (env::var("PDFIUM_LIB_PATH"), env::var("PDFIUM_INCLUDE_PATH")) {
+ return (PathBuf::from(lib), PathBuf::from(inc));
+ }
+
+ // 2. Vendor directory relative to workspace root
+ let manifest = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
+ let vendor_lib = manifest.join("../../vendor/pdfium/release/lib");
+ let vendor_include = manifest.join("../../vendor/pdfium/release/include");
+ if vendor_lib.exists() && vendor_include.exists() {
+ return (vendor_lib, vendor_include);
+ }
+
+ // 3. Auto-download to cache
+ let cache_dir = pdfium_cache_dir();
+ let lib_dir = cache_dir.join("lib");
+ let include_dir = cache_dir.join("include");
+
+ if lib_dir.exists() && include_dir.exists() {
+ return (lib_dir, include_dir);
+ }
+
+ eprintln!("pdfium-sys: downloading pdfium from GitHub releases...");
+ download_pdfium(&cache_dir);
+
+ (lib_dir, include_dir)
+}
+
+/// ~/.cache/pdfium-rs/<tag>/<asset_stem>/
+fn pdfium_cache_dir() -> PathBuf {
+ let base = dirs_cache().join("pdfium-rs");
+ let tag_safe = PDFIUM_RELEASE_TAG.replace('/', "_");
+ let asset = pdfium_asset_stem();
+ base.join(tag_safe).join(asset)
+}
+
+/// Platform cache dir: $XDG_CACHE_HOME, ~/Library/Caches, or ~/.cache
+fn dirs_cache() -> PathBuf {
+ if let Ok(xdg) = env::var("XDG_CACHE_HOME") {
+ return PathBuf::from(xdg);
+ }
+ let home = env::var("HOME").expect("HOME env var not set");
+ let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
+ if target_os == "macos" {
+ PathBuf::from(&home).join("Library/Caches")
+ } else {
+ PathBuf::from(&home).join(".cache")
+ }
+}
+
+/// Map target triple to the pdfium-binaries asset name (without .tgz).
+fn pdfium_asset_stem() -> &'static str {
+ let target = env::var("TARGET").unwrap();
+ match target.as_str() {
+ "aarch64-apple-darwin" => "pdfium-mac-arm64",
+ "x86_64-apple-darwin" => "pdfium-mac-x64",
+ // Universal macOS binary works for both, but we prefer arch-specific
+ "x86_64-unknown-linux-gnu" | "x86_64-unknown-linux-musl" => "pdfium-linux-x64",
+ "aarch64-unknown-linux-gnu" | "aarch64-unknown-linux-musl" => "pdfium-linux-arm64",
+ "armv7-unknown-linux-gnueabihf" => "pdfium-linux-arm",
+ "x86_64-pc-windows-msvc" | "x86_64-pc-windows-gnu" => "pdfium-win-x64",
+ "aarch64-pc-windows-msvc" => "pdfium-win-arm64",
+ "i686-pc-windows-msvc" | "i686-pc-windows-gnu" => "pdfium-win-x86",
+ "wasm32-unknown-unknown" | "wasm32-wasip1" => "pdfium-wasm",
+ other => panic!("unsupported target for pdfium auto-download: {other}"),
+ }
+}
+
+fn download_pdfium(dest: &Path) {
+ let asset = format!("{}.tgz", pdfium_asset_stem());
+ let tag_encoded = PDFIUM_RELEASE_TAG.replace('/', "%2F");
+ let url = format!("{PDFIUM_RELEASE_URL}/{tag_encoded}/{asset}");
+
+ eprintln!("pdfium-sys: GET {url}");
+
+ let response = ureq::get(&url).call().unwrap_or_else(|e| {
+ panic!("failed to download pdfium from {url}: {e}");
+ });
+
+ let reader = response.into_body().into_reader();
+ let gz = flate2::read::GzDecoder::new(reader);
+ let mut archive = tar::Archive::new(gz);
+
+ // Extract to a temp dir first, then rename atomically
+ let tmp = dest.with_extension("tmp");
+ if tmp.exists() {
+ fs::remove_dir_all(&tmp).ok();
+ }
+ fs::create_dir_all(&tmp).expect("failed to create temp dir");
+ archive.unpack(&tmp).expect("failed to extract pdfium archive");
- // Export the lib path so downstream crates can find it
- println!("cargo:lib_path={}", lib_path.display());
+ // Fix dylib install name on macOS so @rpath resolution works
+ fix_dylib_install_name(&tmp);
+
+ // Atomic rename into place
+ if dest.exists() {
+ fs::remove_dir_all(dest).ok();
+ }
+ fs::rename(&tmp, dest).expect("failed to move pdfium to cache dir");
+
+ eprintln!("pdfium-sys: cached pdfium at {}", dest.display());
+}
+
+/// On macOS, pdfium-binaries ships dylibs with install name `./libpdfium.dylib`.
+/// We need `@rpath/libpdfium.dylib` for rpath resolution to work.
+fn fix_dylib_install_name(dir: &Path) {
+ let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
+ if target_os != "macos" {
+ return;
+ }
+
+ let dylib = dir.join("lib/libpdfium.dylib");
+ if !dylib.exists() {
+ return;
+ }
+
+ let status = Command::new("install_name_tool")
+ .args(["-id", "@rpath/libpdfium.dylib"])
+ .arg(&dylib)
+ .status();
+
+ match status {
+ Ok(s) if s.success() => {}
+ Ok(s) => eprintln!("pdfium-sys: install_name_tool exited with {s}"),
+ Err(e) => eprintln!("pdfium-sys: failed to run install_name_tool: {e}"),
+ }
+}
- // Run bindgen
+fn run_bindgen(include_dir: &Path) {
let bindings = bindgen::Builder::default()
.header("wrapper.h")
- .clang_arg(format!("-I{}", include_path.display()))
+ .clang_arg(format!("-I{}", include_dir.display()))
.allowlist_function("FPDF.*")
.allowlist_function("FPDFText_.*")
.allowlist_function("FPDFPage.*")