pub struct PdfReader { /* private fields */ }Expand description
从 PDF 文档中提取内容的读取器。
底层使用 lopdf crate 进行低层 PDF 解析。支持多种读取策略
(ReadStrategy),可根据文档大小自动选择最优解析方式。
§Examples
use easypdf_reader::PdfReader;
let text = PdfReader::open("document.pdf")?.extract_text()?;Implementations§
Source§impl PdfReader
impl PdfReader
Sourcepub fn extract_text(&self) -> Result<String>
pub fn extract_text(&self) -> Result<String>
从所有选定页面提取文本,以换行符连接。
对于 ReadStrategy::Streaming,直接扫描原始字节而不构建
lopdf::Document 对象树。
§Errors
当 PDF 内容无法读取时返回 PdfError::Parse。
Sourcepub fn extract_metadata(&self) -> Result<PdfMetadata>
pub fn extract_metadata(&self) -> Result<PdfMetadata>
从 PDF 文档中提取元数据。
对于 ReadStrategy::Streaming,对原始字节进行启发式扫描
(不解析 xref)。对于其他策略,通过已解析的 lopdf::Document
读取 /Info 字典。
§Errors
当文档无法读取时返回 PdfError::Parse。
§Panics
在非 Streaming 读取器上调用且 document 为 None 时 panic
(正常使用中不应发生)。
Sourcepub fn page_count(&self) -> Result<usize>
pub fn page_count(&self) -> Result<usize>
获取文档的总页数。
对于 ReadStrategy::Streaming,基于原始字节中 /Type /Page
条目的启发式计数返回结果。
§Errors
当文档无法读取时返回 PdfError::Parse。
§Panics
在非 Streaming 读取器上调用且 document 为 None 时 panic
(正常使用中不应发生)。
Sourcepub fn extract_document_model(&self) -> Result<PdfDocumentModel>
pub fn extract_document_model(&self) -> Result<PdfDocumentModel>
Sourcepub fn read_with_listener(
&self,
listener: &mut dyn PdfReadListener,
) -> Result<()>
pub fn read_with_listener( &self, listener: &mut dyn PdfReadListener, ) -> Result<()>
使用事件驱动监听器读取文档(动态分发)。
此方法使用 &mut dyn PdfReadListener 进行动态分发。
如需零开销单态化,请使用 Self::read_with_listener_typed。
§Errors
当文档无法读取时返回 PdfError::Parse。
Sourcepub fn read_with_listener_typed<L: PdfReadListener>(
&self,
listener: &mut L,
) -> Result<()>
pub fn read_with_listener_typed<L: PdfReadListener>( &self, listener: &mut L, ) -> Result<()>
使用类型化的事件驱动监听器读取文档(静态分发)。
这是 read_with_listener 的单态化版本。
编译器为每个具体的监听器类型生成特化版本,消除 vtable 开销。
§Errors
当文档无法读取时返回 PdfError::Parse。
Sourcepub fn extract_text_lazy(&mut self) -> Result<String>
pub fn extract_text_lazy(&mut self) -> Result<String>
使用懒加载策略提取文本。
当读取器以 ReadStrategy::Lazy 打开时,此方法使用懒加载页面
加载器按需加载页面内容并带缓存。对于 ReadStrategy::Full,
委托给 extract_text。对于
ReadStrategy::Streaming,委托给流式扫描器。
§Errors
当页面内容无法解码时返回错误。
§Panics
在非 Streaming 读取器上调用且 document 为 None 时 panic
(正常使用中不应发生)。
Source§impl PdfReader
impl PdfReader
Sourcepub fn open(path: impl AsRef<Path>) -> Result<Self>
pub fn open(path: impl AsRef<Path>) -> Result<Self>
打开 PDF 文件进行读取,自动选择解析策略。
根据文件大小自动选择 ReadStrategy:
5 MB 以下使用 Full,
5–100 MB 使用 Lazy,
更大的文件使用 Streaming。
§Errors
当文件无法打开或不是有效的 PDF 时,返回 PdfError::Parse。
Sourcepub fn open_with_limits(
input: &PdfInput,
limits: ResourceLimits,
) -> Result<Self>
pub fn open_with_limits( input: &PdfInput, limits: ResourceLimits, ) -> Result<Self>
Sourcepub fn open_with_strategy(
path: impl AsRef<Path>,
strategy: ReadStrategy,
) -> Result<Self>
pub fn open_with_strategy( path: impl AsRef<Path>, strategy: ReadStrategy, ) -> Result<Self>
Sourcepub fn open_with_repair(
path: impl AsRef<Path>,
repair: RepairOptions,
strategy: ReadStrategy,
) -> Result<Self>
pub fn open_with_repair( path: impl AsRef<Path>, repair: RepairOptions, strategy: ReadStrategy, ) -> Result<Self>
使用指定的修复选项和读取策略打开 PDF 文件。
如果 is_likely_corrupt 检测到损坏,将使用提供的
RepairOptions 调用 attempt_repair 进行修复后再加载。
§Errors
当文件无法读取、修复、解析或超出资源限制时返回错误。
Sourcepub fn open_with_limits_and_strategy(
input: &PdfInput,
limits: ResourceLimits,
strategy: ReadStrategy,
) -> Result<Self>
pub fn open_with_limits_and_strategy( input: &PdfInput, limits: ResourceLimits, strategy: ReadStrategy, ) -> Result<Self>
Sourcepub const fn strategy(&self) -> ReadStrategy
pub const fn strategy(&self) -> ReadStrategy
返回此读取器打开时使用的读取策略。
Auto Trait Implementations§
impl !RefUnwindSafe for PdfReader
impl !UnwindSafe for PdfReader
impl Freeze for PdfReader
impl Send for PdfReader
impl Sync for PdfReader
impl Unpin for PdfReader
impl UnsafeUnpin for PdfReader
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more