Skip to main content

PdfReader

Struct PdfReader 

Source
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

Source

pub fn extract_text(&self) -> Result<String>

从所有选定页面提取文本,以换行符连接。

对于 ReadStrategy::Streaming,直接扫描原始字节而不构建 lopdf::Document 对象树。

§Errors

当 PDF 内容无法读取时返回 PdfError::Parse

Source

pub fn extract_metadata(&self) -> Result<PdfMetadata>

从 PDF 文档中提取元数据。

对于 ReadStrategy::Streaming,对原始字节进行启发式扫描 (不解析 xref)。对于其他策略,通过已解析的 lopdf::Document 读取 /Info 字典。

§Errors

当文档无法读取时返回 PdfError::Parse

§Panics

在非 Streaming 读取器上调用且 documentNone 时 panic (正常使用中不应发生)。

Source

pub fn page_count(&self) -> Result<usize>

获取文档的总页数。

对于 ReadStrategy::Streaming,基于原始字节中 /Type /Page 条目的启发式计数返回结果。

§Errors

当文档无法读取时返回 PdfError::Parse

§Panics

在非 Streaming 读取器上调用且 documentNone 时 panic (正常使用中不应发生)。

Source

pub fn extract_document_model(&self) -> Result<PdfDocumentModel>

提取引擎中立的语义文档模型。

初始读取器后端输出段落块。更高级的分析器可以后续用标题、 表格、图片和 OCR 结果来丰富这些块。

§Errors

当文本提取失败或超出资源限制时返回错误。

Source

pub fn read_with_listener( &self, listener: &mut dyn PdfReadListener, ) -> Result<()>

使用事件驱动监听器读取文档(动态分发)。

此方法使用 &mut dyn PdfReadListener 进行动态分发。 如需零开销单态化,请使用 Self::read_with_listener_typed

§Errors

当文档无法读取时返回 PdfError::Parse

Source

pub fn read_with_listener_typed<L: PdfReadListener>( &self, listener: &mut L, ) -> Result<()>

使用类型化的事件驱动监听器读取文档(静态分发)。

这是 read_with_listener 的单态化版本。 编译器为每个具体的监听器类型生成特化版本,消除 vtable 开销。

§Errors

当文档无法读取时返回 PdfError::Parse

Source

pub fn extract_text_lazy(&mut self) -> Result<String>

使用懒加载策略提取文本。

当读取器以 ReadStrategy::Lazy 打开时,此方法使用懒加载页面 加载器按需加载页面内容并带缓存。对于 ReadStrategy::Full, 委托给 extract_text。对于 ReadStrategy::Streaming,委托给流式扫描器。

§Errors

当页面内容无法解码时返回错误。

§Panics

在非 Streaming 读取器上调用且 documentNone 时 panic (正常使用中不应发生)。

Source§

impl PdfReader

Source

pub fn open(path: impl AsRef<Path>) -> Result<Self>

打开 PDF 文件进行读取,自动选择解析策略。

根据文件大小自动选择 ReadStrategy: 5 MB 以下使用 Full, 5–100 MB 使用 Lazy, 更大的文件使用 Streaming

§Errors

当文件无法打开或不是有效的 PDF 时,返回 PdfError::Parse

Source

pub fn from_bytes(bytes: impl Into<Vec<u8>>) -> Result<Self>

从内存字节打开 PDF,自动选择解析策略。

§Errors

当字节数据不是有效的 PDF 时,返回 PdfError::Parse

Source

pub fn open_with_limits( input: &PdfInput, limits: ResourceLimits, ) -> Result<Self>

使用指定的资源限制打开 PDF 输入。

文档仅解析一次并由读取器会话保留。

§Errors

当输入超出限制或解析失败时返回错误。

Source

pub fn open_with_strategy( path: impl AsRef<Path>, strategy: ReadStrategy, ) -> Result<Self>

使用指定的 ReadStrategy 打开 PDF 文件。

§Errors

当文件无法读取、解析或超出资源限制时返回错误。

Source

pub fn open_with_repair( path: impl AsRef<Path>, repair: RepairOptions, strategy: ReadStrategy, ) -> Result<Self>

使用指定的修复选项和读取策略打开 PDF 文件。

如果 is_likely_corrupt 检测到损坏,将使用提供的 RepairOptions 调用 attempt_repair 进行修复后再加载。

§Errors

当文件无法读取、修复、解析或超出资源限制时返回错误。

Source

pub fn open_with_limits_and_strategy( input: &PdfInput, limits: ResourceLimits, strategy: ReadStrategy, ) -> Result<Self>

使用指定的资源限制和读取策略打开 PDF 输入。

§Errors

当输入超出限制或解析失败时返回错误。

Source

pub const fn strategy(&self) -> ReadStrategy

返回此读取器打开时使用的读取策略。

Source

pub fn pages(self, range: Range<usize>) -> Self

将提取范围限制为指定的页面范围(从 0 开始)。

Source

pub fn try_pages(self, range: Range<usize>) -> Result<Self>

尝试将提取范围限制为经验证的从零开始的页面范围。

§Errors

当范围反转(起始大于结束)时返回错误。

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more