Media and drawing
Examplea whole program
state said = ""state file: bytes? = noneaction invoice let rows = [["Item", "Qty", "Price"], ["Brass gears", "3", "€36.00"], ["Punched cards", "100", "€4.50"]] let b = pdf.make([ pdf.heading("Invoice 42"), pdf.text(["Billed to ", {text: "Ada Lovelace", bold: true}, ", 12 Analytical Row, London."]), pdf.table(rows, {widths: [3, 1, 1], align: ["left", "right", "right"]}), pdf.text("Total: €40.50", {align: "right", bold: true, size: 13}), pdf.text("Thank you. Payment within 30 days.", {color: "#666666"}), ], {title: "Invoice 42", author: "Analytical Engines Ltd", size: "A4", footer: [pdf.text("Page {page} of {pages}", {align: "center", size: 9})]})? file = b let text = pdf.read(b)? said = text[0]column gap=8 padding=16 button "Make the invoice" press=invoice disabled=pending(invoice) if file link "Open it" href=file.url("application/pdf") target="_blank" text said white-space=pre-wrap text (failure(invoice) ?? "") color=dangerMental model#
Making a document#
The parts#
Text and fonts#
Examplea whole program
action ticket let body = font.open(net.bytes("/fonts/Inter.ttf")?)? let bold = font.open(net.bytes("/fonts/Inter-Bold.ttf")?)? let b = pdf.make([ pdf.heading("Zażółć gęślą jaźń"), pdf.text("Ελληνικά, кириллица, and kerning: AVATAR Wave To."), ], {font: "Inter", fonts: {Inter: body, "Inter Bold": bold}})? fs.save(b, "ticket.pdf")?button "Ticket" press=ticketTables#
Pictures#
Drawing#
Examplea whole program
action badge let b = pdf.make([ pdf.rect(40, 40, 200, 120, {radius: 12, gradient: {colors: ["#0a84ff", "#5e5ce6"], angle: 90}}), pdf.text("VISITOR", {at: [60, 70], size: 28, bold: true, color: "#ffffff"}), pdf.circle(200, 130, 16, {fill: "#ffffff", opacity: 0.6}), pdf.group([pdf.path("M 0 0 L 40 0 L 20 30 Z", {fill: "#ff9f0a"})], {translate: [60, 120], rotate: 15}), ], {size: [280, 200], margin: 10})? fs.save(b, "badge.pdf")?button "Badge" press=badgeHeaders, footers, links and the outline#
Reading a PDF#
Examplea whole program
state lines: string[] = []action open let f = fs.pick(".pdf")? if f == none return let info = pdf.info(f.data)? lines = [`${info.pages.length} pages, "${info.title}"`] let text = pdf.read(f.data, [0])? lines.push(text[0]) for w in pdf.words(f.data, 0)? if w.text == "Total" lines.push(`"Total" is at ${w.x}, ${w.y}`)button "Open a PDF" press=openfor l in lines text lForms#
Examplea whole program
state out: bytes? = noneaction sign(form: bytes) let fields = pdf.fields(form)? log(fields.map(f => `${f.name} (${f.kind})`).join(", ")) out = pdf.fill(form, {Name: "Ada Lovelace", Agree: true, Size: "M", Colour: "Blue"}, {flatten: true})?button "Fill" press={sign(bytes("…the form's bytes…"))}Changing a PDF#
Examplea whole program
action draft(b: bytes) let marked = pdf.stamp(b, [pdf.text("DRAFT", {at: [140, 420], size: 90, color: "#cc0000", opacity: 0.15, rotate: -35})])? let numbered = pdf.stamp(marked, [pdf.text("{page} / {pages}", {at: [500, 800], size: 9})])? let first = pdf.pages(numbered, [0])? fs.save(pdf.merge([first, numbered])?, "draft.pdf")?button "Draft" press={draft(bytes("…a PDF's bytes…"))}On the back end#
Examplea whole program
server fn invoice(n: number): bytes return pdf.make([pdf.heading(`Invoice ${n}`), pdf.text("Thank you.")], {title: `Invoice ${n}`})?server route GET "/invoice.pdf"(req) return {bytes: invoice(42)?, headers: {"content-type": "application/pdf"}}link "Invoice" href="/invoice.pdf"Showing a PDF#
Pitfalls#
Where a new call goes#
In the source#
Edit this page:
docs/pdf.md