# Web Browser

## Content

<span style="color: #7cafc2">Updated 3/06/2026.</span>

## Using DsPdfJS in the Browser

### What is the Browser Environment?

In the browser environment, **DsPdfJS** (`@mescius/ds-pdf`) runs entirely client-side using WebAssembly.

* No server processing required
* No native installation
* Works in modern browsers with WebAssembly support
* PDF files are generated fully in the browser

## Prerequisites

* **Node.js (version 16 or higher)**
* **npm (version 7 or higher)**
* A **modern browser with WebAssembly support** (Chrome, Edge, Firefox, Safari)
* A **code editor** (VS Code recommended)
* Basic knowledge of **HTML and JavaScript**

## Working Example

***

### Step 1 — Create a Project

```auto
mkdir dspdf-browser-demo
cd dspdf-browser-demo
npm init -y
```

### Step 2 — Install DsPdfJS

```auto
npm install @mescius/ds-pdf
```

***

### Step 3 — Run a Dev Server (Vite example)

Modern browsers require a web server to load WASM files due to security restrictions (CORS and module loading rules).
To run the project locally, install a lightweight development server such as Vite:

```auto
npm install vite --save-dev
```

Add this to your `package.json`:

```auto
"scripts": {
  "dev": "vite",
  "build": "vite build",
  "preview": "vite preview"
},
```

***

### Step 4 — Create `index.html`

```html
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8" />
  <title>DsPdfJS Browser Example – npm + Vite</title>
</head>
<body>
  <h1>Create a PDF in the Browser with DsPdfJS</h1>

  <p>
    This example shows how to generate a PDF file in the browser using
    <strong>DsPdfJS</strong> (<code>@mescius/ds-pdf</code>) installed via npm.
  </p>

  <button id="generateBtn">Generate PDF</button>
  <br /><br />
  <a id="downloadLink" style="display:none;"></a>

<script type="module">
  import {
    connectDsPdf,
    DsPdfConfig,
    ObjectManager,
    PdfDocument
  } from "@mescius/ds-pdf";

  import wasmUrl from "@mescius/ds-pdf/assets/DsPdf.wasm?url";

  // Configure the WebAssembly file location
  DsPdfConfig.wasmUrl = wasmUrl;

  async function generatePdf() {
    const connected = await connectDsPdf();
    if (!connected) {
      alert("Failed to initialize DsPdfJS.");
      return;
    }

    const om = new ObjectManager();

    try {
      const doc = new PdfDocument(om);
      const page = doc.newPageContext();

      page.drawText(
        { text: "Hello from DsPdfJS!", fontSize: 20 },
        50,
        50
      );

      const pdfData = doc.savePdf();
      const buffer = pdfData.buffer || pdfData;

      const blob = new Blob([buffer], { type: "application/pdf" });
      const url = URL.createObjectURL(blob);

      const link = document.getElementById("downloadLink");
      link.href = url;
      link.download = "dspdf-browser-example.pdf";
      link.textContent = "Download generated PDF";
      link.style.display = "inline";
    } finally {
      om.dispose();
    }
  }

  document
    .getElementById("generateBtn")
    .addEventListener("click", generatePdf);
</script>
</body>
</html>
```

### Step 5 — Run the Project

```plaintext
npm run dev
```

Open:

```bash
http://localhost:5173
```

### Expected Result

* Click **Generate PDF**
* A download link appears
* The generated file contains the text:

```auto
Hello from DsPdfJS!
```

### Result screenshot

![image](https://cdn.mescius.io/document-site-files/images/4af0c996-6cb0-423e-871d-4c3f8e1cfd98/image-20260224.7dc4ed.png)

### Project Structure

```auto
dspdf-browser-demo/
├── node_modules/
├── package.json
├── package-lock.json
└── index.html
```