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.

14.4 KB
Raw Blame
{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "forme/event-ticket",
  "title": "Event Ticket",
  "description": "Event Ticket PDF block (forme)",
  "dependencies": [
    "@formepdf/react",
    "@formepdf/core",
    "qrcode"
  ],
  "registryDependencies": [
    "@pdfcn/forme/pdf-image",
    "@pdfcn/forme/qrcode",
    "@pdfcn/forme/text",
    "@pdfcn/forme/utils"
  ],
  "files": [
    {
      "path": "registry/bases/forme/blocks/event-ticket/event-ticket.tsx",
      "content": "import { Document, Page, StyleSheet, View } from \"@formepdf/react\";\n\nimport { PdfImage } from \"@/components/pdf/pdf-image/pdf-image\";\nimport { PdfQRCode } from \"@/components/pdf/qrcode/qrcode\";\nimport { Text } from \"@/components/pdf/text/text\";\nimport {\n  PdfcnThemeProvider,\n  usePdfcnTheme,\n} from \"@/components/pdf/theme-provider\";\nimport { resolveColor } from \"@/lib/resolve-color\";\nimport type { PdfcnTheme } from \"@/types/pdf-themes\";\n\nimport type { EventTicketData } from \"./event-ticket.types\";\n\n// Sample data — replace with your own props or data source\nconst sampleData: EventTicketData = {\n  accentColor: \"#52525b\",\n  address: \"123 Main St, Las Vegas, NV\",\n  doorsOpen: \"8:00 AM\",\n  eventDate: \"May 15, 2026\",\n  eventName: \"ShadCN Labs Conf\",\n  eventTime: \"9:00 AM\",\n  logoUrl: \"/favicon.png\",\n  organizer: \"ShadCN Labs\",\n  seat: { number: \"12\", row: \"3\", section: \"A\" },\n  socialLinks: [\n    { platform: \"X\", url: \"x.com/shadcn\" },\n    { platform: \"Instagram\", url: \"instagram.com/shadcn\" },\n  ],\n  terms: \"Ticket is non-transferable. All sales final. No re-entry.\",\n  ticketNumber: \"TKT-00142\",\n  ticketType: \"VIP\",\n  venue: \"Convention Center\",\n};\n\n// Standard event ticket, 7\" x 3.5\", expressed in PDF points (72 dpi)\nconst TICKET_SIZE = { height: 252, width: 504 };\n\nconst STUB_WIDTH = 120;\n\nconst STRIPE_WIDTH = 8;\n\nconst NOTCH_RADIUS = 8;\n\n// Forme renders neither dashed borders nor dashed SVG strokes, so the\n// perforation is painted as a stack of short dashes along the stub seam.\nconst PERFORATION_WIDTH = 1.5;\n\nconst PERFORATION_DASH = 4;\n\nconst PERFORATION_GAP = 3;\n\nconst perforationOffsets: number[] = [];\nfor (\n  let offset = 0;\n  offset < TICKET_SIZE.height;\n  offset += PERFORATION_DASH + PERFORATION_GAP\n) {\n  perforationOffsets.push(offset);\n}\n\nconst hexRgb = (color: string): [number, number, number] | null => {\n  const hex = color.trim().replace(/^#/, \"\");\n\n  if (/^[0-9a-f]{3}$/i.test(hex)) {\n    return [\n      Number.parseInt(hex[0] + hex[0], 16),\n      Number.parseInt(hex[1] + hex[1], 16),\n      Number.parseInt(hex[2] + hex[2], 16),\n    ];\n  }\n\n  if (/^[0-9a-f]{6}([0-9a-f]{2})?$/i.test(hex)) {\n    return [\n      Number.parseInt(hex.slice(0, 2), 16),\n      Number.parseInt(hex.slice(2, 4), 16),\n      Number.parseInt(hex.slice(4, 6), 16),\n    ];\n  }\n\n  return null;\n};\n\nconst inkOnAccent = (accent: string, light: string, dark: string) => {\n  const rgb = hexRgb(accent);\n\n  if (!rgb) {\n    return dark;\n  }\n\n  const [r, g, b] = rgb;\n  const luminance = (0.2126 * r + 0.7152 * g + 0.0722 * b) / 255;\n\n  return luminance > 0.55 ? dark : light;\n};\n\nconst socialHandle = (url: string) => {\n  const clean = url\n    .replace(/^https?:\\/\\//i, \"\")\n    .replace(/^www\\./i, \"\")\n    .replace(/\\/$/, \"\");\n  const parts = clean.split(\"/\").filter(Boolean);\n  const handle = parts.at(-1);\n\n  if (parts.length > 1 && handle) {\n    return handle.startsWith(\"@\") ? handle : `@${handle}`;\n  }\n\n  return clean;\n};\n\nconst EventTicketContent = ({ data }: { data: EventTicketData }) => {\n  const theme = usePdfcnTheme();\n  const paper = theme.colors.background;\n  const ink = theme.colors.foreground;\n  const quiet = theme.colors.mutedForeground;\n\n  const accent = resolveColor(\n    data.accentColor ?? theme.colors.primary,\n    theme.colors\n  );\n  const onAccent = inkOnAccent(accent, paper, ink);\n\n  const styles = StyleSheet.create({\n    admit: {\n      color: onAccent,\n      fontSize: 7,\n      fontWeight: \"bold\",\n      letterSpacing: 2,\n      textTransform: \"uppercase\",\n    },\n    body: {\n      flex: 1,\n      height: TICKET_SIZE.height,\n      justifyContent: \"space-between\",\n      paddingBottom: 16,\n      paddingLeft: 20,\n      paddingRight: 18,\n      paddingTop: 18,\n    },\n    brand: {\n      alignItems: \"center\",\n      flexDirection: \"row\",\n      gap: 8,\n      marginBottom: 14,\n    },\n    fact: {\n      flexShrink: 1,\n      minWidth: 0,\n      paddingRight: 12,\n    },\n    facts: {\n      flexDirection: \"row\",\n    },\n    footer: {\n      alignItems: \"center\",\n      flexDirection: \"row\",\n      gap: 12,\n      marginTop: 10,\n    },\n    label: {\n      color: quiet,\n      fontSize: 7,\n      fontWeight: \"bold\",\n      letterSpacing: 1.2,\n      marginBottom: 3,\n      textTransform: \"uppercase\",\n    },\n    logo: { borderRadius: 3, height: 14, width: 14 },\n    notchBottom: {\n      backgroundColor: paper,\n      borderRadius: NOTCH_RADIUS,\n      bottom: -NOTCH_RADIUS,\n      height: NOTCH_RADIUS * 2,\n      left: TICKET_SIZE.width - STUB_WIDTH - NOTCH_RADIUS,\n      position: \"absolute\",\n      width: NOTCH_RADIUS * 2,\n    },\n    notchTop: {\n      backgroundColor: paper,\n      borderRadius: NOTCH_RADIUS,\n      height: NOTCH_RADIUS * 2,\n      left: TICKET_SIZE.width - STUB_WIDTH - NOTCH_RADIUS,\n      position: \"absolute\",\n      top: -NOTCH_RADIUS,\n      width: NOTCH_RADIUS * 2,\n    },\n    organizer: {\n      color: quiet,\n      // Forme ignores `marginLeft: \"auto\"`, so the organizer line absorbs the\n      // free space and pushes the ticket-type pill to the right edge.\n      flexGrow: 1,\n      fontSize: 8,\n      fontWeight: \"bold\",\n      letterSpacing: 1.1,\n      textTransform: \"uppercase\",\n    },\n    page: {\n      alignItems: \"stretch\",\n      backgroundColor: paper,\n      flexDirection: \"row\",\n      height: TICKET_SIZE.height,\n      overflow: \"hidden\",\n      position: \"relative\",\n      width: TICKET_SIZE.width,\n    },\n    perforation: {\n      height: TICKET_SIZE.height,\n      left: TICKET_SIZE.width - STUB_WIDTH - PERFORATION_WIDTH / 2,\n      position: \"absolute\",\n      top: 0,\n      width: PERFORATION_WIDTH,\n    },\n    perforationDash: {\n      backgroundColor: paper,\n      height: PERFORATION_DASH,\n      marginBottom: PERFORATION_GAP,\n      width: PERFORATION_WIDTH,\n    },\n    pill: {\n      backgroundColor: accent,\n      borderRadius: 9,\n      paddingHorizontal: 8,\n      paddingVertical: 3,\n    },\n    pillText: {\n      color: onAccent,\n      fontSize: 7,\n      fontWeight: \"bold\",\n      letterSpacing: 1.2,\n      textTransform: \"uppercase\",\n    },\n    qr: {\n      backgroundColor: \"#ffffff\",\n      borderRadius: 6,\n      padding: 5,\n    },\n    serial: {\n      color: onAccent,\n      fontSize: 7,\n      fontWeight: \"bold\",\n      letterSpacing: 1.1,\n      textTransform: \"uppercase\",\n    },\n    socialItem: {\n      alignItems: \"center\",\n      flexDirection: \"row\",\n      gap: 4,\n    },\n    socialLabel: {\n      color: quiet,\n      fontSize: 6.5,\n      fontWeight: \"bold\",\n      letterSpacing: 0.4,\n      textTransform: \"uppercase\",\n    },\n    socialList: {\n      alignItems: \"center\",\n      flexDirection: \"row\",\n      gap: 8,\n    },\n    socialValue: {\n      color: quiet,\n      fontSize: 6.5,\n    },\n    stripe: {\n      backgroundColor: accent,\n      height: TICKET_SIZE.height,\n      width: STRIPE_WIDTH,\n    },\n    stub: {\n      alignItems: \"center\",\n      backgroundColor: accent,\n      height: TICKET_SIZE.height,\n      justifyContent: \"space-between\",\n      paddingBottom: 14,\n      paddingHorizontal: 12,\n      paddingTop: 16,\n      width: STUB_WIDTH,\n    },\n    terms: {\n      color: quiet,\n      flex: 1,\n      fontSize: 6.5,\n      lineHeight: 1.4,\n      paddingRight: 8,\n    },\n    title: {\n      color: ink,\n      fontSize: 26,\n      fontWeight: \"bold\",\n      letterSpacing: -0.6,\n      lineHeight: 1.08,\n      marginBottom: 8,\n    },\n    value: {\n      color: ink,\n      fontSize: 12,\n      fontWeight: \"bold\",\n    },\n    venue: {\n      color: quiet,\n      fontSize: 10,\n      lineHeight: 1.4,\n    },\n  });\n\n  return (\n    <Document title={`${data.eventName} ${data.ticketNumber}`}>\n      <Page margin={0} size={TICKET_SIZE}>\n        <View style={styles.page as never}>\n          <View style={styles.stripe} />\n\n          <View style={styles.body}>\n            <View>\n              <View style={styles.brand}>\n                {data.logoUrl && (\n                  <PdfImage\n                    fit=\"contain\"\n                    src={data.logoUrl}\n                    style={styles.logo}\n                  />\n                )}\n                <Text noMargin style={styles.organizer}>\n                  {data.organizer ?? \"Event ticket\"}\n                </Text>\n                <View style={styles.pill}>\n                  <Text noMargin style={styles.pillText}>\n                    {data.ticketType}\n                  </Text>\n                </View>\n              </View>\n              <Text noMargin style={styles.title}>\n                {data.eventName}\n              </Text>\n              <Text noMargin style={styles.venue}>\n                {data.venue}\n              </Text>\n              <Text noMargin style={styles.venue}>\n                {data.address}\n              </Text>\n            </View>\n\n            <View>\n              <View style={styles.facts}>\n                <View style={styles.fact}>\n                  <Text noMargin style={styles.label}>\n                    Date\n                  </Text>\n                  <Text noMargin style={styles.value}>\n                    {data.eventDate}\n                  </Text>\n                </View>\n                <View style={styles.fact}>\n                  <Text noMargin style={styles.label}>\n                    Time\n                  </Text>\n                  <Text noMargin style={styles.value}>\n                    {data.eventTime}\n                  </Text>\n                </View>\n                {data.doorsOpen && (\n                  <View style={styles.fact}>\n                    <Text noMargin style={styles.label}>\n                      Doors\n                    </Text>\n                    <Text noMargin style={styles.value}>\n                      {data.doorsOpen}\n                    </Text>\n                  </View>\n                )}\n                {data.seat && (\n                  <View style={styles.fact}>\n                    <Text noMargin style={styles.label}>\n                      Seat\n                    </Text>\n                    <Text noMargin style={styles.value}>\n                      {`${data.seat.section}–${data.seat.row}–${data.seat.number}`}\n                    </Text>\n                  </View>\n                )}\n              </View>\n              {(data.terms ||\n                (data.socialLinks && data.socialLinks.length > 0)) && (\n                <View style={styles.footer}>\n                  {data.terms && (\n                    <Text noMargin style={styles.terms}>\n                      {data.terms}\n                    </Text>\n                  )}\n                  {data.socialLinks && data.socialLinks.length > 0 && (\n                    <View style={styles.socialList}>\n                      {data.socialLinks.map((link) => (\n                        <View\n                          key={`${link.platform}-${link.url}`}\n                          style={styles.socialItem}\n                        >\n                          <Text noMargin style={styles.socialLabel}>\n                            {link.platform}\n                          </Text>\n                          <Text noMargin style={styles.socialValue}>\n                            {socialHandle(link.url)}\n                          </Text>\n                        </View>\n                      ))}\n                    </View>\n                  )}\n                </View>\n              )}\n            </View>\n          </View>\n\n          <View style={styles.stub}>\n            <Text noMargin style={styles.admit}>\n              Admit one\n            </Text>\n            <View style={styles.qr}>\n              {data.qrCodeUrl ? (\n                <PdfImage\n                  fit=\"contain\"\n                  height={68}\n                  src={data.qrCodeUrl}\n                  width={68}\n                />\n              ) : (\n                <PdfQRCode\n                  backgroundColor=\"#ffffff\"\n                  color=\"#000000\"\n                  size={68}\n                  value={data.ticketNumber}\n                />\n              )}\n            </View>\n            <Text noMargin style={styles.serial}>\n              {data.ticketNumber}\n            </Text>\n          </View>\n\n          <View style={styles.perforation}>\n            {perforationOffsets.map((offset) => (\n              <View\n                key={`perforation-${offset}`}\n                style={styles.perforationDash}\n              />\n            ))}\n          </View>\n\n          <View style={styles.notchTop} />\n          <View style={styles.notchBottom} />\n        </View>\n      </Page>\n    </Document>\n  );\n};\n\nexport const EventTicketDocument = ({\n  theme,\n  data = sampleData,\n}: {\n  theme?: PdfcnTheme;\n  data?: EventTicketData;\n}) => (\n  <PdfcnThemeProvider theme={theme}>\n    <EventTicketContent data={data} />\n  </PdfcnThemeProvider>\n);\n",
      "type": "registry:block",
      "target": "components/pdf/blocks/event-ticket/event-ticket.tsx"
    },
    {
      "path": "registry/bases/forme/blocks/event-ticket/event-ticket.types.ts",
      "content": "export interface EventTicketSeat {\n  number: string;\n  row: string;\n  section: string;\n}\n\nexport interface EventTicketSocialLink {\n  platform: string;\n  url: string;\n}\n\nexport interface EventTicketData {\n  accentColor?: string;\n  address: string;\n  doorsOpen?: string;\n  eventName: string;\n  eventDate: string;\n  eventTime: string;\n  logoUrl?: string;\n  organizer?: string;\n  qrCodeUrl?: string;\n  seat?: EventTicketSeat;\n  socialLinks?: EventTicketSocialLink[];\n  terms?: string;\n  ticketNumber: string;\n  ticketType: string;\n  venue: string;\n}\n",
      "type": "registry:block",
      "target": "components/pdf/blocks/event-ticket/event-ticket.types.ts"
    }
  ],
  "type": "registry:block"
}