### Create a Simple PDF File with HexaPDF Source: https://github.com/gettalong/hexapdf/blob/master/README.md This example demonstrates how to create a basic PDF file with text using the HexaPDF library. Ensure the 'hexapdf' gem is installed. ```ruby require 'hexapdf' doc = HexaPDF::Document.new canvas = doc.pages.add.canvas canvas.font('Helvetica', size: 100) canvas.text("Hello World!", at: [20, 400]) doc.write("hello-world.pdf") ``` -------------------------------- ### Template File Format Example Source: https://github.com/gettalong/hexapdf/blob/master/man/man1/hexapdf.1.md An example illustrating the format of a template file used for filling PDF form fields. Field names are listed at the beginning of lines, followed by their values. Values can span multiple lines. ```text page1.field1: A simple value page1.field3: Another value spanning more than on line. Another form field: Value for this form field. ``` -------------------------------- ### Recursive Object Inspection Example Source: https://github.com/gettalong/hexapdf/blob/master/man/man1/hexapdf.1.md Demonstrates the output format for recursively inspecting an indirect object. It shows how references are replaced with custom indices and how page objects are identified. ```text << /Info {obj 1} << /Producer (HexaPDF version 0.9.3) >> /Root {obj 2} << /Pages {obj 3} << /Count 1 /Kids [{obj page 1} << /MediaBox [0 0 595 842 ] /Parent {ref 3} /Type /Page >> ] /Type /Pages >> /Type /Catalog >> /Size 4 >> ``` -------------------------------- ### Inspect a PDF file Source: https://github.com/gettalong/hexapdf/blob/master/man/man1/hexapdf.1.md Shows the page object for the first page as well as the decoded content stream of the first page. Starts the interactive inspection mode. ```bash hexapdf inspect input.pdf ``` -------------------------------- ### CombinePDF Invocation Source: https://github.com/gettalong/hexapdf/blob/master/benchmark/optimization/README.md Example of how to invoke the CombinePDF script for merging PDF files. This script is written in Ruby. ```bash ruby combine_pdf.rb INPUT OUTPUT ``` -------------------------------- ### Convert Images to PDF with Page Size and Margins Source: https://github.com/gettalong/hexapdf/blob/master/man/man1/hexapdf.1.md Creates a PDF with specified page size (A4) and margins (36pt) around each image. Images are fitted to the pages. ```bash hexapdf image2pdf -p A4 -m 36 image1.jpg image2.pdf image3.png output.pdf ``` -------------------------------- ### Get General Information about a PDF File Source: https://github.com/gettalong/hexapdf/blob/master/man/man1/hexapdf.1.md Reads a PDF file and displays general information such as author details, PDF version, and encryption status. ```bash hexapdf info input.pdf ``` -------------------------------- ### QPDF: Object Streams (CS) Source: https://github.com/gettalong/hexapdf/blob/master/benchmark/optimization/README.md Invoke qpdf with the --object-streams=generate option to enable object streams (S) in addition to compacting (C). ```bash qpdf --object-streams=generate INPUT OUTPUT ``` -------------------------------- ### Executing a Single Command on Multiple Files Source: https://github.com/gettalong/hexapdf/blob/master/man/man1/hexapdf.1.md Applies the 'info' command to multiple PDF files, outputting information for each. ```bash hexapdf batch 'info {}' input1.pdf input2.pdf input3.pdf ``` -------------------------------- ### Check PDF for Errors and Get Information Source: https://github.com/gettalong/hexapdf/blob/master/man/man1/hexapdf.1.md Checks a PDF file for parse and validation errors, prints them, and then displays general information. HexaPDF can still handle the file even if errors are found. ```bash hexapdf info --check input.pdf ``` -------------------------------- ### Apply watermark from a PDF Source: https://github.com/gettalong/hexapdf/blob/master/man/man1/hexapdf.1.md Uses the first page of watermark.pdf as a background for all pages of input.pdf and saves the result to output.pdf. ```bash hexapdf watermark -w watermark.pdf input.pdf output.pdf ``` -------------------------------- ### QPDF: Standard Mode (C) Source: https://github.com/gettalong/hexapdf/blob/master/benchmark/optimization/README.md Invoke qpdf for standard PDF transformation, which includes the 'C' operation (Compacting). ```bash qpdf INPUT OUTPUT ``` -------------------------------- ### Merge PDFs Starting with an Empty File Source: https://github.com/gettalong/hexapdf/blob/master/man/man1/hexapdf.1.md Merges pages from three input files into a new, empty PDF. The output file will only contain the merged pages without any meta data from the input files. ```bash hexapdf merge -e input1.pdf input2.pdf input3.pdf output.pdf ``` -------------------------------- ### Attach Files with Descriptions to PDF Source: https://github.com/gettalong/hexapdf/blob/master/man/man1/hexapdf.1.md Attaches multiple files to a PDF and saves the result to a new output file. Allows specifying descriptions for attached files. ```bash hexapdf files -a invoice.xml -d "Invoice data" -a custom.xml input.pdf output.pdf ``` -------------------------------- ### Apply watermark as stamp with specific pages Source: https://github.com/gettalong/hexapdf/blob/master/man/man1/hexapdf.1.md Applies the first page of watermark.pdf as a stamp to the first page of input.pdf, and the second page of watermark.pdf to all other pages of input.pdf. ```bash hexapdf watermark -w watermark.pdf -i 1,2 -t stamp input.pdf output.pdf ``` -------------------------------- ### HexaPDF: Full Optimization (CSP) Source: https://github.com/gettalong/hexapdf/blob/master/benchmark/optimization/README.md Invoke hexapdf with all optimization flags enabled: Compacting (C), Object Streams (S), and Page Recompression (P). ```bash hexapdf optimize INPUT --compress-pages OUTPUT ```