ExQPDF (ExQPDF v0.1.1)

View Source

An Elixir wrapper for the QPDF library.

QPDF is a command-line program and C++ library for structural, content-preserving transformations on PDF files. ExQPDF provides a simple interface to some of its functionality, focusing on retrieving PDF information and handling password-protected files.

Features

  • Check if a PDF requires a password to open
  • Get information about PDF files (page count, etc.)
  • Open PDFs with or without passwords for future operations

Requirements

This library requires the QPDF command-line tool to be installed on your system. See the README for installation instructions for various platforms.

Examples

Check if a PDF requires a password:

path = "/path/to/document.pdf"
case ExQPDF.password_required?(path) do
  {:ok, true} -> "Password required"
  {:ok, false} -> "No password required"
  {:error, reason} -> "Error: #{reason}"
end

Get information about a PDF:

# For a PDF without password protection
{:ok, info} = ExQPDF.info("/path/to/document.pdf")
"Page count: #{info.page_count}"

# For a password-protected PDF
{:ok, info} = ExQPDF.info("/path/to/protected.pdf", password: "secret")
"Page count: #{info.page_count}"

Summary

Functions

Gets general information about a PDF file.

Extracts metadata from a PDF file.

Opens a PDF file, allowing operations that might require a password.

Checks if a PDF file requires a password to open.

Functions

info(path, opts \\ [])

@spec info(
  String.t(),
  keyword()
) :: {:ok, map()} | {:error, String.t()}

Gets general information about a PDF file.

Parameters

  • path - Path to the PDF file
  • opts - Options including:
    • :password - Password for protected PDFs

Returns

  • {:ok, info} - A map containing file information
  • {:error, reason} - An error occurred

Examples

# If the file doesn't exist, an error is returned
iex> ExQPDF.info("non_existent_file.pdf")
{:error, "File not found: non_existent_file.pdf"}

# With optional password parameter
iex> ExQPDF.info("non_existent_file.pdf", password: "secret")
{:error, "File not found: non_existent_file.pdf"}

metadata(path, opts \\ [])

@spec metadata(
  String.t(),
  keyword()
) :: {:ok, map()} | {:error, String.t()}

Extracts metadata from a PDF file.

This function extracts metadata such as title, author, creation date, and more from a PDF file. For password-protected PDFs, a password can be provided.

Parameters

  • path - Path to the PDF file
  • opts - Options including:
    • :password - Password for protected PDFs

Returns

  • {:ok, metadata} - A map containing metadata information
  • {:error, reason} - An error occurred

Examples

# If the file doesn't exist, an error is returned
iex> ExQPDF.metadata("non_existent_file.pdf")
{:error, "File not found: non_existent_file.pdf"}

open(path, opts \\ [])

@spec open(
  String.t(),
  keyword()
) :: {:ok, ExQPDF.Handle.t()} | {:error, String.t()}

Opens a PDF file, allowing operations that might require a password.

Parameters

  • path - Path to the PDF file
  • opts - Options including:
    • :password - Password for protected PDFs

Returns

  • {:ok, pdf_handle} - A handle for further operations
  • {:error, reason} - An error occurred

Examples

# If the file doesn't exist, an error is returned
iex> ExQPDF.open("non_existent_file.pdf")
{:error, "File not found: non_existent_file.pdf"}

# With optional password parameter
iex> ExQPDF.open("non_existent_file.pdf", password: "secret")
{:error, "File not found: non_existent_file.pdf"}

password_required?(path)

@spec password_required?(String.t()) :: {:ok, boolean()} | {:error, String.t()}

Checks if a PDF file requires a password to open.

Parameters

  • path - Path to the PDF file

Returns

  • {:ok, true} - The PDF is password-protected
  • {:ok, false} - The PDF is not password-protected
  • {:error, reason} - An error occurred

Examples

# If the file doesn't exist, an error is returned
iex> ExQPDF.password_required?("non_existent_file.pdf")
{:error, "File not found: non_existent_file.pdf"}