Tạo file PDF tiếng Việt từ code server bằng manh/pdf, cho phiếu sửa chữa, báo giá, phiếu xuất kho, phiếu bảo hành. Font có sẵn (Be Vietnam Pro) in đủ dấu tiếng Việt và ký hiệu ₫.
Cách dùng
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"` },
});
};Chỉ dùng trong file .ts.
Chữ ký
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>;
// cùng có: rgb, wrap, A4, MARGIN, GREY, DARK, LINE, BRANDTham số
newPdf(title): tạo tài liệu khổ A4 với tiêu đề tài liệutitle.text(p, s, opts): một đoạn chữ, tự xuống dòng.sizecỡ chữ,boldchữ đậm,xlề trái,widthbề rộng,colormàu từrgb(r, g, b)(0–1),gapkhoảng cách sau đoạn.fields(p, rows): các dòng nhãn và giá trị, hai cột.rule(p, gap?): một đường kẻ ngang.table(p, head, rows, widths, alignRight?): bảng có hàng tiêu đề.widthslà tỉ lệ bề rộng từng cột, cộng lại bằng 1.alignRightcăn phải cột số.embedImage(p, bytes): nhúng ảnh JPEG hoặc PNG (8 bit, không trong suốt); định dạng khác trảnull.imageRow(p, images, height?)vẽ các ảnh thành một hàng, cao 110 điểm mặc định.ensure(p, need): sang trang mới khi chỗ còn lại ít hơnneedđiểm.
Trả về
Các hàm vẽ ghi thẳng vào p và tự sang trang khi hết chỗ. pdfBytes(p) trả Uint8Array của file PDF: trả về trong Response với content-type: application/pdf, hoặc lưu bằng blob.
Ví dụ
Ảnh máy lúc nhận và chữ ký khách, lấy từ kho file:
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]);Lưu một bản để gửi lại sau:
app/quotes/repository.ts
await blob.set(`quotes/${q.code}.pdf`, await pdfBytes(p), { contentType: "application/pdf" });- Không dùng
pdf-lib: thư viện này làm cả app không khởi động được và font có sẵn của nó không in được “ư”, “ạ”, ”₫”.checktừ chối import này. - Định dạng tiền và ngày bằng
vnd,vnDatecủamanh/vnđể PDF khớp với màn hình.
Bước tiếp theo
Cập nhật