()
| 11 | import { formatBytes, saveOutput } from "../utils"; |
| 12 | |
| 13 | async function main() { |
| 14 | console.log("Creating a PDF with form fields...\n"); |
| 15 | |
| 16 | // Create a new PDF document |
| 17 | const pdf = PDF.create(); |
| 18 | |
| 19 | // Add a page |
| 20 | pdf.addPage({ size: "letter" }); |
| 21 | |
| 22 | // Get the page object for drawing |
| 23 | const page = pdf.getPage(0); |
| 24 | if (!page) { |
| 25 | throw new Error("Failed to get page"); |
| 26 | } |
| 27 | |
| 28 | // Get or create the form |
| 29 | const form = pdf.getOrCreateForm(); |
| 30 | |
| 31 | // Draw a title |
| 32 | page.drawText("Registration Form", { |
| 33 | x: 200, |
| 34 | y: 750, |
| 35 | size: 24, |
| 36 | color: black, |
| 37 | }); |
| 38 | |
| 39 | // === Text Fields === |
| 40 | console.log("Creating text fields..."); |
| 41 | |
| 42 | // Full name field |
| 43 | const nameLabel = "Full Name:"; |
| 44 | page.drawText(nameLabel, { x: 50, y: 700, size: 12, color: black }); |
| 45 | |
| 46 | const nameField = form.createTextField("fullName", { |
| 47 | fontSize: 12, |
| 48 | maxLength: 50, |
| 49 | }); |
| 50 | page.drawField(nameField, { x: 150, y: 695, width: 300, height: 20 }); |
| 51 | |
| 52 | // Email field |
| 53 | page.drawText("Email:", { x: 50, y: 660, size: 12, color: black }); |
| 54 | |
| 55 | const emailField = form.createTextField("email", { |
| 56 | fontSize: 12, |
| 57 | maxLength: 100, |
| 58 | }); |
| 59 | page.drawField(emailField, { x: 150, y: 655, width: 300, height: 20 }); |
| 60 | |
| 61 | // Phone field |
| 62 | page.drawText("Phone:", { x: 50, y: 620, size: 12, color: black }); |
| 63 | |
| 64 | const phoneField = form.createTextField("phone", { |
| 65 | fontSize: 12, |
| 66 | maxLength: 20, |
| 67 | }); |
| 68 | page.drawField(phoneField, { x: 150, y: 615, width: 200, height: 20 }); |
| 69 | |
| 70 | // Comments (multiline) |
no test coverage detected