README

#pdfread

Read PDF files and construct in-memory Pdf documents.

#Overview

The pdfread package provides functions to:

  • Read PDF files from disk, channels, or memory
  • Parse cross-reference tables and object streams
  • Handle encrypted documents with password support
  • Support lazy loading for large files
  • Query document revisions and encryption

#Types

#PdfRead

PDF reading context.

pub struct PdfRead { ... }
pub fn PdfRead::new() -> PdfRead

#Reading PDFs

#From Input Stream

// Read with all stream data loaded

///|
let pdf = @pdfread.PdfRead::new().pdf_of_input(
user_password=None,
owner_password=None,
input,
)

#Lazy Loading

For large files, use lazy loading to defer stream data:

// Streams loaded on-demand

///|
let pdf = @pdfread.PdfRead::new().pdf_of_input_lazy(
user_password=None,
owner_password=None,
input,
)

#From File (async)

///|
let pdf = @pdfreadfs.PdfReadFs::new().pdf_of_file(
user_password=None,
owner_password=None,
filename="/path/to/document.pdf",
)

#From Channel (async)

///|
let pdf = @pdfreadfs.PdfReadFs::new().pdf_of_channel(
user_password=None,
owner_password=None,
channel,
)

#Password-Protected Documents

For encrypted PDFs:

// With user password

///|
let pdf = @pdfread.PdfRead::new().pdf_of_input(
user_password=Some("secret"),
owner_password=None,
input,
)

// With owner password

///|
let pdf = @pdfread.PdfRead::new().pdf_of_input(
user_password=None,
owner_password=Some("admin123"),
input,
)

#Document Revisions

PDF files can have multiple revisions (incremental saves):

// Count revisions

///|
let num_revisions = @pdfread.PdfRead::new().revisions(input)

// Read specific revision

///|
let pdf = @pdfread.PdfRead::new().pdf_of_input(
revision=2,
user_password=None,
owner_password=None, // Read second revision
input,
)

#Encryption Information

#Query Encryption Method

let method = @pdfread.PdfRead::new().what_encryption(pdf) match method { None => println("Not encrypted") Some(AES128) => println("AES 128-bit") Some(AES256) => println("AES 256-bit") Some(ARC4(40)) => println("RC4 40-bit") Some(ARC4(128)) => println("RC4 128-bit") _ => () }

#Query Permissions

let perms = @pdfread.PdfRead::new().permissions(pdf)
for perm in perms {
match perm {
Print => println("Printing allowed")
Copy => println("Copying allowed")
Edit => println("Editing allowed")
_ => ()
}
}

#Debug Options

// Enable debug output

///|
let reader = @pdfread.PdfRead::new(read_debug=true)

// Treat all documents as malformed (for testing)

///|
let reader_force_malformed = @pdfread.PdfRead::new(
debug_always_treat_malformed=true,
)

// Raise errors on malformed documents

///|
let reader_strict = @pdfread.PdfRead::new(error_on_malformed=true)

#Error Handling

Reading raises @pdf.PdfError on parse failures:

try {
let pdf = @pdfread.PdfRead::new().pdf_of_input!(
user_password=None,
owner_password=None,
input,
)
// use pdf...
} catch {
@pdf.PdfError::Msg(msg) => println("Parse error: \{msg}")
}

#Internal Structure

The package handles:

  • Cross-reference tables: Traditional xref tables and compressed xref streams
  • Object streams: Compressed object storage (PDF 1.5+)
  • Linearization: Optimized web viewing (detected and handled)
  • Encryption: RC4 and AES decryption with key derivation

#
PdfRead

pub struct PdfRead {
read_debug : Bool
error_on_malformed : Bool
debug_always_treat_malformed : Bool
logger : (String) -> Unit
}

#
PdfRead::is_linearized

fn PdfRead::is_linearized(self : PdfRead, input :
Input
) -> Bool

Check if input is linearized.

#
PdfRead::new

fn PdfRead::new(read_debug? : Bool, error_on_malformed? : Bool, debug_always_treat_malformed? : Bool, logger? : (String) -> Unit) -> PdfRead

#
PdfRead::pdf_of_input

fn PdfRead::pdf_of_input(self : PdfRead, revision? : Int, user_password : String?, owner_password : String?, input :
Input
) ->
Pdf
raise

Read a PDF from an input.

#
PdfRead::pdf_of_input_lazy

fn PdfRead::pdf_of_input_lazy(self : PdfRead, revision? : Int, user_password : String?, owner_password : String?, input :
Input
) ->
Pdf
raise

Read a PDF from an input, lazily.

#
PdfRead::permissions

Return list of permissions.

#
PdfRead::revisions

fn PdfRead::revisions(self : PdfRead, input :
Input
) -> Int raise

Return number of revisions.

#
PdfRead::what_encryption

Return encryption method in use.

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io