| Kind | ffi-c |
|---|---|
| Capabilities | ffi |
| Categories | document-processing ffi |
| Keywords | pdf document pdfio report |
PDF reading and writing library for Kit using pdfio
Files
| File | Description |
|---|---|
.editorconfig | Editor formatting configuration |
.gitignore | Git ignore rules for build artifacts and dependencies |
.tool-versions | asdf tool versions (Zig, Kit) |
LICENSE | MIT license file |
README.md | This file |
c/kit_pdf.c | C FFI wrapper around PDFio |
c/kit_pdf.h | C header for the FFI wrapper |
examples/basic.kit | Basic PDF creation and reading example |
kit.toml | Package manifest with metadata, capabilities, and native library settings |
src/pdf.kit | Kit PDF API |
tests/pdf.test.kit | Tests for PDF helpers |
Dependencies
No Kit package dependencies.
This package requires the native pdfio library at build and runtime. The Kit wrapper library is built as libkit_pdf.dylib on macOS or libkit_pdf.so on Linux.
Installation
kit add gitlab.com/kit-lang/packages/kit-pdf.gitSystem Requirements
| Platform | Command |
|---|---|
| macOS | Build PDFio from source into the Homebrew prefix; Homebrew does not provide a pdfio formula. |
| Ubuntu | sudo apt install libpdfio-dev, or build PDFio from source if your Ubuntu release does not package it. |
| Fedora | sudo dnf install pdfio-devel |
On macOS:
brew install pkg-config libpng
tmpdir=$(mktemp -d)
git clone --recursive https://github.com/michaelrsweet/pdfio.git "$tmpdir/pdfio"
cd "$tmpdir/pdfio"
./configure --prefix=$(brew --prefix) --enable-shared CFLAGS="-arch $(uname -m)"
make
make installIf you already cloned PDFio without submodules, initialize them before rebuilding:
git -C pdfio submodule update --init --recursive
cd pdfio
./configure --prefix=$(brew --prefix) --enable-shared CFLAGS="-arch $(uname -m)"
make
make installUsage
import Kit.PDF as Pdf
main = fn =>
# Create a PDF
match Pdf.create "/tmp/report.pdf" "Monthly Report" "Kit User"
| Ok pdf ->
match Pdf.add-page pdf Pdf.letter-width Pdf.letter-height
| Ok page ->
Pdf.add-text pdf page "Monthly Report" 72 720 24
Pdf.add-text pdf page "Generated with kit-pdf" 72 690 12
| Err e -> println ("Page error: " ++ e)
Pdf.finalize pdf
println "PDF created"
| Err e -> println ("Error: " ++ e)
# Read a PDF
match Pdf.open "/tmp/report.pdf"
| Ok pdf ->
println ("Pages: " ++ (show (Pdf.page-count pdf)))
match Pdf.get-title pdf
| Some title -> println ("Title: " ++ title)
| None -> println "No title"
match Pdf.get-author pdf
| Some author -> println ("Author: " ++ author)
| None -> println "No author"
Pdf.close pdf
| Err e -> println ("Error: " ++ e)
mainDevelopment
Local Native Build
Build and install the package wrapper locally:
kit installThis installs the package to ~/.kit/packages/@kit/pdf/ and builds the native wrapper library there.
The package manifest links both pdfio and kit_pdf. The kit install native build skips self-linking kit_pdf, while downstream Kit builds use it to resolve the wrapper symbols from libkit_pdf.
Running Examples
Run examples with the interpreter:
kit run examples/basic.kitCompile examples to a native binary:
kit build examples/basic.kit && ./basicRunning Tests
Run the test suite:
kit testRun the test suite with coverage:
kit test --coverageRunning Parity
Run interpreter/compiler parity checks for the examples:
kit parity --no-spinner --failures-onlyRunning kit dev
Run the standard development workflow (format, check, test):
kit devThis will:
- Format and check source files in
src/ - Run tests in
tests/with coverage
Generating Documentation
Generate API documentation from doc comments:
kit docNote: Kit sources with doc comments (##) will generate HTML documents in docs/*.html.
Cleaning Build Artifacts
Remove generated files, caches, and build artifacts:
kit task cleanNote: Defined in kit.toml.
Local Installation
To install this package locally for development:
kit installThis installs the package to ~/.kit/packages/@kit/pdf/, making it available for import as Kit.PDF in other projects.
Troubleshooting
If kit install reports that pdfio is missing, verify that pkg-config can find it:
pkg-config --exists pdfio && pkg-config --libs pdfioOn macOS, PDFio must be installed into the same prefix used by the compiler and pkg-config, usually $(brew --prefix).
License
This package is released under the MIT License - see LICENSE for details.
PDFio is released under the Apache License 2.0.
Exported Functions & Types
open
Open a PDF file for reading.
Parameters:
Returns:
NonEmptyString -> Result Ptr String
match open "report.pdf"
| Ok pdf ->
println ("Pages: " ++ (show (page-count pdf)))
close pdf
| Err e -> println "Error: ${e}"close
Close a PDF file and release resources.
Parameters:
Ptr -> Unit
close pdfpage-count
Get the number of pages in a PDF.
Parameters:
Returns:
Ptr -> Int
pages = page-count pdf
println ("Document has " ++ (show pages) ++ " pages")get-title
Get the title from PDF metadata.
Parameters:
Returns:
Ptr -> Option String
match get-title pdf
| Some t -> println ("Title: " ++ t)
| None -> println "No title set"get-author
Get the author from PDF metadata.
Parameters:
Returns:
Ptr -> Option String
match get-author pdf
| Some a -> println ("Author: " ++ a)
| None -> println "No author set"get-version
Get the PDF version string.
Parameters:
Returns:
Ptr -> String
create
Create a new PDF file for writing.
Parameters:
Returns:
NonEmptyString -> String -> String -> Result Ptr String
match create "output.pdf" "My Report" "Kit User"
| Ok pdf ->
# ... add pages and content ...
finalize pdf
| Err e -> println "Error: ${e}"add-page
Add a new page to a PDF being written.
Use letter-width/letter-height or a4-width/a4-height for standard page sizes. Dimensions are in points (72 points = 1 inch).
Parameters:
Returns:
Ptr -> PositiveInt -> PositiveInt -> Result Ptr String
match add-page pdf letter-width letter-height
| Ok page -> add-text pdf page "Hello!" 72 720 12
| Err e -> println "Error: ${e}"add-text
Add text to a page at the given position.
Coordinates are measured from the bottom-left corner of the page in points (72 points = 1 inch).
Parameters:
Returns:
Ptr -> Ptr -> String -> Int -> Int -> PositiveInt -> Result Unit String
add-text pdf page "Hello, PDF!" 72 720 14finalize
Finalize and close a PDF being written.
Must be called after all pages and content have been added.
Parameters:
Ptr -> Unit
finalize pdfletter-width
US Letter page width in points (8.5 inches).
Int
letter-height
US Letter page height in points (11 inches).
Int
a4-width
A4 page width in points (210mm).
Int
a4-height
A4 page height in points (297mm).
Int