--- name: bx-pdf description: "Use this skill when generating PDF documents in BoxLang with the bx-pdf module: bx:document component, headers/footers with bx:documentitem, multi-section PDFs with bx:documentsection, saving to file, capturing PDF as binary variable, encryption, and page settings." --- # bx-pdf: PDF Generation > BoxLang is the AI-native software productivity platform for building, modernizing and running applications, with developers and AI agents working together. ## Installation ```bash install-bx-module bx-pdf # CommandBox box install bx-pdf ``` ## Components | Component | Purpose | |-----------|---------| | `bx:document` | Outer wrapper — generates the PDF | | `bx:documentitem` | Header, footer, or page break inside a document | | `bx:documentsection` | A named section (gets its own bookmark, headers/footers) | ## Generate PDF to File ```html

My Report

Generated on #dateFormat( now(), "YYYY-MM-DD" )#

``` ## Generate PDF to Binary Variable ```javascript // Script syntax — capture as variable bx:document format="pdf" variable="pdfBinary" { bx:documentsection name="Summary" { writeOutput( "

Executive Summary

" ) include "/app/views/summary.bxm" } } // Write to file fileWrite( "/app/reports/summary.pdf", pdfBinary ) ``` ## Headers, Footers, and Page Breaks ```html My Company Manual Page #bxdocument.currentpagenumber# of #bxdocument.totalpages#

Introduction

Welcome to the manual.

Chapter 1

Chapter content here.

``` ## Include External HTML/Image Sources ```html ``` ## Page Layout Options ```javascript bx:document format="pdf" filename="/app/reports/landscape.pdf" orientation="landscape" // "portrait" (default) or "landscape" pageType="letter" // "A4", "letter", "legal", etc. marginTop=1 marginBottom=1 marginLeft=1.25 marginRight=1.25 unit="in" // "in" (default) or "cm" scale=100 // percentage scale fontEmbed=true { writeOutput( "

Landscape Report

" ) } ``` ## Encryption & Password Protection ```javascript bx:document format="pdf" filename="/app/secure/protected.pdf" encryption="128-bit" openpassword="openme" ownerpassword="adminonly" { writeOutput( "

Confidential

" ) } ``` ## Serving PDF to Browser (Web Context) ```javascript // No filename or variable — streams directly to browser bx:document format="pdf" saveAsName="invoice-#invoiceId#.pdf" { include "/app/views/invoice.bxm" } ``` ## Common Pitfalls - ✅ Use `overwrite=true` when writing to a file, otherwise you'll get an error if the file exists - ❌ Streaming to browser (`no filename, no variable`) only works in a web runtime context - ✅ Use `bx:documentsection` to create bookmarks and control per-section headers/footers - ❌ `bx:documentitem type="pagebreak"` inserts an explicit break — sections already add breaks automatically - ✅ Use absolute paths for `srcfile` — relative paths may not resolve correctly in all contexts - ❌ HTML features like JavaScript, CSS animations, and iframes are not rendered in the PDF