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.

10.7 KB
Raw Blame
{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "forme/shipping-label",
  "title": "Shipping Label",
  "description": "Shipping Label PDF block (forme)",
  "dependencies": [
    "@formepdf/react",
    "@formepdf/core",
    "qrcode"
  ],
  "registryDependencies": [
    "@pdfcn/forme/utils",
    "@pdfcn/forme/qrcode",
    "@pdfcn/forme/text"
  ],
  "files": [
    {
      "path": "registry/bases/forme/blocks/shipping-label/shipping-label.tsx",
      "content": "import { Document, Page, StyleSheet, View } from \"@formepdf/react\";\n\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 { Image } from \"@/lib/pdf-primitives\";\nimport { resolveColor } from \"@/lib/resolve-color\";\nimport type { PdfcnTheme } from \"@/types/pdf-themes\";\n\nimport type {\n  ShippingLabelAddress,\n  ShippingLabelData,\n} from \"./shipping-label.types\";\n\n// Sample data — replace with your own props or data source\nconst sampleData: ShippingLabelData = {\n  carrier: \"UPS\",\n  dimensions: '12\" x 8\" x 6\"',\n  from: {\n    address: \"456 Industrial Blvd\",\n    city: \"Los Angeles\",\n    country: \"USA\",\n    name: \"ACME Corporation\",\n    state: \"CA\",\n    zip: \"90001\",\n  },\n  handlingLabels: [\"FRAGILE\", \"THIS SIDE UP\"],\n  packageCount: 1,\n  postage: \"$18.40\",\n  serviceLevel: \"Ground\",\n  to: {\n    address: \"123 Main Street, Apt 4B\",\n    city: \"New York\",\n    country: \"USA\",\n    name: \"John Doe\",\n    phone: \"(503) 555-0142\",\n    state: \"NY\",\n    zip: \"10001\",\n  },\n  trackingNumber: \"TRACK123456789US\",\n  weight: \"2.5 kg\",\n};\n\n// Standard 4\" x 6\" shipping label, expressed in PDF points (72 dpi)\nconst LABEL_SIZE = { height: 432, width: 288 };\n\n// Quiet zone kept clear of the printed label frame, plus the frame itself\nconst LABEL_MARGIN = 8;\nconst LABEL_BORDER = 2;\nconst LABEL_PADDING = 10;\n\n// Forme scales an image with the Image element's own width/height (style\n// dimensions only size the box), so the barcode is measured in points from\n// the label's content box instead of a percentage width.\nconst BARCODE_WIDTH =\n  LABEL_SIZE.width - 2 * (LABEL_MARGIN + LABEL_BORDER + LABEL_PADDING);\nconst BARCODE_HEIGHT = 72;\n\nconst formatCityLine = (party: ShippingLabelAddress) => {\n  const cityStateZip = `${party.city}, ${party.state} ${party.zip}`;\n  return party.country ? `${cityStateZip}, ${party.country}` : cityStateZip;\n};\n\n// Shared address block — same treatment for both parties (10pt, bold name).\n// Forme's serializer drops components that return a Fragment, so the lines are\n// wrapped in a View; inside the column parent the stacking is identical.\nconst AddressLines = ({\n  party,\n}: {\n  party: ShippingLabelAddress & { phone?: string };\n}) => (\n  <View>\n    <Text noMargin variant=\"xs\" weight=\"bold\">\n      {party.name}\n    </Text>\n    <Text noMargin variant=\"xs\">\n      {party.address}\n    </Text>\n    <Text noMargin variant=\"xs\">\n      {formatCityLine(party)}\n    </Text>\n    {party.phone && (\n      <Text noMargin variant=\"xs\" color=\"mutedForeground\">\n        {party.phone}\n      </Text>\n    )}\n  </View>\n);\n\nconst ShippingLabelContent = ({ data }: { data: ShippingLabelData }) => {\n  const theme = usePdfcnTheme();\n\n  const accent = resolveColor(\n    data.accentColor ?? theme.colors.primary,\n    theme.colors\n  );\n\n  const detailRows = [\n    ...(data.weight ? [{ key: \"Weight\", value: data.weight }] : []),\n    ...(data.dimensions ? [{ key: \"Dimensions\", value: data.dimensions }] : []),\n    ...(typeof data.packageCount === \"number\"\n      ? [{ key: \"Packages\", value: `${data.packageCount}` }]\n      : []),\n    { key: \"Postage\", value: data.postage ?? \"PAID\" },\n  ];\n\n  const styles = StyleSheet.create({\n    addresses: { flex: 1, flexDirection: \"row\" },\n    badgesRow: {\n      flexDirection: \"row\",\n      flexWrap: \"wrap\",\n      gap: 6,\n      marginTop: 8,\n    },\n    barcodeArea: { alignItems: \"center\", marginTop: 10 },\n    carrierName: { fontSize: 16, fontWeight: \"bold\" },\n    fromColumn: { flex: 1, paddingLeft: 12 },\n    handlingTag: {\n      alignItems: \"center\",\n      backgroundColor: theme.colors.foreground,\n      justifyContent: \"center\",\n      paddingHorizontal: 8,\n      paddingVertical: 4,\n    },\n    handlingTagText: {\n      color: theme.colors.background,\n      fontSize: 8,\n      fontWeight: \"bold\",\n      letterSpacing: 1,\n      textTransform: \"uppercase\",\n    },\n    headerRow: {\n      alignItems: \"center\",\n      flexDirection: \"row\",\n      justifyContent: \"space-between\",\n    },\n    label: {\n      borderColor: theme.colors.foreground,\n      borderWidth: LABEL_BORDER,\n      flex: 1,\n      padding: LABEL_PADDING,\n    },\n    page: {\n      backgroundColor: theme.colors.background,\n      flex: 1,\n      flexDirection: \"column\",\n      height: LABEL_SIZE.height - LABEL_MARGIN * 2,\n      overflow: \"hidden\",\n    },\n    rule: {\n      backgroundColor: theme.colors.foreground,\n      height: 2,\n      marginVertical: 6,\n    },\n    sectionLabel: {\n      color: accent,\n      fontSize: 8,\n      fontWeight: \"bold\",\n      letterSpacing: 1,\n      marginBottom: 4,\n      textTransform: \"uppercase\",\n    },\n    serviceRight: { alignItems: \"flex-end\" },\n    shipToColumn: {\n      borderRightColor: theme.colors.foreground,\n      borderRightWidth: 2,\n      flex: 1.4,\n      paddingRight: 12,\n    },\n    table: {\n      borderColor: theme.colors.foreground,\n      borderWidth: 2,\n    },\n    tableKey: {\n      color: theme.colors.mutedForeground,\n      fontSize: 8,\n      fontWeight: \"bold\",\n      letterSpacing: 0.5,\n      textTransform: \"uppercase\",\n      width: 84,\n    },\n    tableRow: {\n      alignItems: \"center\",\n      borderBottomColor: theme.colors.border,\n      borderBottomWidth: 1,\n      flexDirection: \"row\",\n      paddingHorizontal: 8,\n      paddingVertical: 3,\n    },\n    tableRowLast: {\n      alignItems: \"center\",\n      flexDirection: \"row\",\n      paddingHorizontal: 8,\n      paddingVertical: 3,\n    },\n    tableValue: { flex: 1 },\n    trackingText: {\n      fontWeight: \"bold\",\n      letterSpacing: 2,\n      marginTop: 6,\n      textTransform: \"uppercase\",\n    },\n  });\n\n  return (\n    <Document title={`Shipping Label ${data.trackingNumber}`}>\n      <Page size={LABEL_SIZE} margin={LABEL_MARGIN}>\n        <View style={styles.page as never}>\n          <View style={styles.label}>\n            <View style={styles.headerRow}>\n              <Text noMargin style={styles.carrierName}>\n                {data.carrier}\n              </Text>\n              <View style={styles.serviceRight}>\n                <Text noMargin style={styles.sectionLabel}>\n                  {data.serviceLevel}\n                </Text>\n              </View>\n            </View>\n\n            <View style={styles.rule} />\n\n            <View style={styles.addresses}>\n              <View style={styles.shipToColumn}>\n                <Text noMargin style={styles.sectionLabel}>\n                  Ship To\n                </Text>\n                <AddressLines party={data.to} />\n              </View>\n              <View style={styles.fromColumn}>\n                <Text noMargin style={styles.sectionLabel}>\n                  From\n                </Text>\n                <AddressLines party={data.from} />\n              </View>\n            </View>\n\n            <View style={styles.rule} />\n\n            <View style={styles.table}>\n              {detailRows.map((row, index) => (\n                <View\n                  key={row.key}\n                  style={\n                    index === detailRows.length - 1\n                      ? styles.tableRowLast\n                      : styles.tableRow\n                  }\n                >\n                  <Text noMargin style={styles.tableKey}>\n                    {row.key}\n                  </Text>\n                  <Text noMargin style={styles.tableValue} variant=\"xs\">\n                    {row.value}\n                  </Text>\n                </View>\n              ))}\n            </View>\n\n            {data.handlingLabels && data.handlingLabels.length > 0 && (\n              <View style={styles.badgesRow}>\n                {data.handlingLabels.map((label, index) => (\n                  <View key={`${label}-${index}`} style={styles.handlingTag}>\n                    <Text noMargin style={styles.handlingTagText}>\n                      {label}\n                    </Text>\n                  </View>\n                ))}\n              </View>\n            )}\n\n            <View style={styles.barcodeArea}>\n              {data.barcodeUrl ? (\n                <Image\n                  height={BARCODE_HEIGHT}\n                  src={data.barcodeUrl}\n                  width={BARCODE_WIDTH}\n                />\n              ) : (\n                <PdfQRCode\n                  backgroundColor=\"#ffffff\"\n                  color=\"#000000\"\n                  size={80}\n                  value={data.trackingNumber}\n                />\n              )}\n              <Text\n                noMargin\n                style={styles.trackingText}\n                variant=\"xs\"\n                weight=\"bold\"\n              >\n                {data.trackingNumber}\n              </Text>\n            </View>\n          </View>\n        </View>\n      </Page>\n    </Document>\n  );\n};\n\nexport const ShippingLabelDocument = ({\n  theme,\n  data = sampleData,\n}: {\n  theme?: PdfcnTheme;\n  data?: ShippingLabelData;\n}) => (\n  <PdfcnThemeProvider theme={theme}>\n    <ShippingLabelContent data={data} />\n  </PdfcnThemeProvider>\n);\n",
      "type": "registry:block",
      "target": "components/pdf/blocks/shipping-label/shipping-label.tsx"
    },
    {
      "path": "registry/bases/forme/blocks/shipping-label/shipping-label.types.ts",
      "content": "export interface ShippingLabelAddress {\n  name: string;\n  address: string;\n  city: string;\n  state: string;\n  zip: string;\n  country?: string;\n}\n\nexport interface ShippingLabelDestination extends ShippingLabelAddress {\n  phone?: string;\n}\n\nexport interface ShippingLabelData {\n  from: ShippingLabelAddress;\n  to: ShippingLabelDestination;\n  carrier: string;\n  serviceLevel: string;\n  trackingNumber: string;\n  barcodeUrl?: string;\n  weight?: string;\n  dimensions?: string;\n  packageCount?: number;\n  handlingLabels?: string[];\n  postage?: string;\n  accentColor?: string;\n}\n",
      "type": "registry:block",
      "target": "components/pdf/blocks/shipping-label/shipping-label.types.ts"
    }
  ],
  "type": "registry:block"
}