Skip to content

armgit / pdfcn

Public Imported from GitHub
0

Beautiful pdf components, built on Takumi and Forme. 100% Free, Zero config, one command setup.

1.6 KB
Raw Blame
import type { RenderOptions } from "takumi-pdf";

import { pointToCssPixel } from "@/registry/bases/takumi/lib/pdf-primitives";

const DEFAULT_MARGIN = 40;

const BLOCK_NAMES = new Set([
  "event-agenda",
  "event-ticket",
  "invoice-classic",
  "invoice-consultant",
  "invoice-corporate",
  "invoice-creative",
  "invoice-minimal",
  "invoice-modern",
  "lesson-plan",
  "medical-intake-form",
  "meeting-minutes",
  "packing-slip",
  "press-release",
  "report-financial",
  "report-marketing",
  "report-operations",
  "report-security",
  "shipping-label",
  "work-order",
]);

const COMPONENT_MARGINS: Record<string, number> = {
  divider: 30,
  heading: 30,
  link: 30,
  "page-break": 30,
  "page-footer": 30,
  "page-header": 30,
  section: 30,
  stack: 30,
  text: 30,
};

const COMPONENT_SIZES: Record<
  string,
  Extract<RenderOptions, { viewport?: never }>["size"]
> = {
  badge: { height: pointToCssPixel(200), width: pointToCssPixel(595) },
  "event-ticket": {
    height: pointToCssPixel(252),
    width: pointToCssPixel(504),
  },
  "page-footer": {
    height: pointToCssPixel(300),
    width: pointToCssPixel(595),
  },
  "page-header": {
    height: pointToCssPixel(240),
    width: pointToCssPixel(595),
  },
  "shipping-label": {
    height: pointToCssPixel(432),
    width: pointToCssPixel(288),
  },
};

export const getTakumiPreviewOptions = (name: string): RenderOptions => {
  const margin = BLOCK_NAMES.has(name)
    ? 0
    : pointToCssPixel(COMPONENT_MARGINS[name] ?? DEFAULT_MARGIN);

  return {
    margin,
    size: COMPONENT_SIZES[name] ?? "a4",
  };
};