HexaPDF Documentation

repository·master·Indexed 21 days ago

https://github.com/gettalong/hexapdf

A pure Ruby library and command-line tool for creating, manipulating, merging, and optimizing PDF files. Unlike generation-only libraries, HexaPDF is a full PDF engine capable of reading and modifying existing PDF structures. Key features include a low-level Canvas API, a high-level document composition engine for automatic layout, support for PDF forms (AcroForm), AES256 encryption, digital signatures, and lossless file size optimization.

Tokens
13.8K
Snippets
13
Records
92
Agent score
79%

What's inside HexaPDF

  1. HexaPDF core features overview

    master

    HexaPDF provides a wide range of capabilities for PDF work:

    Creation & Manipulation

    • Creating new PDFs and merging multiple files.
    • Manipulating existing files and optimizing file size.
    • Canvas API: A low-level API that maps directly to PDF internal operators for path drawing (lines, curves, etc.).
    • Document Composition Engine: High-level layout features including flowing text, automatic page breaks, styles, and multi-column layouts.

    Extraction & Inspection

    • Extracting metadata, text, images, and attached files.
    • PDF object validation.

    Security & Interactivity

    • Encryption: Supports PDF 2.0 features like AES256.
    • Digital Signatures.
    • PDF Forms (AcroForm): Supports Adobe Reader-like appearance generation.
    • Annotations and Document Outlines.
  2. Use the hexapdf CLI application

    master

    The hexapdf command-line application is used for various PDF manipulation tasks including modifying, merging, splitting, optimizing, watermarking, and more.

    Basic Syntax: hexapdf [OPTIONS] command [COMMAND OPTIONS]...

    Available Commands:

    • modify: Modifying an existing PDF file
    • merge: Merging multiple PDF files into one
    • split: Splitting a PDF file into subsets
    • optimize: Optimizing the file size of a PDF file
    • watermark: Watermarking/Stamping a PDF onto another one
    • form: Filling out an interactive PDF form
    • files: Extracting embedded files
    • images: Extracting images
    • image2pdf: Converting images to PDF
    • info: Showing general information of a PDF file
    • fonts: Listing all fonts of a PDF file
    • usage: Show space usage information of a PDF file
    • batch: Batch execution of a command on multiple PDF files
    • inspect: Inspecting the internal structure of a PDF file
    • help: Show help for the application or a specific command
  3. HexaPDF vs Prawn: When to use which

    master

    Choosing between HexaPDF and Prawn depends on your primary goal:

    Use HexaPDF if:

    • You need a full PDF library that can read, manipulate, and modify existing PDF files (e.g., removing annotations or changing the object structure).
    • You need advanced features like PDF forms (AcroForm), encryption (including AES256), digital signatures, or file attachments.
    • You want to use existing PDFs as templates.
    • You need a high-level document composition engine with automatic layout (flowing text, multi-column, etc.).

    Use Prawn if:

    • Your primary goal is strictly generating content from scratch and you do not need to manipulate existing PDF structures.
  4. Specify pages using the PAGES argument

    master

    Many hexapdf commands accept a PAGES argument to target specific parts of a document. This argument supports comma-separated single page numbers, ranges, and special modifiers:

    Page Selection Syntax

    • Single Pages: A comma-separated list (e.g., 1,2,3).
    • Ranges: Use the format START-END (e.g., 4-9). If the start number is higher than the end number, pages are selected in reverse order.
    • Last Page: Use e to represent the last page.
    • Reverse Counting: Prefix a number with r to count from the end (e.g., r1 is the last page, r3 is the third last page).
    • All Pages: Use 1-e or 1-r1.
    • Step Values: Use */STEP after a range to skip pages. STEP defines how many pages are skipped after each used page (e.g., 1-5/2 selects pages 1, 3, and 5).

    Page Rotation Modifiers

    You can suffix page numbers or ranges with rotation modifiers (supported by commands like modify):

    • l: Rotate left (90 degrees counterclockwise).
    • r: Rotate right (90 degrees clockwise).
    • d: Rotate 180 degrees.
    • n: Remove any existing page rotation.

    Behavior Notes

    • Invalid single page numbers are ignored.
    • If a range exceeds the document length, the last page is used instead of the requested number.
    • Pages are processed in the exact order specified.
  5. HexaPDF Layouting Benchmark Methods

    master

    HexaPDF is benchmarked using two distinct layouting approaches:

    1. Low-level Layouting (L): Uses the HexaPDF::Layout::TextLayouter facility and processes the entire input file at once. This is generally the fastest method for HexaPDF.
    2. High-level Composition (C): Uses HexaPDF::Composer to construct the document. To manage peak memory usage, this method processes the input in chunks of 1000 lines at a time.

    Note: HexaPDF performs better when the whole input is provided at once, though it maintains acceptable runtimes for line-by-line input.

  6. Generate benchmark graphs using Gnuplot

    master

    You can generate graphs from benchmark output using the plot.sh script. The script requires the name of the benchmark directory as its first argument. Any additional arguments provided to plot.sh are passed directly to the underlying script.sh file.

    ./plot.sh raw_text "1x 5x"
  7. Run the Simple Text Rendering Benchmark

    master

    The simple text rendering benchmark tests raw text output performance by writing the text of Homer's Odyssey onto A4 pages with a 72-point margin. It uses basic text drawing methods without line wrapping or text measuring.

    To run the benchmark for HexaPDF (and other supported libraries), use the following command structure:

    script-executable TXT_FILE OUTPUT_FILE [TTF_FILE]

    Where:

    • script-executable: The executable script for the library being tested.
    • TXT_FILE: The source text file (e.g., the Odyssey text).
    • OUTPUT_FILE: The destination PDF file.
    • [TTF_FILE]: (Optional) A path to a TrueType font file. If omitted, the benchmark typically uses standard PDF Type1 fonts like Times-Roman.
    script-executable TXT_FILE OUTPUT_FILE [TTF_FILE]
  8. Run HexaPDF benchmarks

    master

    To run a specific type of benchmark, execute the script.sh file located within that benchmark's directory. Each directory in the benchmark/ folder represents a different benchmark type and contains its own necessary files.

    Note: These scripts are designed for Linux environments. They may work on macOS or via WSL on Windows 10.

    optimization/script.sh
  9. Run the Line Wrapping Benchmark

    master

    The line wrapping benchmark evaluates performance for line wrapping and general layouting using text from Project Gutenberg. You can run individual benchmark scripts using the following command structure:

    script-executable TXT_FILE WIDTH OUTPUT_FILE [TTF_FILE]

    Arguments:

    • TXT_FILE: The source text file (e.g., Homer's Odyssey).
    • WIDTH: The page width in points (e.g., 400, 200, 100, or 50).
    • OUTPUT_FILE: The destination path for the generated PDF.
    • [TTF_FILE] (Optional): A path to a TrueType font file (e.g., DejaVu Sans).
    script-executable TXT_FILE WIDTH OUTPUT_FILE [TTF_FILE]
  10. Run the Table Benchmark script

    master

    The table benchmark tests the performance of various PDF table implementations (HexaPDF, Prawn, ReportLab, fpdf2) by laying out a table with three columns (text, image, text) and varying row counts, including automatic page breaking.

    To run a benchmark script, execute the script file directly with the following positional arguments:

    1. NR_ROWS: The number of rows to include in the table.
    2. IMAGE_FILE: The path to an image file to be used in the table.
    3. OUTPUT_FILE: The path where the resulting PDF should be saved.
    script-executable NR_ROWS IMAGE_FILE OUTPUT_FILE
  11. Install HexaPDF via RubyGems

    master

    HexaPDF is a pure Ruby library. To install it, ensure you have Ruby version 3.0 or higher installed, then use the following command:

    gem install hexapdf

    Requirements:

    • Ruby 3.0 or higher.
    • The library has one external dependency, geom2d (provided by the HexaPDF authors).
    • If using the hexapdf command-line application, cmdparse is also required.