> For the complete documentation index, see [llms.txt](https://help.pixfizz.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.pixfizz.com/ecommerce/pixfizz-ecommerce/shopper-managed-storefront/auto-detect-page-count-from-pdfs.md).

# Auto-detect page count from PDFs

When customers upload a PDF file on a product page, Pixfizz can automatically read the page count and page dimensions from the file. This is useful for print products where pricing depends on page count, or where you want to warn customers if their file doesn't match the expected size.

### What it does

* Detects the number of pages in an uploaded PDF and sets it on a target option (e.g. `page_count`)
* Optionally displays the detected page dimensions (width × height in inches)
* Optionally compares the detected size against an expected size and shows a green (match) or amber (mismatch) message
* PDF.js library loads lazily — only fetched when a customer actually uploads a PDF, so there's no performance impact on pages that don't use it

### Requirements

Your product needs two template options:

1. A **File Upload** option (where the customer uploads the PDF)
2. A **Number** option for page count (where the detected value gets written)

The page count option should typically be set to **read\_only** so the customer can't manually change it.

***

### Step 1: Set up the page count option

Create or edit a template option with these settings:

| Field         | Value                                          |
| ------------- | ---------------------------------------------- |
| Name          | Page Count                                     |
| Code          | `page_count`                                   |
| Type          | Number                                         |
| Default Value | `1.0`                                          |
| Min Value     | `1.0`                                          |
| Price         | `value * 0` (or your per-page pricing formula) |

Then add the following **custom fields** on that option:

| Custom field      | Value                    | Purpose                                                                                                            |
| ----------------- | ------------------------ | ------------------------------------------------------------------------------------------------------------------ |
| `read_only`       | `true` (checked)         | Renders as a display-only chip instead of an editable input. The value is still submitted via a hidden form field. |
| `oninput_handler` | *(optional — see below)* | JavaScript to sync the value to other inputs if needed                                                             |

The `read_only` display will show the default value (e.g. "1") before any PDF is uploaded, and update automatically when a PDF is detected.

#### When do I need `oninput_handler`?

Only if the page count value needs to be copied to other inputs on the same page. For example, if you have a matching product variant called `page_count` that also needs to receive the value, you would set:

```
this.closest('px-option-selector').querySelectorAll('input[name^="variants[page_count_"]').forEach(element => element.value = this.value);
```

If the page count only feeds into the pricing formula on the template option itself, you don't need this field.

***

### Step 2: Configure the file upload option

On your **File Upload** template option, add the following custom fields:

| Custom field          | Value        | Required?                                                                                   |
| --------------------- | ------------ | ------------------------------------------------------------------------------------------- |
| `pdf_page_target`     | `page_count` | **Yes** — this activates PDF detection. The value must match the code of the target option. |
| `pdf_expected_width`  | e.g. `8.5`   | Optional — expected page width in inches                                                    |
| `pdf_expected_height` | e.g. `11`    | Optional — expected page height in inches                                                   |

#### Page target (required)

`pdf_page_target` is the only required field. Set its value to the **code** of the option where the page count should be written. When a customer uploads a PDF, the script will:

1. Read the number of pages
2. Set that value on the target option's input
3. Update the read\_only display chip (if the target uses `read_only`)
4. Trigger a price recalculation

#### Size validation (optional)

If you set both `pdf_expected_width` and `pdf_expected_height`, the script will compare the uploaded PDF's first page dimensions against your expected values.

* **Tolerance:** ±0.25 inches — small variations from PDF creators are normal
* **Orientation-agnostic:** An 8.5 × 11 expected size will match an 11 × 8.5 (landscape) PDF
* **Non-blocking:** A size mismatch shows an amber warning but does not prevent the customer from adding to cart

#### Feedback messages

After a PDF is uploaded, the customer sees a small info bar below the upload button:

* **No expected size configured:** Neutral grey — "Detected: 8.50 × 11.00 in, 4 pages"
* **Size matches:** Green — "✓ Detected: 8.50 × 11.00 in, 4 pages"
* **Size mismatch:** Amber — "⚠ Detected: 3.58 × 2.40 in, 2 pages / Expected: 8.50 × 11.00 in"
* **Unreadable file:** Red — "Could not read PDF"

***

### Example: Business card printing

A print shop accepts business card PDFs. The product is priced per page.

**File Upload option custom fields:**

| Custom field          | Value        |
| --------------------- | ------------ |
| `pdf_page_target`     | `page_count` |
| `pdf_expected_width`  | `3.5`        |
| `pdf_expected_height` | `2`          |

**Page Count option settings:**

| Field         | Value          |
| ------------- | -------------- |
| Code          | `page_count`   |
| Type          | Number         |
| Default Value | `1.0`          |
| Price         | `value * 0.10` |

**Page Count custom fields:**

| Custom field | Value   |
| ------------ | ------- |
| `read_only`  | checked |

When a customer uploads a 2-page PDF at 3.58 × 2.40 inches, they see:

* Green size match message (within tolerance of 3.5 × 2)
* Page count chip updates to "2"
* Price updates to $0.20

***

### Notes

* The PDF.js library (\~400KB) is only loaded when a customer actually uploads a PDF file — not on page load
* Non-PDF files are silently ignored (no error, no detection)
* The script runs entirely in the browser — no server round-trip needed
* The `product/px-pdf-detection` snippet is included on the parent Shopper template and activates automatically when `pdf_page_target` is configured
* The size is measured from the first page of the PDF only
