Phase 1 - Refactoring: - Modular architecture: fonts/, graphics/, objects/, output/ - Fixed Zig 0.15 API changes (ArrayListUnmanaged) - Fixed memory issues in render() Phase 2 - Text System: - cell() with borders, fill, alignment - cellAdvanced() with position control - multiCell() with automatic word wrap - ln() for line breaks - getStringWidth() for text width calculation - Page margins (setMargins, setCellMargin) - Align enum (left, center, right) - Border packed struct New features: - New Pdf API (cleaner than legacy Document) - Document metadata (setTitle, setAuthor, setSubject) - Color: RGB, CMYK, Grayscale support - 52 unit tests passing - New example: text_demo.zig 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
74 lines
2.3 KiB
Zig
74 lines
2.3 KiB
Zig
//! Minimal PDF example - Hello World
|
|
|
|
const std = @import("std");
|
|
const pdf = @import("zpdf");
|
|
|
|
pub fn main() !void {
|
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
|
defer _ = gpa.deinit();
|
|
const allocator = gpa.allocator();
|
|
|
|
std.debug.print("zpdf - Hello World example\n", .{});
|
|
|
|
// Create document
|
|
var doc = pdf.Document.init(allocator);
|
|
defer doc.deinit();
|
|
|
|
// Add a page
|
|
var page = try doc.addPage(.a4);
|
|
|
|
// Title
|
|
try page.setFont(.helvetica_bold, 36);
|
|
page.setFillColor(pdf.Color.rgb(0, 100, 200));
|
|
try page.drawText(50, 750, "Hello, PDF!");
|
|
|
|
// Subtitle
|
|
try page.setFont(.helvetica, 14);
|
|
page.setFillColor(pdf.Color.medium_gray);
|
|
try page.drawText(50, 710, "Generated with zpdf - Pure Zig PDF library");
|
|
|
|
// Draw a line
|
|
try page.setLineWidth(1);
|
|
page.setStrokeColor(pdf.Color.light_gray);
|
|
try page.drawLine(50, 700, 545, 700);
|
|
|
|
// Body text
|
|
try page.setFont(.times_roman, 12);
|
|
page.setFillColor(pdf.Color.black);
|
|
try page.drawText(50, 670, "This PDF was generated entirely in Zig with zero external dependencies.");
|
|
try page.drawText(50, 655, "The zpdf library supports:");
|
|
try page.drawText(70, 635, "- Multiple fonts (Helvetica, Times, Courier)");
|
|
try page.drawText(70, 620, "- Colors (RGB)");
|
|
try page.drawText(70, 605, "- Lines and rectangles");
|
|
try page.drawText(70, 590, "- Multiple pages");
|
|
|
|
// Draw some shapes
|
|
try page.setLineWidth(2);
|
|
page.setStrokeColor(pdf.Color.blue);
|
|
page.setFillColor(pdf.Color.rgb(230, 240, 255));
|
|
try page.drawFilledRect(50, 500, 200, 60);
|
|
|
|
try page.setFont(.helvetica_bold, 14);
|
|
page.setFillColor(pdf.Color.blue);
|
|
try page.drawText(60, 540, "Shapes work too!");
|
|
|
|
// Red rectangle
|
|
page.setStrokeColor(pdf.Color.red);
|
|
page.setFillColor(pdf.Color.rgb(255, 230, 230));
|
|
try page.drawFilledRect(280, 500, 200, 60);
|
|
|
|
page.setFillColor(pdf.Color.red);
|
|
try page.drawText(290, 540, "Multiple colors!");
|
|
|
|
// Footer
|
|
try page.setFont(.courier, 10);
|
|
page.setFillColor(pdf.Color.medium_gray);
|
|
try page.drawText(50, 50, "zpdf v0.1.0 - https://git.reugenio.com/reugenio/zpdf");
|
|
|
|
// Save
|
|
const filename = "hello.pdf";
|
|
try doc.saveToFile(filename);
|
|
|
|
std.debug.print("Created: {s}\n", .{filename});
|
|
std.debug.print("Done!\n", .{});
|
|
}
|