Something went wrong. Try again.
Simple web tool for turning images into printable label PDFs (various sizes)
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109/** * Assembles a `LayoutPlan` into a print-ready PDF with `pdf-lib`. PDF points * are used in this module only, at the moment geometry is emitted. */import { PDFDocument, type PDFImage } from 'pdf-lib';import { mmToPt } from '$lib/core/units';import type { LabelPosition, LayoutPlan, SheetSpec } from '$lib/core/types';import { DEFAULT_DPI, rasterCacheKey, rasterizeForLabelCached } from './raster';
export interface BuildPdfOptions { dpi?: number;}
export interface PdfRect { x: number; y: number; width: number; height: number;}
/** * The label rect, expanded by bleed, converted from the domain's top-left * origin (y down) to PDF's bottom-left origin (y up). A label near the top of * the page must land at a HIGH y in the returned rect — get this flip wrong * and the whole sheet prints upside down. */export function labelRectToPdfRect( position: LabelPosition, bleedMm: number, pageHeightMm: number): PdfRect { const xMm = position.xMm - bleedMm; const topMm = position.yMm - bleedMm; const widthMm = position.widthMm + 2 * bleedMm; const heightMm = position.heightMm + 2 * bleedMm; const bottomMm = topMm + heightMm;
return { x: mmToPt(xMm), y: mmToPt(pageHeightMm - bottomMm), width: mmToPt(widthMm), height: mmToPt(heightMm) };}
export async function buildPdf(plan: LayoutPlan, opts: BuildPdfOptions = {}): Promise<Blob> { const dpi = opts.dpi ?? DEFAULT_DPI; const doc = await PDFDocument.create(); const embedded = new Map<string, PDFImage>();
for (const page of plan.pages) { const pdfPage = doc.addPage([mmToPt(plan.sheet.pageWidthMm), mmToPt(plan.sheet.pageHeightMm)]);
for (const placement of page.placements) { const targetWidthMm = placement.position.widthMm + 2 * plan.options.bleedMm; const targetHeightMm = placement.position.heightMm + 2 * plan.options.bleedMm;
const raster = await rasterizeForLabelCached( placement.image, targetWidthMm, targetHeightMm, dpi );
const key = rasterCacheKey( placement.image.id, placement.image.rotation, targetWidthMm, targetHeightMm, dpi );
let pdfImage = embedded.get(key); if (!pdfImage) { const bytes = new Uint8Array(await raster.blob.arrayBuffer()); pdfImage = raster.format === 'png' ? await doc.embedPng(bytes) : await doc.embedJpg(bytes); embedded.set(key, pdfImage); }
const rect = labelRectToPdfRect( placement.position, plan.options.bleedMm, plan.sheet.pageHeightMm ); pdfPage.drawImage(pdfImage, rect); } }
const bytes = await doc.save(); // pdf-lib types `save()` as Uint8Array<ArrayBufferLike>; Blob wants a concrete // ArrayBuffer-backed view, hence the copy. return new Blob([new Uint8Array(bytes)], { type: 'application/pdf' });}
function slugify(value: string): string { return value .toLowerCase() .trim() .replace(/[^a-z0-9]+/g, '-') .replace(/^-+|-+$/g, '');}
/** A sensible download filename, e.g. `avery-l7160-2-pages.pdf`. */export function downloadFilename(sheet: SheetSpec, pageCount: number): string { const slug = slugify(`${sheet.brand}-${sheet.code}`) || 'labels'; const pages = `${pageCount}-page${pageCount === 1 ? '' : 's'}`; return `${slug}-${pages}.pdf`;}