Skip to Content

pdf

Makes Vietnamese PDF files from server code with manh/pdf, for repair slips, quotes, delivery notes and warranty slips. The built-in font (Be Vietnam Pro) prints every Vietnamese letter and the ₫ sign.

Usage

app/tickets/[id:int]/pdf/get.ts
import { newPdf, text, fields, table, rule, pdfBytes } from "manh/pdf"; import { vnd, vnDate } from "manh/vn"; import { getTicket } from "../../repository"; export default async (c, { params }) => { const t = await getTicket(params.id); const p = await newPdf(`Phiếu sửa chữa ${t.code}`); text(p, "PHIẾU SỬA CHỮA", { size: 16, bold: true }); fields(p, [["Mã phiếu", t.code], ["Khách hàng", t.customerName], ["Ngày nhận", vnDate(t.createdAt)]]); rule(p); table(p, ["Linh kiện / công", "SL", "Thành tiền"], t.lines.map((l) => [l.name, String(l.qty), vnd(l.total)]), [0.62, 0.12, 0.26], [false, true, true]); text(p, `Tổng cộng: ${vnd(t.total)}`, { bold: true, x: 380 }); return new Response(await pdfBytes(p), { headers: { "content-type": "application/pdf", "content-disposition": `inline; filename="${t.code}.pdf"` }, }); };

Server code (.ts) only.

Signature

manh/pdf
type Pdf = { doc: any; page: any; regular: any; bold: any; y: number }; function newPdf(title: string): Promise<Pdf>; function text(p: Pdf, s: string, opts?: { x?: number; size?: number; bold?: boolean; color?: Color; width?: number; gap?: number }): void; function fields(p: Pdf, rows: Array<[string, string]>, opts?: { labelWidth?: number; size?: number }): void; function rule(p: Pdf, gap?: number): void; function table(p: Pdf, head: string[], rows: string[][], widths: number[], alignRight?: boolean[]): void; function embedImage(p: Pdf, bytes: Uint8Array, contentType?: string): Promise<any>; function imageRow(p: Pdf, images: any[], height?: number): void; function ensure(p: Pdf, need: number): void; function pdfBytes(p: Pdf): Promise<Uint8Array>; // also: rgb, wrap, A4, MARGIN, GREY, DARK, LINE, BRAND

Parameters

  • newPdf(title): starts an A4 document whose document title is title.
  • text(p, s, opts): a paragraph that wraps by itself. size font size, bold, x left edge, width, color from rgb(r, g, b) (0–1), gap space after it.
  • fields(p, rows): label and value rows in two columns.
  • rule(p, gap?): a horizontal line.
  • table(p, head, rows, widths, alignRight?): a table with a header row. widths are column fractions that add up to 1. alignRight right-aligns number columns.
  • embedImage(p, bytes): embeds a JPEG or PNG (8-bit, no transparency); other formats return null. imageRow(p, images, height?) draws images in one row, 110 points high by default.
  • ensure(p, need): starts a new page when fewer than need points are left.

Returns

The drawing functions write into p and start a new page when space runs out. pdfBytes(p) returns the PDF’s Uint8Array: return it in a Response with content-type: application/pdf, or store it with blob.

Examples

The device photo taken at intake, from file storage:

app/tickets/[id:int]/pdf/get.ts
const photo = await (await blob.get(t.photoKey)).arrayBuffer(); const img = await embedImage(p, new Uint8Array(photo)); if (img) imageRow(p, [img]);

Keep a copy to send again later:

app/quotes/repository.ts
await blob.set(`quotes/${q.code}.pdf`, await pdfBytes(p), { contentType: "application/pdf" });
  • Don’t use pdf-lib: it keeps the whole app from starting and its built-in fonts can’t print “ư”, “ạ” or ”₫”. check rejects that import.
  • Format money and dates with vnd and vnDate from manh/vn so the PDF matches the screens.

Next steps

  • blob: store the PDF and share an expiring download link.
  • email: send the customer a link to the slip.
Last updated