<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ Tutorial - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ Tutorial - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Thu, 06 Aug 2026 09:15:32 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/tutorial/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ The ETL Pipeline Handbook: How to Build a Production-Grade Pipeline in Python ]]>
                </title>
                <description>
                    <![CDATA[ Tracking flood risk takes one unglamorous but essential thing: clean and structured data. In this tutorial, you'll build a data pipeline yourself. You'll create a Python ETL (Extract, Transform, Load) ]]>
                </description>
                <link>https://www.freecodecamp.org/news/the-etl-pipeline-handbook-how-to-build-a-production-grade-pipeline-in-python/</link>
                <guid isPermaLink="false">6a679921f4d9ad6845fede20</guid>
                
                    <category>
                        <![CDATA[ data-engineering ]]>
                    </category>
                
                    <category>
                        <![CDATA[ handbook ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Tutorial ]]>
                    </category>
                
                    <category>
                        <![CDATA[ ETL ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Python ]]>
                    </category>
                
                    <category>
                        <![CDATA[ python projects ]]>
                    </category>
                
                    <category>
                        <![CDATA[ pandas ]]>
                    </category>
                
                    <category>
                        <![CDATA[ automation ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Pipeline ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ brooklyn ]]>
                </dc:creator>
                <pubDate>Mon, 27 Jul 2026 17:45:05 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/uploads/covers/5e1e335a7a1d3fcc59028c64/36906090-d056-4207-8632-fcdf35843018.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Tracking flood risk takes one unglamorous but essential thing: clean and structured data.</p>
<p>In this tutorial, you'll build a data pipeline yourself. You'll create a Python ETL (Extract, Transform, Load) pipeline that pulls daily water-level readings from <a href="https://hubeau.eaufrance.fr/">Hub'Eau</a>, France's official open water-data API. Then, you'll clean that data and publish it as a public dataset, just like the <a href="https://www.kaggle.com/code/grimespoint/paris-flood-dataset-weekly-updater">live version</a> does.</p>
<p>This tutorial is based on a real pipeline that runs once a week, on a schedule, and it keeps the <a href="https://www.kaggle.com/datasets/grimespoint/paris-flood-dataset">Paris Flood Dataset</a> updated automatically.</p>
<p>You won't just copy and paste code, though. The real goal is to understand <em>why</em> the pipeline works the way it does. You'll walk through the design decisions that separate a script meant to run once from a script that keeps working, unattended, for years.</p>
<p>You can code along with this <a href="https://www.kaggle.com/code/grimespoint/data-engineering-with-python-etl-pipeline">notebook</a>. For most of the tutorial, the pipeline runs on <strong>simulated (mock) API data</strong>. This lets you run every cell safely, without hammering a real server. A later section shows how to switch to the live API.</p>
<p>By the end, you'll be able to:</p>
<ul>
<li><p>Explain and implement the Extract, Transform, Load pattern</p>
</li>
<li><p>Manage configuration with Python <code>@dataclass</code> instead of scattering constants everywhere</p>
</li>
<li><p>Write API-fetching code that survives network failures and paginated responses</p>
</li>
<li><p>Apply robust type-coercion so one bad row can't crash a whole pipeline run</p>
</li>
<li><p>Deduplicate and merge incremental data safely</p>
</li>
<li><p>Wire everything into a single, idempotent, schedulable <code>main()</code> entry point</p>
</li>
</ul>
<h3 id="heading-table-of-contents">Table of Contents:</h3>
<ul>
<li><p><a href="#heading-prerequisites">Prerequisites</a></p>
</li>
<li><p><a href="#heading-part-1-the-design-logic">Part 1: The Design Logic</a></p>
<ul>
<li><p><a href="#heading-what-is-an-etl-pipeline">What is an ETL pipeline?</a></p>
</li>
<li><p><a href="#heading-the-architecture-at-a-glance">The architecture, at a glance</a></p>
</li>
<li><p><a href="#heading-two-patterns-that-make-it-production-grade">Two patterns that make it production-grade</a></p>
</li>
</ul>
</li>
<li><p><a href="#heading-part-2-setup-and-dependencies">Part 2: Setup and Dependencies</a></p>
</li>
<li><p><a href="#heading-part-3-manage-configuration-with-dataclasses">Part 3: Manage Configuration with Dataclasses</a></p>
<ul>
<li><p><a href="#heading-why-bother-with-a-config-layer-at-all">Why bother with a config layer at all?</a></p>
</li>
<li><p><a href="#heading-code-level-walkthrough">Code-level walkthrough</a></p>
</li>
</ul>
</li>
<li><p><a href="#heading-part-4-the-extraction-step">Part 4: The Extraction Step</a></p>
<ul>
<li><p><a href="#heading-graceful-file-loading">Graceful file loading</a></p>
</li>
<li><p><a href="#heading-incremental-update-logic">Incremental update logic</a></p>
</li>
<li><p><a href="#heading-simulate-the-api">Simulate the API</a></p>
</li>
<li><p><a href="#heading-fetch-data-for-real">Fetch data for real</a></p>
</li>
</ul>
</li>
<li><p><a href="#heading-part-5-the-transform-step">Part 5: The Transform Step</a></p>
<ul>
<li><p><a href="#heading-type-parsing-and-graceful-coercion">Type parsing and graceful coercion</a></p>
</li>
<li><p><a href="#heading-schema-translation-with-bidirectional-mappings">Schema translation with bidirectional mappings</a></p>
</li>
<li><p><a href="#heading-compute-flood-alerts">Compute flood alerts</a></p>
</li>
<li><p><a href="#heading-column-ordering">Column ordering</a></p>
</li>
<li><p><a href="#heading-deduplication">Deduplication</a></p>
</li>
<li><p><a href="#heading-put-it-all-together-in-postprocess">Put it all together inpostprocess()</a></p>
</li>
</ul>
</li>
<li><p><a href="#heading-part-6-the-load-step">Part 6: The Load Step</a></p>
<ul>
<li><p><a href="#heading-design-logic">Design logic</a></p>
</li>
<li><p><a href="#heading-code-level-walkthrough">Code-level walkthrough</a></p>
</li>
</ul>
</li>
<li><p><a href="#heading-part-7-assemble-the-full-pipeline">Part 7: Assemble the Full Pipeline</a></p>
<ul>
<li><p><a href="#heading-the-global-rehearsal-mock-mode">The global rehearsal (mock mode)</a></p>
</li>
<li><p><a href="#heading-main-pipeline-orchestration">main(): pipeline orchestration</a></p>
</li>
<li><p><a href="#heading-the-if-name-main-guard">Theif name == "main":guard</a></p>
</li>
</ul>
</li>
<li><p><a href="#heading-part-8-go-live-and-switch-to-the-real-api">Part 8: Go Live and Switch to the Real API</a></p>
<ul>
<li><a href="#heading-post-run-validation">Post-run validation</a></li>
</ul>
</li>
<li><p><a href="#heading-summary">Summary</a></p>
</li>
<li><p><a href="#heading-references">References</a></p>
</li>
</ul>
<h2 id="heading-prerequisites">Prerequisites</h2>
<ul>
<li><p><strong>Python 3.10+</strong>. The code uses type hints and dataclasses. These work from Python 3.7 onward, but 3.10+ is best.</p>
</li>
<li><p><a href="https://leetcode.com/studyplan/introduction-to-pandas/"><strong>Knowledge of pandas DataFrames</strong></a>: read data, filter rows, and basic column operations.</p>
</li>
<li><p>Comfort with <strong>functions and basic OOP</strong> (<a href="https://realpython.com/python3-object-oriented-programming/">Object-Oriented programming</a>) in Python. Don't worry, this guide explains every non-obvious piece as you go.</p>
</li>
<li><p>Optional: a free <a href="https://www.kaggle.com/">Kaggle</a> account and the <a href="https://github.com/Kaggle/kaggle-api">Kaggle CLI</a>, only if you want to run the final publishing step for real.</p>
</li>
</ul>
<p>Install the dependencies:</p>
<pre><code class="language-bash">pip install requests pandas numpy ipykernel
</code></pre>
<p>You'll do the work inside a <a href="https://www.kaggle.com/code/grimespoint/data-engineering-with-python-etl-pipeline">Jupyter notebook</a>. Download the <code>.ipynb</code> file from Kaggle, or click "Copy and Edit" to work directly on Kaggle. You'll need a Kaggle account for that.</p>
<img src="https://cdn.hashnode.com/uploads/covers/67c84561e3f229edf2351ba2/681c54fc-c12a-4c01-83ba-17368d4ccc49.png" alt="The menu button that provides options to download the data engineering follow along notebook on Kaggle." style="display:block;margin:0 auto" width="695" height="502" loading="lazy">

<p><strong>Optional:</strong> If you plan to run the notebook locally, install the <code>notebook</code> package too:</p>
<pre><code class="language-bash">pip install notebook
</code></pre>
<h2 id="heading-part-1-the-design-logic">Part 1: The Design Logic</h2>
<p>Before you touch a single line of code, we'll spend three minutes on <em>why</em> the pipeline is shaped this way.</p>
<p>This is the <strong>big-picture view</strong>. Every code-level decision later traces back to one of these ideas. Read this section even if you skim everything else.</p>
<h3 id="heading-what-is-an-etl-pipeline">What is an ETL Pipeline?</h3>
<p>ETL stands for <strong>Extract, Transform, Load</strong>. It's the standard pattern for moving data from a source to a destination in a reliable, repeatable way.</p>
<ul>
<li><p><strong>Extract</strong>: pull data from a source, like an API, a database, or files.</p>
</li>
<li><p><strong>Transform</strong>: clean, standardize, enrich, and validate the data.</p>
</li>
<li><p><strong>Load</strong>: write the result to a destination, like a warehouse, a CSV, or a public platform.</p>
</li>
</ul>
<p>Here's how those three stages map onto this project:</p>
<table>
<thead>
<tr>
<th>Stage</th>
<th>What happens here</th>
</tr>
</thead>
<tbody><tr>
<td><strong>Extract</strong></td>
<td>Load existing data if it exists, then call the Hub'Eau API (via <code>requests</code>) for each gauging station</td>
</tr>
<tr>
<td><strong>Transform</strong></td>
<td>Translate French columns and entries to English, fix data types, remove duplicates</td>
</tr>
<tr>
<td><strong>Load</strong></td>
<td>Write a CSV and a metadata file, then publish to Kaggle via the CLI</td>
</tr>
</tbody></table>
<h3 id="heading-the-architecture-at-a-glance">The Architecture, at a Glance</h3>
<img src="https://cdn.hashnode.com/uploads/covers/67c84561e3f229edf2351ba2/6bede708-bdb9-4cae-bfd8-1dc1aa0348f6.png" alt="Schema of a simple ETL pipeline." style="display:block;margin:0 auto" width="2040" height="900" loading="lazy">

<p>Notice that "Extract" already touches two different sources: the <em>existing</em> dataset (what you already have) and the <em>new</em> data from the API. That distinction is the seed of the next idea.</p>
<h3 id="heading-two-patterns-that-make-it-production-grade">Two Patterns that Make it Production-Grade</h3>
<p>There two patterns that make this pipeline safe to run unattended, scheduled, for years.</p>
<h4 id="heading-1-idempotency">1. Idempotency</h4>
<p><a href="https://en.wikipedia.org/wiki/Idempotence"><strong>Idempotence</strong></a> means when the same operation runs twice it gives the same result as if it runs once. Deduplication is what makes this pipeline idempotent. If the scheduler accidentally triggers twice, or a network retry fetches the same day again, the second run won't create duplicate rows. This matters a lot for anything that runs on a schedule with no one watching it.</p>
<h4 id="heading-2-incremental-loading">2. Incremental loading</h4>
<p>A naïve pipeline would re-download the <em>entire</em> history on every run. That's slow, it wastes your API quota, and it's fragile: the more data you transfer, the more chances something fails.</p>
<p>An <strong>incremental</strong> pipeline avoids this. Instead, it:</p>
<ul>
<li><p>Checks the most recent date already in the dataset</p>
</li>
<li><p>Requests only the data <em>from that date onward</em></p>
</li>
<li><p>Merges the new records into the existing dataset</p>
</li>
</ul>
<p>You'll find this logic built explicitly in <a href="#heading-incremental-update-logic"><code>determine_update_range</code></a>.</p>
<p>Always ask yourself: <strong>"Do I actually need to do this?"</strong> That one habit separates a fragile script from a production pipeline. For example, every "expensive" or "external" step in this pipeline (network calls, disk writes, publishing) is guarded by a cheap, local check first.</p>
<h2 id="heading-part-2-setup-and-dependencies">Part 2: Setup and Dependencies</h2>
<p>Here's the import block. It looks unremarkable, but how it's organized is itself a best practice worth calling out.</p>
<pre><code class="language-python"># Standard library imports
import json
import os
import random
import subprocess
from dataclasses import dataclass, field
from datetime import date, timedelta
from pathlib import Path
from typing import Dict, List, Optional, Set, Tuple

# Third-party libraries
import numpy as np
import pandas as pd
import requests

# Display config for notebooks
pd.set_option('display.max_columns', None)   # All columns will show
pd.set_option('display.max_colwidth', None)  # Prevents cutting long column text with ...
</code></pre>
<p><strong>Code-level notes:</strong></p>
<ul>
<li><p>Imports fall into three blocks: <strong>standard library, then third-party, then local</strong>, with blank lines between them. This follows <a href="https://peps.python.org/pep-0008/#imports">PEP 8's import-ordering convention</a>. Most <a href="https://en.wikipedia.org/wiki/Pretty-printing">auto-formatters</a> (<code>isort</code>, <code>ruff</code>) enforce this same grouping.</p>
</li>
<li><p>Nothing is imported with <code>from module import *</code>. That syntax pollutes the current namespace. It makes it hard to trace where a name came from when someone debugs the code six months later. Python style guides echo this advice, including <a href="https://google.github.io/styleguide/pyguide.html">Google's Python Style Guide</a>.</p>
</li>
<li><p>The <code>pd.set_option(...)</code> calls exist purely for <em>notebook readability</em>, so wide DataFrames don't get truncated with <code>...</code>. They have zero effect on the pipeline's logic. You'd typically remove or scope them differently in a <code>.py</code> script.</p>
</li>
</ul>
<h2 id="heading-part-3-manage-configuration-with-dataclasses">Part 3: Manage Configuration with Dataclasses</h2>
<h3 id="heading-why-bother-with-a-config-layer-at-all">Why Bother with a Config Layer at All?</h3>
<p>Every pipeline has <strong>knobs</strong>: which stations to monitor, what the flood threshold is, and where to publish. The bad approach sprinkles these values as literals throughout the code as you write it. You end up with an <code>if level &gt; 6000:</code> buried three functions deep. Then changing <em>any</em> setting means hunting through the whole file, and it's easy to update one spot and miss another.</p>
<p>The fix: <strong>centralize all settings in one place.</strong> Python's <a href="https://docs.python.org/3/library/dataclasses.html"><code>@dataclass</code></a> decorator is a natural fit for that.</p>
<table>
<thead>
<tr>
<th>Feature</th>
<th>Why it matters</th>
</tr>
</thead>
<tbody><tr>
<td>Auto-generated <code>__init__</code>, <code>__repr__</code>, <code>__eq__</code></td>
<td>You don't need to write them yourself</td>
</tr>
<tr>
<td><a href="https://www.geeksforgeeks.org/python/type-hints-in-python/">Type hints</a></td>
<td>Gives you IDE autocomplete and self-documenting code</td>
</tr>
<tr>
<td>Optional <code>frozen=True</code></td>
<td>Gives you <a href="https://stackoverflow.com/questions/66194804/what-does-frozen-mean-for-dataclasses">true immutability</a> if you want config knobs that can't change after creation</td>
</tr>
<tr>
<td><code>__post_init__</code> hook</td>
<td>Validates or computes derived fields once, right after construction</td>
</tr>
</tbody></table>
<p>Compare that to a configuration written as a plain <code>dict</code>:</p>
<pre><code class="language-python">config = {
    "stations": ["STN001", "STN002"],
    "flood_threshold": 6000,
    "publish_url": "https://example.com/alerts",
    "retry_count": 2,
    "timeout_seconds": 5,
}
</code></pre>
<p>A plain dict gives you none of that: no immutability, no type checking, and no autocomplete.</p>
<h3 id="heading-code-level-walkthrough">Code-Level Walkthrough</h3>
<p>The pipeline defines three configuration classes. Each one has <em>a single responsibility</em>: API details, station rules, and publishing destination. Each class becomes one <strong>module-level singleton instance</strong>. Every other function in the pipeline reads from these singletons.</p>
<pre><code class="language-python">@dataclass
class APIConfig:
    """API configuration for HubEau data fetching.

    Think of this as the "address book" for the API.
    """
    use_mock: bool = True
    base_url: str = "https://hubeau.eaufrance.fr/api/v2/hydrometrie/obs_elab"
    metric: str = "HIXnJ"  # Daily max water level (elaborated observations)
    # Pagination: fetch 20k records per request (API Limit)
    max_per_page: int = 20000
    timeout_seconds: int = 60  # Network timeout

    def __post_init__(self):
        """Validate configuration after initialization."""
        if self.max_per_page &lt;= 0:
            raise ValueError("max_per_page must be positive")
        if self.timeout_seconds &lt;= 0:
            raise ValueError("timeout_seconds must be positive")
</code></pre>
<p>Notice the validation inside <code>__post_init__</code>. It runs right after the auto-generated <code>__init__</code>. A misconfigured <code>APIConfig(max_per_page=-1)</code> fails loudly and immediately at startup, instead of surfacing as a bug later during an actual pipeline run.</p>
<pre><code class="language-python">@dataclass
class StationConfig:
    """Station monitoring configuration.

    The 'what' of data collection: which stations, what's a flood?
    """
    station_codes: List[str] = field(default_factory=lambda: [
        "F700000109", "F700000110", "F700000111",
        "F700000102", "F700000103",
    ])
    flood_threshold_mm: int = 6000  # Flood alert threshold
    earliest_date: str = "1900-01-01"  # How far back to go
</code></pre>
<p><strong>The mutable-default trap:</strong> Look closely at <code>station_codes</code>. It isn't written as <code>station_codes: List[str] = [...]</code>. That's deliberate, and it dodges one of the most common gotchas in Python. If you use a plain mutable object (a list, dict, or set) as a default argument or dataclass field, <strong>every instance shares the same underlying object</strong>. Mutate it on one instance, and you silently mutate it everywhere else too.</p>
<p>Stack Overflow covers this at length in its <a href="https://stackoverflow.com/questions/1132941/least-astonishment-and-the-mutable-default-argument">"least astonishment" mutable-default-argument discussion</a>, as does <a href="https://realpython.com/python-optional-arguments/">Real Python's guide to optional arguments</a>. The fix is <code>field(default_factory=...)</code>. It calls a <em>fresh</em> factory function (here, a <code>lambda</code>) for every new instance, so each one gets its own independent list.</p>
<p>Explanation:</p>
<pre><code class="language-python"># Bad: shared mutable default
@dataclass
class BadConfig:
    station_codes: list[str] = []

a = BadConfig()
b = BadConfig()

a.station_codes.append("ALERT")
print(a.station_codes)  # ['ALERT']
print(b.station_codes)  # ['ALERT']  &lt;-- same list!!


# Good: fresh list per instance
@dataclass
class GoodConfig:
    station_codes: list[str] = field(default_factory=list)

x = GoodConfig()
y = GoodConfig()

x.station_codes.append("ALERT")
print(x.station_codes)  # ['ALERT']
print(y.station_codes)  # []  &lt;-- independent list
</code></pre>
<p>And the last Config singleton, the Kaggle settings:</p>
<pre><code class="language-python">@dataclass
class KaggleConfig:
    """Kaggle dataset publishing configuration."""
    dataset_slug: str = "grimespoint/paris-flood-dataset"
    input_csv: str = "kaggle/input/datasets/{slug}/paris_flood_dataset.csv"
    output_dir: Path = field(default_factory=lambda: Path("kaggle/working/kaggle_dataset"))
    mock_output_dir: Path = field(default_factory=lambda: Path("mock_output"))
    output_filename: str = "paris_flood_dataset.csv"
    mock_output_filename: str = "mock_flood_dataset.csv"
    metadata_filename: str = "dataset-metadata.json"

    # Metadata
    title: str = "Paris flood dataset"
    keywords: list = field(default_factory=lambda: [
        "tabular", "weather and climate", "environment", "europe", "time series analysis"
    ])
    geospatial_coverage: str = "Paris, France"
    update_frequency: str = "Weekly"
    license_name: str = "CC0-1.0"

    # Computed fields (set in __post_init__)
    output_csv_path: Path = field(init=False)
    metadata_path: Path = field(init=False)

    def __post_init__(self):
        """Compute derived paths after initialization."""
        self.input_csv = self.input_csv.format(slug=self.dataset_slug)
        self.output_csv_path = self.output_dir / self.output_filename
        self.metadata_path = self.output_dir / self.metadata_filename
        self.mock_output_filename = self.mock_output_dir / self.mock_output_filename

# Initialize configs - module-level singletons
API_CONFIG = APIConfig()
STATION_CONFIG = StationConfig()
KAGGLE_CONFIG = KaggleConfig()
</code></pre>
<p>This is the most common use of <code>__post_init__</code>. <code>output_csv_path</code> and <code>metadata_path</code> are marked <code>field(init=False)</code>, so you can't set them directly through the constructor. Instead, <code>__post_init__</code> computes them from other fields (<code>output_dir</code> and <code>output_filename</code>).</p>
<p>Use this pattern for <strong>derived, computed values</strong>: compute them once, in one place, instead of recomputing <code>output_dir / output_filename</code> every time you need the path elsewhere in the codebase.</p>
<p>See <a href="https://realpython.com/python-data-classes/#comparing-cards">Real Python's data classes guide</a> or the data classes chapter of O'Reilly's <em>Fluent Python</em> for more on this pattern.</p>
<p><strong>Tip:</strong> the auto-generated <code>__repr__</code> gives you a readable printout for free. Call <code>print(API_CONFIG)</code>: it shows every field and value without a single line of formatting code. It's handy for quick sanity checks when you're debugging a pipeline run.</p>
<pre><code class="language-python">print(APIConfig)
# prints APIConfig(use_mock=True, base_url='https://hubeau.eaufrance.fr/api/v2/hydrometrie/obs_elab', ...)
</code></pre>
<h2 id="heading-part-4-the-extraction-step">Part 4: The Extraction Step</h2>
<p>The Extract phase pulls data from source systems and reads it into memory. Here, you genuinely have <strong>two</strong> sources to extract from: the <em>existing</em> dataset (what you already published last time) and the <em>new</em> (recent) data from the Hub'Eau API.</p>
<h3 id="heading-graceful-file-loading">Graceful File Loading</h3>
<h4 id="heading-design-logic">Design logic</h4>
<p>What should happen the very first time this pipeline runs (and there's no existing dataset yet)? A naïve implementation would crash with a <code>FileNotFoundError</code>.</p>
<p>Here's the trick: <code>load_csv()</code> follows the <a href="https://en.wikipedia.org/wiki/Null_object_pattern"><strong>Null Object pattern</strong></a>. Instead of raising an error, it returns an <em>empty</em> DataFrame. Every downstream function can then treat "no existing data" and "some existing data" the same way, with no special-casing needed.</p>
<h4 id="heading-code-level-walkthrough">Code-level walkthrough</h4>
<p>This function loads a CSV file into a DataFrame. If the file doesn't exist, it returns an empty DataFrame instead of crashing.</p>
<p><code>low_memory=False</code> tells pandas to read the file carefully, so it avoids mixed-type guesses. <code>parse_dates=True</code> tries to automatically convert date-like columns into dates. <code>delimiter=","</code> tells pandas the file is comma-separated.</p>
<pre><code class="language-python">def load_csv(path: str) -&gt; pd.DataFrame:
    """Load CSV file or return empty DataFrame if file does not exist.

    Args:
        path (str): Full path to the CSV file.

    Returns:
        pd.DataFrame: Loaded data, or empty DataFrame if file not found.

    Raises:
        pd.errors.ParserError: If the CSV is malformed.
    """
    if os.path.exists(path):
        return pd.read_csv(path, low_memory=False, parse_dates=True, delimiter=",")
    return pd.DataFrame()   # Null Object: consistent return type
</code></pre>
<p>To test, try it against a path that doesn't exist:</p>
<pre><code class="language-python">df_missing = load_csv("/tmp/does_not_exist.csv")
print(df_missing.empty)  # True: no crash

# Callers can always do this, instead of an `is None` check:
if df_missing.empty:
    print("No existing data. Will run a full fetch from earliest date.")
</code></pre>
<p><strong>Best practice:</strong> return a <strong>consistent type</strong> from every code path in a function. A function that sometimes returns a <code>DataFrame</code> and sometimes <code>None</code> forces every caller to add a <code>None</code> check before using the result.</p>
<p>Return a <code>DataFrame</code>, empty or not, which keeps things simpler. You never have to ask <em>"did I get a real result, or</em> <code>None</code><em>?"</em> before using it.</p>
<h3 id="heading-incremental-update-logic">Incremental Update Logic</h3>
<h4 id="heading-design-logic">Design logic</h4>
<p>This is the "incremental loading" pattern from Part 1 in action. Before you fetch anything, ask: <em>"What's the most recent record I already have, and do I actually need more?"</em></p>
<p><strong>Strategy:</strong> check whether the existing data already covers <em>yesterday</em>.</p>
<ul>
<li><p>Yes: skip the update entirely. Nothing to do, the dataset is current.</p>
</li>
<li><p>No: fetch starting from the day after the last known date.</p>
</li>
</ul>
<pre><code class="language-text">Existing data: Jan. 1 – Jan. 15
Yesterday: Jan. 19

Decision: fetch from Jan. 16 onwards (not from Jan. 1)
</code></pre>
<p>Why <em>yesterday</em> and not <em>today</em>? Today's measurement might not be finalized on the source system yet. Hub'Eau's "elaborated observations" are a processed daily aggregate, so the safest check is against the last <strong>fully completed</strong> day.</p>
<h4 id="heading-code-level-walkthrough">Code-level walkthrough</h4>
<p><code>determine_update_range()</code> checks the newest saved date. It tells you either "you're already up to date" or "start downloading from this next date."</p>
<p>Step by step:</p>
<ol>
<li><p><strong>The function starts with existing data</strong></p>
<ul>
<li><code>existing</code> is a pandas <code>DataFrame</code> that already has some rows of data.</li>
</ul>
</li>
<li><p><strong>If there is no data at all</strong></p>
<ul>
<li><p><code>if existing.empty:</code></p>
</li>
<li><p>If the DataFrame has zero rows, it says: <em>"Nothing is saved yet, so fetch everything."</em></p>
</li>
<li><p>It returns:</p>
<ul>
<li><p><code>True</code> = update needed</p>
</li>
<li><p><code>STATION_CONFIG.earliest_date</code> = start from the earliest allowed date</p>
</li>
</ul>
</li>
</ul>
</li>
<li><p><strong>Find the date column</strong></p>
<ul>
<li><p>The code checks which column contains dates:</p>
<ul>
<li><p>first tries <code>"date_obs_elab"</code></p>
</li>
<li><p>then <code>"record_date"</code></p>
</li>
</ul>
</li>
<li><p>If neither exists, it raises an error because it doesn’t know which column to use.</p>
</li>
</ul>
</li>
<li><p><strong>Convert the date column into real dates</strong></p>
<ul>
<li><p><code>pd.to_datetime(...)</code> turns the column into date objects pandas can work with.</p>
</li>
<li><p><code>errors="coerce"</code> means bad date values become missing values instead of crashing.</p>
</li>
</ul>
</li>
<li><p><strong>Find the latest date in the data</strong></p>
<ul>
<li><p><code>last_day = s.max().date()</code></p>
</li>
<li><p>This gets the newest date already in the dataset.</p>
</li>
</ul>
</li>
<li><p><strong>Compare it with yesterday</strong></p>
<ul>
<li><p><code>yesterday = date.today() - timedelta(days=1)</code></p>
</li>
<li><p>The function checks whether data already includes yesterday.</p>
</li>
</ul>
</li>
<li><p><strong>If data is already up to date</strong></p>
<ul>
<li><p>If <code>last_day &gt;= yesterday</code>, it returns:</p>
<ul>
<li><p><code>False</code> = no update needed</p>
</li>
<li><p><code>None</code> = no start date needed</p>
</li>
</ul>
</li>
</ul>
</li>
<li><p><strong>If data is behind</strong></p>
<ul>
<li><p>It sets <code>next_day</code> to the day after the last saved date.</p>
</li>
<li><p>Then it returns:</p>
<ul>
<li><p><code>True</code> = update needed</p>
</li>
<li><p>that next day as a string like <code>"2026-07-12"</code></p>
</li>
</ul>
</li>
</ul>
</li>
</ol>
<pre><code class="language-python">def determine_update_range(existing: pd.DataFrame) -&gt; Tuple[bool, Optional[str]]:
    """Determine whether an update is needed and from what date.

    Logic:
    1. Check if existing data covers yesterday's date
    2. If yes → no update needed
    3. If no → start fetching from day after last data

    Returns:
        Tuple[should_update, start_date]
    """
    # Case 1: nothing on disk yet
    if existing.empty:
        print("No existing data found. Will fetch all data from earliest date.")
        return True, STATION_CONFIG.earliest_date

    if "date_obs_elab" in existing.columns:
        record_colname = "date_obs_elab"
    elif "record_date" in existing.columns:
        record_colname = "record_date"
    else:
        raise KeyError("Missing date column: expected 'date_obs_elab' or 'record_date'")

    s = pd.to_datetime(existing[record_colname], errors="coerce")
    last_day = s.max().date()
    yesterday = date.today() - timedelta(days=1)

    # Case 2: already current
    if last_day &gt;= yesterday:
        print("\nDataset already covers yesterday or later. No update needed.")
        return False, None

    # Case 3: fetch the gap
    next_day = (last_day + pd.Timedelta(days=1))
    print(f"\nWill retrieve data starting from: {next_day}")
    return True, next_day.isoformat()
</code></pre>
<p>A few things worth a note here.</p>
<p>First, the function checks for <strong>two possible column names</strong>: <code>date_obs_elab</code>, the raw API name, or the already-renamed English name <code>record_date</code>. It doesn't assume just one. This makes the function work whether you call it on freshly-fetched raw data or an already-processed CSV loaded from disk.</p>
<p><code>errors="coerce"</code> shows up here, and it'll show up again (we'll dig into this in Part 5). Any date pandas can't parse becomes <code>NaT</code> (Not a Time) instead of raising an exception.</p>
<p>The return type is <code>Tuple[bool, Optional[str]]</code>. This <a href="https://www.w3schools.com/python/python_tuples.asp">tuple</a> bundles two related results: <em>should I update, and from when?</em> That beats returning two separate values, or worse, one ambiguous value that means different things depending on context.</p>
<p><strong>Best practice:</strong> use <code>Tuple</code> return types (or, for more fields, a small dataclass or <code>NamedTuple</code>) to bundle related results together. Document clearly what each position means. If you return different <em>types</em> from different code paths without documentation, you'll hit a common source of confusion and bugs: <em>"why is this</em> <code>None</code> <em>sometimes and a string other times?"</em>.</p>
<p>Run a quick check against three scenarios:</p>
<pre><code class="language-python"># Test 1: No existing data
should_update, start_date = determine_update_range(pd.DataFrame())
# → True, "1900-01-01"

# Test 2: Old existing data (covers only Jan 10-15)
# → True, "2026-01-16"  (the day after the last known date)

# Test 3: Recent data that already covers yesterday
# → False, None
</code></pre>
<h3 id="heading-simulate-the-api">Simulate the API</h3>
<h4 id="heading-design-logic">Design logic</h4>
<p>In production, fetching means a real HTTP call:</p>
<pre><code class="language-python">requests.get(
    "https://hubeau.eaufrance.fr/api/v2/hydrometrie/obs_elab",
    params={"code_entite": "F700000109", "size": 20000, ...}
)
</code></pre>
<p>Real API calls bring real challenges. Rather than fight those on your first read-through, this tutorial first builds and tests everything against a <strong>mock</strong> generator. It returns data shaped exactly like the real API. Only once the logic works does it swap in the real endpoint.</p>
<p>This technique is useful well beyond this project. Build and test your transform logic against fixtures or mocks first. That way, you're not into "is my parsing wrong?" and "is the network flaky right now?" at the same time.</p>
<h4 id="heading-code-level-walkthrough">Code-level walkthrough</h4>
<p><code>generate_mock_api_data()</code> generates fake sample data for a station, one record per day, starting from <code>start_date</code>.</p>
<p>How it works:</p>
<ul>
<li><p>It converts <code>start_date</code> into a real date.</p>
</li>
<li><p>It loops for <code>num_days</code>.</p>
</li>
<li><p>For each day, it creates a mock observation dictionary.</p>
</li>
<li><p>It adds a small random variation to the water level so the data looks realistic.</p>
</li>
<li><p>It randomly picks status, quality, and method labels.</p>
</li>
<li><p>It returns a list of these dictionaries.</p>
</li>
</ul>
<pre><code class="language-python">def generate_mock_api_data(station_code: str, start_date: str, num_days: int = 10) -&gt; List[Dict]:
    """Generate realistic mock API data for demonstration.

    Simulates what HubEau API would return: list of observation dicts.
    """
    start = pd.to_datetime(start_date).date()
    records = []

    validation_statuses = ["Donnée validée", "Donnée brute", "Donnée pré-validée"]
    qualities = ["Bonne", "Non qualifiée", "Douteuse"]
    methods = ["Mesurée", "Calculée", "Expertisée"]

    for i in range(num_days):
        obs_date = start + timedelta(days=i)
        base_level = 5500 + int(station_code[-2:])  # Varies by station
        noise = random.randint(-200, 200)
        water_level = base_level + noise

        record = {
            "code_site": "mock_" + station_code[1:],
            "code_station": "mock_" + station_code,
            "date_obs_elab": obs_date.isoformat(),
            "resultat_obs_elab": water_level,
            "date_prod": (obs_date + timedelta(days=1)).isoformat(),
            "code_statut": "1",
            "libelle_statut": random.choice(validation_statuses),
            "code_methode": "1",
            "libelle_methode": random.choice(methods),
            "code_qualification": "1",
            "libelle_qualification": random.choice(qualities),
            "longitude": 2.3522 + random.uniform(-0.01, 0.01),
            "latitude": 48.8566 + random.uniform(-0.01, 0.01),
            "grandeur_hydro_elab": "mock_HIXnJ",
        }
        records.append(record)

    return records
</code></pre>
<h3 id="heading-fetch-data-for-real">Fetch Data for Real</h3>
<h4 id="heading-design-logic">Design logic</h4>
<p>Two functions handle extraction. They're deliberately split according to the <a href="https://en.wikipedia.org/wiki/Single-responsibility_principle"><strong>Single Responsibility Principle</strong></a> (SRP): each function should have one reason to change.</p>
<pre><code class="language-text">fetch_all_data() (orchestrator)
    ├── fetch_single_station_data(station_1) ← handles all complexity
    ├── fetch_single_station_data(station_2) ← handles all complexity
    └── fetch_single_station_data(station_n) ← handles all complexity
</code></pre>
<ul>
<li><p><code>fetch_single_station_data()</code> owns <em>all</em> the messy per-station complexity: pagination, cursor advancement, stop conditions, and network error handling.</p>
</li>
<li><p><code>fetch_all_data()</code> owns none of that. It just loops over stations and delegates.</p>
</li>
</ul>
<p>This split has two payoffs. First, you can debug or swap out the pagination strategy for one station without touching the orchestration code at all. Second, if you ever want to parallelize fetching (with <code>concurrent.futures</code> or <code>asyncio</code>, for example), the orchestrator is the <em>only</em> place you'd need to touch.</p>
<h4 id="heading-code-level-walkthrough">Code-level walkthrough</h4>
<p><code>fetch_single_station_data()</code> fetches station data either from mock test data or from the real API, page by page, until it has everything it needs.</p>
<ul>
<li><p><strong>If</strong> <code>use_mock=True</code>, it uses <strong>fake data</strong> instead of calling the real API.</p>
<ol>
<li><p>It calls <code>generate_mock_api_data()</code></p>
</li>
<li><p>Turns the result into a DataFrame</p>
</li>
<li><p>Converts the date column into real pandas dates</p>
</li>
<li><p>Returns that DataFrame</p>
</li>
</ol>
</li>
<li><p><strong>If</strong> <code>use_mock=False</code>, it does the <strong>real API request</strong>:</p>
<ol>
<li><p>Creates a reusable HTTP session</p>
</li>
<li><p>Starts from <code>start_date</code></p>
</li>
<li><p>Repeatedly asks the API for a page of data</p>
</li>
<li><p>Stops when:</p>
<ul>
<li><p>the API returns no data</p>
</li>
<li><p>the latest date reaches yesterday</p>
</li>
<li><p>the page is smaller than expected</p>
</li>
<li><p>a network error happens</p>
</li>
</ul>
</li>
<li><p>Combines all pages into one DataFrame</p>
</li>
<li><p>Returns an empty DataFrame if nothing was fetched</p>
</li>
</ol>
</li>
</ul>
<pre><code class="language-python">def fetch_single_station_data(station_code: str, start_date: str, use_mock: bool = True) -&gt; pd.DataFrame:
    """Fetch all hydrometric data for a single station from mock API (default) or from the real endpoint.

    In production (cursor based pagination strategy):
    - Fetches max_per_page records per request
    - Continues until no new data or yesterday's date reached
    - Stop when: no data returned | last date &gt;= yesterday | page was not full
    - Handles network errors gracefully
    """
    if use_mock:
        data = generate_mock_api_data(station_code, start_date, num_days=7)
        page_df = pd.DataFrame(data)
        page_df["date_obs_elab"] = pd.to_datetime(
            page_df["date_obs_elab"], errors="coerce").dt.normalize()
        return page_df

    # Real implementation
    else:
        session = requests.Session()  # Reuse TCP connection across pages
        frames = []
        cursor = start_date

        while True:
            params = {
                "code_entite": station_code,
                "grandeur_hydro_elab": API_CONFIG.metric,
                "date_debut_obs_elab": cursor,
                "size": API_CONFIG.max_per_page,
            }

            try:
                response = session.get(
                    API_CONFIG.base_url,
                    params=params,
                    timeout=API_CONFIG.timeout_seconds  # Best practice, always set
                )
                response.raise_for_status()
            except requests.RequestException as e:  # Don't let one station kill the whole pipeline
                print(f"Error fetching data for station {station_code}: {e}")
                break

            data = response.json().get("data", [])
            if not data:  # Empty response: we've exhausted this station
                break

            page_df = pd.DataFrame(data)
            page_df["date_obs_elab"] = pd.to_datetime(
                page_df["date_obs_elab"], errors="coerce").dt.normalize()
            frames.append(page_df)

            last_page_date = page_df["date_obs_elab"].max()
            yesterday = date.today() - timedelta(days=1)

            # Prevent infinite loops
            if pd.isna(last_page_date) or last_page_date.date() &gt;= yesterday:
                break

            cursor = (last_page_date + pd.Timedelta(days=1)).strftime("%Y-%m-%d")

            if len(data) &lt; API_CONFIG.max_per_page:
                break

    if frames:
        return pd.concat(frames, ignore_index=True)
    return pd.DataFrame()
</code></pre>
<p><strong>How does the pagination loop actually work?</strong></p>
<p>Let's walk through it step by step. This is the densest bit of logic in the whole notebook.</p>
<ol>
<li><p>Send a request with <code>date_debut_obs_elab=cursor</code>: "give me records from this date on."</p>
</li>
<li><p>If the request fails outright (<code>requests.RequestException</code>), log it and <code>break</code>. <code>break</code> stops the loop right away and moves on. One station's network hiccup shouldn't kill the pipeline for every other station.</p>
</li>
<li><p>If the response has no data at all, you've caught up: <code>break</code>.</p>
</li>
<li><p>Otherwise, note the <em>latest</em> date seen on this page (<code>last_page_date</code>).</p>
</li>
<li><p>If that latest date is already <code>&gt;= yesterday</code>, you've caught up: <code>break</code>.</p>
</li>
<li><p>Otherwise, advance the cursor to <code>last_page_date + 1 day</code> and loop again for the next page.</p>
</li>
<li><p>As a safety net: if the page returned <em>fewer</em> records than <code>max_per_page</code>, that also means you've reached the end. The API wouldn't return a partial page unless it ran out of data, so: <code>break</code>.</p>
</li>
</ol>
<p>That last check (step 7) is a classic <strong>pagination termination heuristic</strong>. You don't always need a <code>next_page</code> token from the API. If a full page is <code>size=20000</code> and you get back only <code>4213</code> records, there's nothing left to fetch.</p>
<p><strong>Three specific, deliberate choices, called out:</strong></p>
<pre><code class="language-python">session = requests.Session()
</code></pre>
<p>A <a href="https://requests.readthedocs.io/en/latest/user/advanced/#session-objects"><code>Session</code></a> object reuses the underlying TCP connection across multiple requests to the same host. That avoids a fresh TCP/TLS handshake on every single page request. In short, it's faster for you and more polite to Hub'Eau's servers.</p>
<p><strong>Best practice:</strong> any time you call <code>requests.get()</code> more than once against the same host in a loop, reach for a <code>Session</code>.</p>
<pre><code class="language-python">except requests.RequestException as e:
</code></pre>
<p><code>RequestException</code> is the base class for <a href="https://requests.readthedocs.io/en/latest/api/#requests.RequestException">every exception</a> <code>requests</code> can raise: timeouts, connection errors, HTTP errors from <code>raise_for_status()</code> and more. Catching the base class here means <em>any</em> network hiccup gets handled the same forgiving way: log it, stop fetching this station, move on.</p>
<pre><code class="language-python">timeout=API_CONFIG.timeout_seconds
</code></pre>
<p>By default, <code>requests</code> calls <strong>never time out</strong>. Without an explicit timeout, a hung server can freeze your entire pipeline indefinitely. The <a href="https://requests.readthedocs.io/en/latest/user/advanced/#timeouts">requests advanced usage docs</a> states that requests to external servers should have a timeout attached.</p>
<p><strong>Best practice:</strong> wrap every external I/O call in <code>try/except</code>. Always fail gracefully. Log the error and let the pipeline recover or move on. This avoids one flaky request taking down an unattended weekly job.</p>
<p>Now the orchestrator, <code>fetch_all_data()</code>, is deliberately much simpler:</p>
<ol>
<li><p>Loop through all station codes.</p>
<ul>
<li><p>Fetch each station's data.</p>
</li>
<li><p>Keep the non-empty results.</p>
</li>
</ul>
</li>
<li><p>Combine them into one big DataFrame.</p>
</li>
</ol>
<pre><code class="language-python">def fetch_all_data(start_date: str, use_mock: bool = True) -&gt; pd.DataFrame:
    """Orchestrator: Fetch data for all configured stations."""
    frames = []

    for station_code in STATION_CONFIG.station_codes:
        print(f"Fetching data for station {station_code}...")
        df_station = fetch_single_station_data(station_code, start_date, use_mock=use_mock)

        if not df_station.empty:
            print(f"  Got {len(df_station)} records")
            frames.append(df_station)
        else:
            print(f"  (no data)")

    if frames:
        return pd.concat(frames, ignore_index=True)
    return pd.DataFrame()
</code></pre>
<p>That's it. A loop and a <code>pd.concat</code>. All the hard-won complexity lives one layer down, exactly where SRP says it should.</p>
<h2 id="heading-part-5-the-transform-step">Part 5: The Transform Step</h2>
<p>This is where raw, freshly-fetched data becomes something you can publish. Here's the design philosophy for this whole section: compose many small, <strong>pure functions</strong>. Each one takes a DataFrame in and returns a <em>new</em> DataFrame out without side effects. Avoid the one giant do-everything function trap.</p>
<pre><code class="language-text">    (EXTRACT)
    Raw API Data
    ↓
    (TRANSFORM)
1. Type parsing (datetime, numeric)
2. Column renaming (French → English)
3. Categorical mapping (validation status, quality)
4. Derived columns computation (flood alert flags)
5. Column reordering (logical grouping)
6. Sorting &amp; index reset
    ↓
    (LOAD)
    Publication-ready dataset
</code></pre>
<p>Why split this into six tiny steps instead of one big function? Each piece is testable and replaceable on its own. When something breaks at 3am on a scheduled run, every function is debuggable in isolation. You can pinpoint exactly which stage produced bad output. No need to pick apart one 200-line function.</p>
<h3 id="heading-type-parsing-and-graceful-coercion">Type Parsing and Graceful Coercion</h3>
<h4 id="heading-design-logic">Design logic</h4>
<p>Data from a CSV or a JSON API starts out, by default, as strings. Pandas needs real types to sort dates chronologically, do date arithmetic (<code>last_date + timedelta(days=1)</code>), or compare numeric values (<code>water_level &gt; 6000</code>). Type mismatches are a common error that break batch pipelines. One malformed row, like <code>"N/A"</code>, a truncated date, misaligned values, or stray characters and a strict parser throws an exception that kills the whole run.</p>
<p><strong>Best practice:</strong> both conversion functions below use <code>errors="coerce"</code>, so values pandas can't parse become <code>NaT</code> (Not a Time) or <code>NaN</code> (Not a Number) instead of raising an error. This is called <a href="https://stackoverflow.com/questions/36394814/what-is-the-significance-of-coerce-in-python-pandas">graceful coercion</a>.</p>
<h4 id="heading-code-level-walkthrough">Code-level walkthrough</h4>
<p><code>convert_to_date()</code> and <code>convert_to_numeric()</code> are small helper functions that make sure certain columns have the right type.</p>
<ul>
<li><p>Both start with <code>df.copy()</code> so they don’t change the original DataFrame.</p>
</li>
<li><p><code>errors="coerce"</code> means bad values become missing values instead of causing a crash.</p>
</li>
</ul>
<p>For <code>convert_to_date()</code>:</p>
<ol>
<li><p><code>df.copy()</code> makes a separate copy, so the original table stays unchanged</p>
</li>
<li><p>The loop goes through each column name in <code>columns</code></p>
<ul>
<li><p><code>if col in df.columns</code> checks that the column actually exists before trying to convert it</p>
</li>
<li><p><code>pd.to_datetime(...)</code> turns text like <code>"2026-07-12"</code> into real pandas date/time values</p>
</li>
<li><p><code>errors="coerce"</code> means invalid values become <code>NaT</code> (missing date) instead of raising an error</p>
</li>
<li><p><code>.dt.normalize()</code> removes the time part and keeps only the date at midnight</p>
</li>
</ul>
</li>
</ol>
<p>For <code>convert_to_numeric()</code>:</p>
<ol>
<li><p><code>df.copy()</code> is used here as well.</p>
</li>
<li><p>The loop goes through each column name in <code>columns</code></p>
<ul>
<li><p><code>if col in df.columns</code> checks that the column actually exists before trying to convert it</p>
</li>
<li><p>The loop calls <code>pd.to_numeric()</code> turns text like <code>"12.5"</code> into numbers</p>
</li>
<li><p><code>errors="coerce"</code> turns bad values into <code>NaN</code> instead of crashing</p>
</li>
</ul>
</li>
</ol>
<pre><code class="language-python">def convert_to_date(df: pd.DataFrame, columns: List[str]) -&gt; pd.DataFrame:
    """Convert specified columns to pandas datetime type."""
    df = df.copy()  # Never modify the original!
    for col in columns:
        if col in df.columns:
            df[col] = pd.to_datetime(df[col], errors="coerce").dt.normalize()
    return df


def convert_to_numeric(df: pd.DataFrame, columns: List[str]) -&gt; pd.DataFrame:
    """Convert specified columns to numeric (float) type."""
    df = df.copy()
    for col in columns:
        if col in df.columns:
            df[col] = pd.to_numeric(df[col], errors="coerce")
    return df
</code></pre>
<p>Why is this useful?</p>
<ul>
<li><p>You can aggregate the data: group it, summarize it and so on...</p>
</li>
<li><p>Dates become sortable and filterable as real dates.</p>
</li>
<li><p>Numbers work correctly in calculations like averages, sums, or comparisons.</p>
</li>
<li><p>It prevents bugs caused by mixed types, like <code>"12"</code> and <code>12</code>.</p>
</li>
</ul>
<p>Both <code>pd.to_datetime</code> and <code>pd.to_numeric</code> are official pandas functions with an <code>errors</code> parameter. By default, that parameter is set to <code>"raise"</code>, which throws on bad input. The other options are <code>"coerce"</code> (replace with null) or <code>"ignore"</code> (leave untouched). See the <a href="https://pandas.pydata.org/docs/reference/api/pandas.to_datetime.html">pandas <code>to_datetime</code> docs</a> and <a href="https://pandas.pydata.org/docs/reference/api/pandas.to_numeric.html"><code>to_numeric</code> docs</a> for the full parameter list.</p>
<p>Test it against an example of messy input:</p>
<pre><code class="language-python">messy_df = pd.DataFrame({
    "date_obs_elab": ["2026-01-15", "2026-01-16", "not a date", None],
    "resultat_obs_elab": [5800.0, "5900", "N/A", None],
})

type_safe_df = convert_to_date(messy_df, ["date_obs_elab"])
type_safe_df = convert_to_numeric(type_safe_df, ["resultat_obs_elab"])

# "not a date"  → NaT
# "N/A"         → NaN
# Pipeline continues safely — nothing crashed.
</code></pre>
<p><strong>Best practice:</strong> don't let one bad row kill an entire run. Coerce bad data to nulls rather than raising exceptions. Flag or log nulls separately if you need to investigate data quality later.</p>
<p>This is a deliberate trade-off. A choice between <em>availability</em> (the pipeline keeps running) over <em>strictness</em> (catching every bad row immediately). That's usually the right call for a scheduled, unattended job.</p>
<h4 id="heading-dfcopy-immutability-by-convention"><code>df.copy()</code>: immutability by convention</h4>
<p>Look again at the top of both functions above: <code>df = df.copy()</code>. This single line appears at the start of <strong>every transform function</strong> in the pipeline, and that's not an accident.</p>
<p>Python DataFrames are mutable objects, passed by reference. If a function modifies <code>df</code> in place without copying first, the caller's original DataFrame changes too. That's a classic <a href="https://en.wikipedia.org/wiki/Side_effect_(computer_science)">side effect</a> and it can produce genuinely confusing bugs. Call <code>.copy()</code> first means each function's output is a brand-new object. The input the caller passed in stays <em>guaranteed untouched</em>.</p>
<p><strong>Best practice:</strong> treat DataFrames as <strong>immutable inputs</strong>. Return a new DataFrame rather than modify it one in place. Even if it costs a small amount of memory or CPU, the debugging win is almost always worth it for a pipeline that isn't operating at extreme scale.</p>
<p>Finally, one auto-detecting convenience function wraps these two low-level functions. It scans text columns, guesses whether they contain dates or numbers, and calls the matching parsing function above.</p>
<p><code>auto_convert_columns()</code> tries to <strong>guess which columns are dates or numbers</strong> and then fixes these types automatically.</p>
<p>How it works:</p>
<p>It starts with two empty lists:</p>
<ul>
<li><p><code>datetime_cols</code> for date columns</p>
</li>
<li><p><code>numeric_cols</code> for number columns</p>
</li>
</ul>
<p>It goes through each column in the DataFrame. If a column is already a real datetime or numeric type, it skips it. If the column is text-like (<code>object</code> or <code>string</code>), it looks at up to 10 non-empty sample values.</p>
<p>It first tries to read those values as dates:</p>
<ul>
<li>if that works, the column is added to <code>datetime_cols</code></li>
</ul>
<p>If not, it tries to read them as numbers:</p>
<ul>
<li>if that works, the column is added to <code>numeric_cols</code></li>
</ul>
<p>At the end, it converts all date columns with <code>convert_to_date()</code>. Then it converts all numeric columns with <code>convert_to_numeric()</code></p>
<pre><code class="language-python">def auto_convert_columns(df: pd.DataFrame) -&gt; pd.DataFrame:
    """Auto-detect and convert datetime and numeric columns to the correct type."""
    datetime_cols = []
    numeric_cols = []

    for col in df.columns:
        if pd.api.types.is_datetime64_any_dtype(df[col]):
            continue
        if pd.api.types.is_numeric_dtype(df[col]):
            continue

        if pd.api.types.is_object_dtype(df[col]) or pd.api.types.is_string_dtype(df[col]):
            sample = df[col].dropna().head(10)
            if len(sample) == 0:
                continue

            try:
                pd.to_datetime(sample, errors='raise', format='mixed')
                datetime_cols.append(col)
                continue
            except (ValueError, TypeError):
                pass

            try:
                pd.to_numeric(sample, errors='raise')
                numeric_cols.append(col)
                continue
            except (ValueError, TypeError):
                pass

    df = convert_to_date(df, datetime_cols)
    df = convert_to_numeric(df, numeric_cols)
    return df
</code></pre>
<p>Notice the inner <code>try/except</code> blocks here use <code>errors='raise'</code>. That's the <em>opposite</em> of the coercion strategy above, but it only runs against a small <code>.head(10)</code> <strong>sample</strong> of each column.</p>
<p>This is a type-<em>sniffing</em> step, not the final conversion. It tests <em>"does this column look like dates, or numbers, or neither?"</em> on a cheap sample. Then, it hands off the actual, forgiving conversion of the <em>whole</em> column to <code>convert_to_date()</code> or <code>convert_to_numeric()</code>. Two different <code>errors</code> strategies, two different jobs.</p>
<h3 id="heading-schema-translation-with-bidirectional-mappings">Schema Translation with Bidirectional mappings</h3>
<h4 id="heading-design-logic">Design logic</h4>
<p>The Hub'Eau API returns French column names and French categorical values, like <code>code_station</code> or <code>"Donnée validée"</code>. A dataset meant for an international audience should ship in English. If you rename columns inline, wherever it's convenient, the mapping between French and English ends up scattered across the codebase. Then you'd have no way to reverse it if you ever needed to.</p>
<p><strong>The fix:</strong> define <strong>one authoritative mapping</strong> at the top of the module, and derive everything else from it.</p>
<h4 id="heading-code-level-walkthrough">Code-level walkthrough</h4>
<p><code>API_TO_EN</code> maps API field names to English translations.</p>
<pre><code class="language-python"># Primary mapping: French API columns to English column names
API_TO_EN = {
    "code_site": "location_code",
    "code_station": "station_code",
    "date_obs_elab": "record_date",
    "resultat_obs_elab": "water_level_mm",
    "date_prod": "data_production_date",
    "code_statut": "validation_status_code",
    "libelle_statut": "validation_status",
    "code_methode": "production_method_code",
    "libelle_methode": "production_method",
    "code_qualification": "quality_code",
    "libelle_qualification": "quality_assessment",
    "longitude": "longitude",
    "latitude": "latitude",
    "grandeur_hydro_elab": "hubeau_elab_code",
}

# Reverse mapping: English to French (computed automatically)
EN_TO_API = {v: k for k, v in API_TO_EN.items()}
</code></pre>
<p>The second line (<code>EN_TO_API</code>) is just a shortcut: it swaps each key and value from <code>API_TO_EN</code>. A <a href="https://docs.python.org/3/tutorial/datastructures.html#dictionaries">dict comprehension</a> builds it. <strong>Here's the important design point:</strong> <code>EN_TO_API</code> isn't hand-maintained, it's <em>derived</em>. If you add, remove, or rename an entry in <code>API_TO_EN</code>, <code>EN_TO_API</code> updates automatically the next time the module runs.</p>
<p>There's <em>exactly one place</em> in the entire codebase where a schema change needs to happen.</p>
<p>Categorical <em>values</em> (not just column names) get the same treatment:</p>
<pre><code class="language-python">CATEGORICAL_MAPPINGS = {
    "validation_status": {
        "Donnée validée": "validated",
        "Donnée brute": "raw",
        "Donnée pré-validée": "pre-validated",
    },
    "quality_assessment": {
        "Bonne": "good",
        "Non qualifiée": "unqualified",
        "Douteuse": "dubious",
    },
    "production_method": {
        "Calculée": "calculated",
        "Mesurée": "measured",
        "Expertisée": "expert-reviewed",
    },
}
</code></pre>
<p>The functions below apply these mappings, in order, to standardize column names and category values.</p>
<p><code>rename_to_english()</code>:</p>
<ol>
<li><p>If the DataFrame is empty, it returns a copy right away.</p>
</li>
<li><p>It builds a list of columns that are renamed from API names to English names.</p>
<ul>
<li><p>It only renames a column if:</p>
<ul>
<li><p>the old name exists, and</p>
</li>
<li><p>the new name does not already exist</p>
</li>
</ul>
</li>
</ul>
</li>
<li><p>Then it returns the renamed DataFrame.</p>
</li>
</ol>
<p><code>rename_to_api_schema()</code>:</p>
<ol>
<li><p>Also returns a copy for empty data.</p>
</li>
<li><p>Does the reverse: English names back to API names.</p>
</li>
<li><p>Returns the DataFrame.</p>
</li>
</ol>
<p>(Useful if you need to send data back in the API’s original format).</p>
<p><code>apply_categorical_mappings()</code>:</p>
<ol>
<li><p>Makes a copy so the original DataFrame is not changed.</p>
</li>
<li><p>For each column in <code>CATEGORICAL_MAPPINGS</code>, it replaces values using the mapping.</p>
<ul>
<li><p>Example: <code>"Donnée validée"</code> becomes <code>"validated"</code>.</p>
</li>
<li><p><code>.fillna(df[col_name])</code> keeps the original value if a value isn’t found in the mapping.</p>
</li>
</ul>
</li>
<li><p>Returns the DataFrame.</p>
</li>
</ol>
<pre><code class="language-python">def rename_to_english(df: pd.DataFrame) -&gt; pd.DataFrame:
    """Rename API column names to English schema names."""
    if df.empty:
        return df.copy()

    columns_to_rename = {}
    for src, dst in API_TO_EN.items():
        if src in df.columns and dst not in df.columns:
            columns_to_rename[src] = dst

    return df.rename(columns=columns_to_rename)

def rename_to_api_schema(df: pd.DataFrame) -&gt; pd.DataFrame:
    """Rename English column names back to API schema names (defensive/reverse operation)."""
    if df.empty:
        return df.copy()

    columns_to_rename = {k: v for k, v in EN_TO_API.items() if k in df.columns}
    return df.rename(columns=columns_to_rename)
</code></pre>
<pre><code class="language-python">def apply_categorical_mappings(df: pd.DataFrame) -&gt; pd.DataFrame:
    df = df.copy()
    for col_name, mapping in CATEGORICAL_MAPPINGS.items():
        if col_name in df.columns:
            df[col_name] = df[col_name].map(mapping).fillna(df[col_name])
    return df
</code></pre>
<p>Two defensive habits are worth a note here:</p>
<p><strong>Robustness to partial inputs:</strong> Both rename functions only rename columns that actually exist in the input (<code>if src in df.columns</code>).</p>
<p>A rename function that assumes every mapped column is always present will crash the moment it's called on a partial or differently-shaped DataFrame.</p>
<p><code>.map(mapping).fillna(df[col_name])</code>. <a href="https://pandas.pydata.org/docs/reference/api/pandas.Series.map.html"><code>Series.map</code></a> replaces every value it finds in the mapping dict and turns any value <em>not</em> found in the dict into <code>NaN</code>. Chain <code>.fillna(df[col_name])</code> right after restores the <em>original</em> value wherever the mapping didn't apply. That way, unexpected categorical values pass through unchanged instead of silently becoming null. It's a subtle but important robustness choice.</p>
<p>See this exact <code>.map()</code>-then-<code>.fillna()</code> idiom discussed on <a href="https://stackoverflow.com/questions/19798153/difference-between-map-applymap-and-apply-methods-in-pandas">Stack Overflow: pandas map vs apply performance</a>.</p>
<p>A quick round-trip test proves the bidirectional mapping actually works:</p>
<pre><code class="language-python">sample_api_df = pd.DataFrame({"code_station": ["F700000109"], ...})
renamed_df = rename_to_english(sample_api_df)          # French → English
reversed_df = rename_to_api_schema(renamed_df)          # English → French
# reversed_df.columns.tolist() == sample_api_df.columns.tolist()  → True
</code></pre>
<h3 id="heading-compute-flood-alerts">Compute Flood Alerts</h3>
<p><code>add_derived_columns()</code> is a one-liner. It just computes <code>flood_alert</code>. If the water level is greater than the flood threshold set in the <code>Config</code>, the result is <code>True</code>. Otherwise, the result is <code>False</code>.</p>
<pre><code class="language-python">def add_derived_columns(df: pd.DataFrame) -&gt; pd.DataFrame:
    """Add a computed columns based on raw data.
    """
    df = df.copy()

    if "water_level_mm" in df.columns:
        df["flood_alert"] = df["water_level_mm"] &gt; STATION_CONFIG.flood_threshold_mm

    return df
</code></pre>
<h3 id="heading-column-ordering">Column Ordering</h3>
<p>Here's a small but user-facing detail: define a preferred column order once, as data, and reuse it everywhere. <code>COLUMN_ORDER</code> holds that preferred column sequence.</p>
<p><code>order_columns()</code> rearranges a DataFrame so those columns come first, while any extra columns stay at the end.</p>
<pre><code class="language-python">COLUMN_ORDER = [
    # Primary identifiers &amp; measurements
    "station_code", "record_date", "water_level_mm", "flood_alert",
    # Metadata about the observation
    "hubeau_elab_code", "data_production_date",
    "validation_status_code", "validation_status",
    "production_method_code", "production_method",
    "quality_code", "quality_assessment",
    # Geographic info (less important)
    "location_code", "longitude", "latitude",
]

def order_columns(df: pd.DataFrame) -&gt; pd.DataFrame:
    """Reorder columns to preferred order."""
    present_cols = [c for c in COLUMN_ORDER if c in df.columns]
    other_cols = [c for c in df.columns if c not in present_cols]
    return df[present_cols + other_cols]
</code></pre>
<p><code>other_cols</code> acts as a safety net: any column not explicitly listed in <code>COLUMN_ORDER</code> still gets included at the end. They're not silently dropped.</p>
<p>Rules, in order of priority:</p>
<ul>
<li><p>Keep identifiers and key fields up front.</p>
</li>
<li><p>Put the most important, most frequently used, and most stable columns first.</p>
</li>
<li><p>Group related fields together, so the table reads naturally.</p>
</li>
<li><p>Push optional or rarely-used fields to the end.</p>
</li>
</ul>
<h3 id="heading-deduplication">Deduplication</h3>
<h4 id="heading-design-logic">Design logic</h4>
<p>Remove duplicates as you go. In an incremental pipeline, date ranges and records can overlap:</p>
<ul>
<li><p>The same day might get re-fetched, because API data arrives late or gets finalized later.</p>
</li>
<li><p>The same observation might appear on two different pages of a paginated response.</p>
</li>
</ul>
<p>Without deduplication, these scenarios let duplicate rows pile up in the dataset over time. This is also, recall from Part 1, exactly what makes the pipeline <strong>idempotent</strong>: run it once or run it five times and the resulting dataset stays identical.</p>
<p>The fix requires defining what makes a record <strong>unique</strong>. Define a composite key:</p>
<pre><code class="language-text">key = (station_code, observation_date, water_level_value)
</code></pre>
<p>Why this specific combination? Physically, one sensor (<code>station_code</code>) reports one day's (<code>observation_date</code>) daily-maximum reading (<code>water_level_mm</code>), and that reading should be unique.</p>
<ul>
<li><p>Records from different stations obviously aren't duplicates of each other.</p>
</li>
<li><p>Two readings on different days aren't duplicates.</p>
</li>
<li><p>A subtler point: if the <em>same</em> station reports the <em>same</em> day but with a <em>different</em> value, that counts as a distinct observation, for example a corrected or revised measurement, not a duplicate to silently discard.</p>
</li>
</ul>
<h4 id="heading-code-level-walkthrough">Code-level walkthrough</h4>
<p><code>create_dedup_key()</code> builds a unique text ID for each row by joining three pieces together with an underscore:</p>
<ul>
<li><p>station code</p>
</li>
<li><p>date, as YYYY-MM-DD</p>
</li>
<li><p>water level value</p>
</li>
</ul>
<p>Example key:</p>
<pre><code class="language-text">"F700000109_2024-01-15_5800.0"
</code></pre>
<pre><code class="language-python">def create_dedup_key(df: pd.DataFrame) -&gt; pd.Series:
    """Create unique deduplication key from station, day, and water level value.

    Key format: "station_code_YYYY-MM-DD_value"
    Example: "F700000109_2024-01-15_5800.0"
    """
    parts = []

    if "code_station" in df.columns:
        parts.append(df["code_station"].astype(str))

    if "date_obs_elab" in df.columns:
        parts.append(df["date_obs_elab"].dt.strftime("%Y-%m-%d"))

    if "resultat_obs_elab" in df.columns:
        parts.append(df["resultat_obs_elab"].astype(str))

    if not parts:
        return pd.Series(index=df.index, dtype="object")

    return pd.Series(
        ["_".join(row) for row in zip(*parts)],
        index=df.index
    )
</code></pre>
<p><strong>How the key-building works:</strong> <code>parts</code> ends up as a list of <a href="https://www.geeksforgeeks.org/pandas/python-pandas-series/">Series</a>, one per key component (station, date, value), each the same length as the DataFrame.</p>
<p>Read the last line from the inside out:</p>
<pre><code class="language-python">    return pd.Series(["_".join(row) for row in zip(*parts)], index=df.index)
</code></pre>
<p><code>zip(*parts)</code> transposes that list of columns into row-wise tuples. It yields <code>(station_1, date_1, value_1)</code>, then <code>(station_2, date_2, value_2)</code>, and so on. The <a href="https://www.geeksforgeeks.org/python/python-list-comprehension/">list comprehension</a> then joins each row-tuple with underscores into one string key per row.</p>
<p>This <em>"list of columns → zip → row tuples"</em> idiom is a common and efficient way to combine several Series into one derived Series, without writing <code>.apply(lambda row: ..., axis=1)</code>. Row-wise <code>.apply</code> is notoriously <a href="https://stackoverflow.com/questions/54432583/when-should-i-not-want-to-use-pandas-apply-in-my-code">slow in pandas</a> compared to <a href="https://www.datacamp.com/es/tutorial/pandas-iterate-over-rows">vectorized</a> string operations.</p>
<p>The actual deduplication happens in <code>remove_duplicates()</code>. It removes rows from <em>"new"</em> (freshly fetched data) that already exist in <em>"existing"</em> (historic data).</p>
<p>Step by step:</p>
<ol>
<li><p>If one table is empty, it just returns <code>new</code>.</p>
</li>
<li><p>It enforces types in both DataFrames with <code>auto_convert_columns()</code> first, so dates and numbers compare correctly.</p>
</li>
<li><p>It creates a deduplication key for each row in both tables with <code>create_dedup_key()</code>.</p>
</li>
<li><p>It checks which keys from the fetched data don't appear in the existing historic dataset, and keeps only truly new rows.</p>
</li>
<li><p>It returns the filtered result, keeping the original columns from <code>new</code>.</p>
</li>
</ol>
<pre><code class="language-python">def remove_duplicates(existing: pd.DataFrame, new: pd.DataFrame) -&gt; pd.DataFrame:
    """Remove rows from 'new' that already exist in 'existing'."""
    # Short-circuit: if either is empty, no work to do
    if existing.empty or new.empty:
        return new.copy()

    # Parse types on both sides for a fair comparison
    existing_std = auto_convert_columns(existing)
    new_std = auto_convert_columns(new)

    # Build the keys
    existing_keys = set(create_dedup_key(existing_std).dropna())
    new_keys = create_dedup_key(new_std)

    # Boolean mask: True where the new row is genuinely new
    mask = ~new_keys.isin(existing_keys)

    # Index back into the ORIGINAL (non-standardized) 'new' to preserve all columns
    result = new.iloc[new_keys[mask].index].copy()
    return result
</code></pre>
<p>Two performance and robustness details are worth to note:</p>
<p>First, <code>existing_keys</code> <strong>is a</strong> <code>set</code><strong>, not a</strong> <code>list</code><strong>.</strong> Testing membership (<code>in</code> / <code>.isin()</code>) against a Python <code>set</code> is <a href="https://robbell.io/2009/06/a-beginners-guide-to-big-o-notation"><strong>O(1)</strong></a> on average, because it uses a fast hash lookup to answer <em>is this item already here?</em> directly. Testing against a <code>list</code> is <strong>O(n)</strong>: it has to scan item by item and it gets slower as the existing dataset grows.</p>
<p>For a dataset with tens of thousands of rows, checked on every single pipeline run, that difference matters and that's a textbook example of choosing the right data structure for the job. See the general discussion of <a href="https://stackoverflow.com/questions/513882/python-list-vs-dict-for-look-up-table">list vs. set lookup performance in Python</a> and this <a href="https://thelinuxcode.com/pandas-value-list/">guidance</a> on <a href="https://stackoverflow.com/questions/61515457/fastest-way-to-filter-a-pandas-dataframe-using-a-list"><code>.isin()</code> performance for filtering</a>.</p>
<p><strong>Simple rule:</strong> use a <code>set</code> when you care about fast membership checks. Use a <code>list</code> when you care about order or duplicates.</p>
<p>Second, <code>new.iloc[new_keys[mask].index]</code> <strong>indexes back into the <em>original</em>, non-validated</strong> <code>new</code> <strong>DataFrame</strong>, not <code>new_std</code>.</p>
<p>Why? <code>auto_convert_columns()</code> only ran to get <em>consistent types for comparison</em>. The caller still wants the <em>original</em> raw values and schema back for everything that survives deduplication.</p>
<p>Don't let a side-computation <em>accidentally</em> become your source of truth. Always modify a copy only to decide what to keep or remove. Once you've made that decision, apply it to the original data. That way you preserve the real source values for later steps.</p>
<p>In short:</p>
<ol>
<li><p>First modifications are only for comparison.</p>
</li>
<li><p>Use that comparison to filter the original data.</p>
</li>
<li><p>Return the original rows unchanged, so later stages can process them.</p>
</li>
</ol>
<p><strong>Best practice:</strong> keep your deduplication key simple and stable (immutable) and always short-circuit (with <code>if existing.empty or new.empty: return new.copy()</code>) before doing any heavier work. This cheap guard clause skips the expensive deduplication logic when there's no data to process.</p>
<h3 id="heading-put-it-all-together-in-postprocess">Put it All Together in <code>postprocess()</code></h3>
<p>All the pieces above are small and testable on their own. The last step of <strong>Transform</strong> glues them together, <em>in order</em>, into the single pipeline function below:</p>
<pre><code class="language-python">def postprocess(df: pd.DataFrame) -&gt; pd.DataFrame:
    """Apply all post-processing transformations."""
    if df.empty:
        return df

    df = df.copy()

    print("  1. Converting types...")
    df = auto_convert_columns(df)

    print("  2. Renaming columns (French → English)...")
    df = rename_to_english(df)

    print("  3. Mapping categorical values...")
    df = apply_categorical_mappings(df)

    print("  4. Adding derived columns...")
    df = add_derived_columns(df)

    print("  5. Reordering columns...")
    df = order_columns(df)

    print("  6. Sorting and resetting index...")
    df = df.sort_values(["record_date", "station_code"]).reset_index(drop=True)

    return df
</code></pre>
<p>Notice the shape of this function: it's basically a linear script with six numbered, printed steps. Each one calls a previously-defined pure function. There's no new <em>logic</em> here, only <em>sequencing</em>. That's intentional.</p>
<p><strong>Best practice:</strong> log progress clearly at each stage. When a scheduled job fails at 3am, a clear step-by-step log is the difference between a two-minute diagnosis and an hour of guessing. A good option is to use a <a href="https://www.geeksforgeeks.org/python/logging-in-python/">logger</a>. Here, this tutorial sticks with classic console printing (<code>print(f" 1. Converting types...")</code>).</p>
<h2 id="heading-part-6-the-load-step">Part 6: The Load Step</h2>
<h3 id="heading-design-logic">Design Logic</h3>
<p><strong>Load</strong> is the final stage: take the processed data and move it to its destination. Typical destinations include data warehouses, data lakes, and databases.</p>
<p>Before publishing, the pipeline prepares two things: the output folder itself, and a metadata file that describes the dataset.</p>
<h4 id="heading-code-level-walkthrough">Code-level walkthrough</h4>
<p><code>create_output_dir()</code> makes sure an output folder exists, and creates one if it doesn't:</p>
<ul>
<li><p>If <code>use_mock</code> is <code>True</code>, it uses <code>KAGGLE_CONFIG.mock_output_dir</code>, the directory for fake data.</p>
</li>
<li><p>Otherwise, it uses the real output directory set in <code>KAGGLE_CONFIG.output_dir</code>.</p>
</li>
</ul>
<pre><code class="language-python">def create_output_dir(use_mock: bool = False) -&gt; None:
    """Create output directory (and parents) if it does not already exist."""
    output_dir = KAGGLE_CONFIG.mock_output_dir if use_mock else KAGGLE_CONFIG.output_dir
    output_dir.mkdir(parents=True, exist_ok=True)
    # exist_ok=True: idempotent, safe to call multiple times
</code></pre>
<p><code>exist_ok=True</code> is a small but important detail. Without it, <a href="https://docs.python.org/3/library/pathlib.html#pathlib.Path.mkdir"><code>Path.mkdir()</code></a> raises <code>FileExistsError</code> if the directory already exists, which it will on every run after the first.</p>
<p>Setting <code>exist_ok=True</code> makes directory creation <strong>idempotent</strong>: calling it 100 times has the same effect as calling it once. On the filesystem, this is the "safe to re-run" idea behind the deduplication logic from Part 5.</p>
<p>The Kaggle API follows the <a href="https://frictionlessdata.io/specs/data-package/">Data Package specification</a>. It <a href="https://github.com/Kaggle/kaggle-cli/wiki/Dataset-Metadata/bc2684f533cd40afae28210d8f6e62b88d793d62">requires</a> a descriptive <code>dataset-metadata.json</code> file alongside the CSV. This matters for the search and discoverability of the <a href="https://www.kaggle.com/datasets/grimespoint/paris-flood-dataset">Paris flood dataset</a>:</p>
<pre><code class="language-python">def create_metadata(df: pd.DataFrame, config: KaggleConfig) -&gt; Dict:
    """Generate Kaggle dataset metadata from DataFrame and config."""
    if df.empty or "record_date" not in df.columns:
        first_date = "unknown"
        last_date = "unknown"
    else:
        first_date = df["record_date"].min().strftime("%Y-%m-%d")
        last_date = df["record_date"].max().strftime("%Y-%m-%d")

    return {
        "title": config.title,
        "id": config.dataset_slug,
        "licenses": [{"name": config.license_name}],
        "keywords": config.keywords,
        "temporalCoverage": {"startDate": first_date, "endDate": last_date},
        "geospatialCoverage": config.geospatial_coverage,
        "updateFrequency": config.update_frequency,
    }
</code></pre>
<p>Metadata comes from the <code>KaggleConfig</code> dataclass. Note that <code>temporalCoverage</code> is computed <em>from the data itself</em> (<code>df["record_date"].min()</code>/<code>.max()</code>) rather than hardcoded. Every time the pipeline runs, the metadata's date range automatically reflects reality, with zero manual bookkeeping.</p>
<p>Finally, <code>publish_to_kaggle()</code> uploads the updated dataset to Kaggle. It calls out to the <a href="https://github.com/Kaggle/kaggle-api">Kaggle CLI</a> as an external command.</p>
<p>What it does:</p>
<ol>
<li><p>Gets the current time and turns it into a text label like <code>Weekly update: 2026-07-13 14:30:00</code></p>
</li>
<li><p>Builds a Kaggle command that says:</p>
<ul>
<li><p>publish a new dataset version</p>
</li>
<li><p>use files from <code>KAGGLE_CONFIG.output_dir</code></p>
</li>
<li><p>attach the message</p>
</li>
<li><p>zip the directory contents</p>
</li>
</ul>
</li>
<li><p>Prints the command so you can see what will run</p>
</li>
<li><p>Runs the command with <code>subprocess.run(...)</code></p>
</li>
</ol>
<p>If the upload works:</p>
<ul>
<li>it prints <code>Successfully published to Kaggle.</code></li>
</ul>
<p>If it fails:</p>
<ul>
<li>it prints the error and then raises the error again so the failure isn't hidden.</li>
</ul>
<pre><code class="language-python">def publish_to_kaggle() -&gt; None:
    """Publish the updated dataset to Kaggle using the Kaggle CLI."""
    timestamp = pd.Timestamp.now().strftime("%Y-%m-%d %H:%M:%S")
    message = f"Weekly update: {timestamp}"

    cmd = [
        "kaggle", "datasets", "version",
        "-p", str(KAGGLE_CONFIG.output_dir),
        "-m", message,
        "--dir-mode", "zip",
    ]

    print("Publishing to Kaggle...")
    print("Command:", " ".join(cmd))

    try:
        subprocess.run(cmd, check=True)
        print("Successfully published to Kaggle.")
    except subprocess.CalledProcessError as e:
        print(f"Error publishing to Kaggle: {e}")
        raise
</code></pre>
<p><code>publish_to_kaggle</code> calls the Kaggle CLI (Command Line Interface) through Python's <a href="https://docs.python.org/3/library/subprocess.html"><code>subprocess</code></a> module. This is the standard way to run an external command-line tool from a Python pipeline.</p>
<p>Two choices here are worth adopting as general habits, not just for Kaggle:</p>
<ul>
<li><p><code>cmd</code> <strong>is built as a</strong> <code>list</code><strong>, never as a concatenated string.</strong> Pass a list of arguments to <code>subprocess.run</code> skips invoking a shell entirely. That sidesteps <a href="https://portswigger.net/web-security/os-command-injection">shell-injection</a> risks and correctly handles arguments containing spaces or special characters. See the <a href="https://docs.python.org/3/library/subprocess.html#security-considerations">official <code>subprocess</code> security considerations</a>.</p>
</li>
<li><p><code>check=True</code> means that if the Kaggle CLI fails, by exiting with a non-zero status, <code>subprocess.run</code> raises <code>CalledProcessError</code> instead of silently returning. Without <code>check=True</code>, a failed publish would look exactly like a successful one to the rest of the code.</p>
</li>
</ul>
<h2 id="heading-part-7-assemble-the-full-pipeline">Part 7: Assemble the Full Pipeline</h2>
<h3 id="heading-the-global-rehearsal-mock-mode">The Global Rehearsal (Mock Mode)</h3>
<p>The whole pipeline is built and tested in isolation. It's time to wire the blocks into one linear function. First, run it entirely against mock data. Nothing gets published this way. For Demonstration purposes, a CSV filled with artificial data will land in a local <code>mock_output</code> folder.</p>
<pre><code class="language-python">def run_etl_pipeline(use_mock: bool = True) -&gt; pd.DataFrame:
    """Run the complete ETL pipeline: Extract → Transform → Load to CSV."""

    # STEP 1: LOAD EXISTING DATA
    if use_mock:
        loaded_df = pd.DataFrame(mock_api_response)
        loaded_df['date_obs_elab'] = pd.to_datetime(loaded_df['date_obs_elab'])
        loaded_df = rename_to_english(loaded_df)
    else:
        loaded_df = load_csv(KAGGLE_CONFIG.input_csv)

    # STEP 2: DETERMINE UPDATE RANGE
    should_update, start_date = determine_update_range(loaded_df)
    if not should_update:
        return loaded_df   # already current: nothing more to do

    # STEP 3: EXTRACT (FETCH DATA)
    fetched_data = fetch_all_data(start_date, use_mock=use_mock)

    # STEP 4: TRANSFORM I - DEDUPLICATE
    deduped_fetched_data = remove_duplicates(loaded_df, fetched_data)

    # STEP 5: TRANSFORM II - COMBINE (MERGE WITH EXISTING)
    new_df_english = rename_to_english(deduped_fetched_data)
    merged_historical_and_new = pd.concat([loaded_df, new_df_english], ignore_index=True)

    # STEP 6: TRANSFORM III - POST-PROCESS
    processed_records = postprocess(merged_historical_and_new)

    # STEP 7: EXPORT (LOAD TO CSV)
    create_output_dir(use_mock=use_mock)
    output_path = KAGGLE_CONFIG.mock_output_filename if use_mock else KAGGLE_CONFIG.output_csv_path
    processed_records.to_csv(output_path, index=False, sep=",")

    return processed_records
</code></pre>
<p>Every one of the seven steps above corresponds to a function you already built, and tested earlier in this tutorial. <strong>To Assemble them is almost mechanical.</strong> That's always the payoff of composing small, single-responsibility functions:</p>
<pre><code class="language-python">loaded_df                 = load_csv(...)                          # 1. load existing data
should_update, start_date = determine_update_range(...)            # 2. check what's needed
fetched_data               = fetch_all_data(start_date)             # 3. fetch new records
deduped_fetched_data       = remove_duplicates(existing, new_raw)   # 4. deduplicate
merged_historical_and_new  = pd.concat([existing, new_clean])       # 5. merge
processed_records          = postprocess(merged_historical_and_new) # 6. postprocess
processed_records.to_csv(...)                                       # 7. save
write_metadata() + publish_to_kaggle()                               # 8. publish
</code></pre>
<p>Each line's intent is obvious just from reading it left to right. That is exactly the point of good decomposition.</p>
<h3 id="heading-main-pipeline-orchestration"><code>main()</code>: Pipeline Orchestration</h3>
<p><code>main()</code> is the <a href="https://docs.python.org/en/3/library/__main__.html">conductor</a>. It wires every previously-built component together in the right order, just like <code>postprocess()</code> did one level down.</p>
<pre><code class="language-python">def main() -&gt; None:
    """Execute the full Paris Flood Monitoring ETL pipeline.

    EXTRACT
    1. Load the existing dataset from the Kaggle input mount
    2. Determine whether an update is needed (and from what date)
    3. Fetch new data from the Hub'Eau API

    TRANSFORM
    4. Deduplicate against the existing dataset
    5. Combine and post-process (type parsing, translation, derived cols...)

    LOAD
    6. Write the updated CSV and metadata file
    7. Publish the new dataset version to Kaggle

    Exits early if the dataset is already up to date.
    """
    final_dataset = run_etl_pipeline(use_mock=False)
    final_dataset.to_csv(KAGGLE_CONFIG.output_csv_path, index=False)
    write_metadata(final_dataset)

    print("\n[FINAL STEP] Publishing to Kaggle...")
    publish_to_kaggle()

    print("Running post-run validation")
    validate_and_analyze(final_dataset)


if __name__ == "__main__":
    main()
</code></pre>
<h3 id="heading-the-if-name-main-guard">The <code>if __name__ == "__main__":</code> Guard</h3>
<pre><code class="language-python">if __name__ == "__main__":
    main()
</code></pre>
<p>This is one of the most common idioms in Python, and it's worth understanding exactly what it does. Per the <a href="https://docs.python.org/3/library/__main__.html">official Python documentation</a> on <code>__main__</code>:</p>
<ul>
<li><p>Run the file directly (<code>python script.py</code>)</p>
<ul>
<li><p>→ the special variable <code>__name__</code> is set to <code>"__main__"</code></p>
</li>
<li><p>→ the condition is <code>True</code></p>
</li>
<li><p>→ <code>main()</code> executes.</p>
</li>
</ul>
</li>
<li><p><strong>Import</strong> the file as a module elsewhere (<code>import script</code>)</p>
<ul>
<li><p>→ <code>__name__</code> is set to the module's name instead</p>
</li>
<li><p>→ the condition is <code>False</code></p>
</li>
<li><p>→ <code>main()</code> does <strong>not</strong> run automatically.</p>
</li>
</ul>
</li>
</ul>
<p><strong>Best practice:</strong> always guard your entry point this way. It makes the module safely <strong>importable</strong> for testing individual functions, or for reuse in another script, without triggering the full pipeline (including a real publish to Kaggle!) just by importing it.</p>
<h2 id="heading-part-8-go-live-and-switch-to-the-real-api">Part 8: Go Live and Switch to the Real API</h2>
<p>Everything above runs safely against mock data. To point the pipeline at the real Hub'Eau API instead:</p>
<ol>
<li><p>Note the <code>use_mock=False</code> setting above. It threads through <code>APIConfig.use_mock</code>, and the calls to <code>run_etl_pipeline(use_mock=False)</code> and <code>main()</code>.</p>
</li>
<li><p>Make sure <code>KAGGLE_CONFIG.dataset_slug</code> and <code>input_csv</code> point at <strong>your own</strong> Kaggle dataset copy before you use it. You can only publish new versions of a dataset you own. Fork both the <a href="https://www.kaggle.com/code/grimespoint/data-engineering-with-python-etl-pipeline">notebook</a> and the <a href="https://www.kaggle.com/datasets/grimespoint/paris-flood-dataset">dataset</a>, then update the slug in the <a href="#heading-part-3-manage-configuration-with-dataclasses"><code>KaggleConfig</code></a> section.</p>
</li>
<li><p>Install and authenticate the <a href="https://github.com/Kaggle/kaggle-api">Kaggle CLI</a> if you plan to call <code>publish_to_kaggle()</code> outside of a Google Colab or Kaggle notebook.</p>
</li>
</ol>
<p>Everything else needs <strong>zero changes</strong>.</p>
<h3 id="heading-post-run-validation">Post-Run Validation</h3>
<p><code>validate_and_analyze()</code> closes the loop, and it's more than just a nice-to-have. It's a welcome sanity check that runs <em>after</em> the pipeline finishes. The output is a human-readable report covering shape, data types, null counts, summary statistics on <code>water_level_mm</code>, how many flood-alert records turned up, the temporal coverage and per-station record counts.</p>
<p>It doesn't change any data. It exists so that whoever, or whatever monitoring system, reads the run's log output can immediately see whether this week's numbers look sane. No need to analyze the CSV by hand. Try it!</p>
<h2 id="heading-summary">Summary</h2>
<p>You've just built a <em>complete</em> ETL pipeline that you can run again and again without breaking anything. The skeleton is here, and that same pattern repeats for almost any scheduled data job: swap out the API, the field mapping, and the destination.</p>
<p>For more tutorials like this one, check out my <a href="https://github.com/hyperphantasia">GitHub</a> or <a href="https://kaggle.com/grimespoint">Kaggle profile</a>.</p>
<p>Thanks for reading!</p>
<h2 id="heading-references">References</h2>
<ul>
<li><p><a href="https://hubeau.eaufrance.fr/">Hub'Eau API</a>: France's official open water-data platform</p>
</li>
<li><p>The <a href="https://www.kaggle.com/code/grimespoint/paris-flood-dataset-weekly-updater">updater <code>.py</code> script</a> runs live once a week and updates the original <a href="https://www.kaggle.com/datasets/grimespoint/paris-flood-dataset">dataset</a>.</p>
</li>
<li><p>The source dataset generator is available on <a href="https://github.com/hyperphantasia/paris-flood-dataset">GitHub</a>.</p>
</li>
<li><p>The complete <a href="https://www.kaggle.com/code/grimespoint/data-engineering-with-python-etl-pipeline">Jupyter notebook</a> to follow this tutorial and code all along (<a href="https://github.com/hyperphantasia/kaggle/blob/main/notebook_archive/data-engineering-with-python-etl-pipeline.ipynb">backup available</a>).</p>
</li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Build a Browser-Based PDF Blur Tool Using JavaScript ]]>
                </title>
                <description>
                    <![CDATA[ Many PDF documents contain information that shouldn't be shared publicly. Personal details, financial figures, signatures, addresses, account numbers, employee information, or confidential business da ]]>
                </description>
                <link>https://www.freecodecamp.org/news/build-pdf-blur-tool-javascript/</link>
                <guid isPermaLink="false">6a63cec8c741f882378e9b06</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ pdf ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Online PDF Tools ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Tutorial ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Bhavin Sheth ]]>
                </dc:creator>
                <pubDate>Fri, 24 Jul 2026 20:44:56 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/uploads/covers/5e1e335a7a1d3fcc59028c64/ca664176-3589-4e16-b95e-506fa53bcfce.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Many PDF documents contain information that shouldn't be shared publicly. Personal details, financial figures, signatures, addresses, account numbers, employee information, or confidential business data often need to be hidden before a file is sent to someone else.</p>
<p>A PDF Blur Tool makes this process simple. Instead of permanently removing content, it places a blur effect over selected areas of a PDF so sensitive information becomes difficult to read while the rest of the document remains unchanged.</p>
<p>In this tutorial, you'll build a browser-based PDF Blur Tool using JavaScript. Users will be able to upload a PDF, preview every page, draw blur boxes over sensitive content, adjust the blur intensity, apply the blur to selected pages, preview the final result, and download the processed PDF, all without uploading files to a server.</p>
<p>We'll use PDF.js to render PDF pages inside the browser, HTML Canvas to create and manage blur regions, and PDF-lib to generate the final blurred PDF.</p>
<p>By the end of this tutorial, you'll have a fully functional client-side PDF editing tool similar to the one available on my site, AllInOneTools.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/76925a1f-2b94-4b5b-923c-0252d1703b22.png" alt="allinonetools - pdf tools- blur pdf documents" style="display:block;margin:0 auto" width="905" height="282" loading="lazy">

<h2 id="heading-table-of-contents">Table of Contents</h2>
<ul>
<li><p><a href="#heading-what-this-pdf-blur-tool-does-and-how-it-works">What This PDF Blur Tool Does and How It Works</a></p>
</li>
<li><p><a href="#heading-project-setup">Project Setup</a></p>
</li>
<li><p><a href="#heading-creating-the-html-layout">Creating the HTML Layout</a></p>
</li>
<li><p><a href="#heading-uploading-and-previewing-pdfs">Uploading and Previewing PDFs</a></p>
</li>
<li><p><a href="#heading-creating-blur-regions">Creating Blur Regions</a></p>
</li>
<li><p><a href="#heading-applying-blur-to-pages">Applying Blur to Pages</a></p>
</li>
<li><p><a href="#heading-generating-the-final-pdf">Generating the Final PDF</a></p>
</li>
<li><p><a href="#heading-previewing-the-result">Previewing the Result</a></p>
</li>
<li><p><a href="#heading-renaming-and-downloading">Renaming and Downloading</a></p>
</li>
<li><p><a href="#heading-demo-how-the-pdf-blur-tool-works">Demo: How the PDF Blur Tool Works</a></p>
</li>
<li><p><a href="#heading-performance-tips">Performance Tips</a></p>
</li>
<li><p><a href="#heading-common-mistakes">Common Mistakes</a></p>
</li>
<li><p><a href="#heading-conclusion">Conclusion</a></p>
</li>
</ul>
<h2 id="heading-what-this-pdf-blur-tool-does-and-how-it-works">What This PDF Blur Tool Does and How It Works</h2>
<p>A PDF Blur Tool helps protect sensitive information before a document is shared. Instead of editing or deleting the original content, it applies a visual blur effect over selected areas so confidential information becomes unreadable while the rest of the document remains unchanged.</p>
<p>This approach is useful for hiding personal details, financial information, account numbers, signatures, addresses, faces, or any other private content that shouldn't be visible in the final document.</p>
<p>In this project, users can upload a PDF directly from their browser, preview every page, and draw one or more blur regions over the areas they want to hide. The tool also allows users to adjust the blur intensity, blur either selected areas or entire pages, apply the effect to the current page, all pages, or specific page ranges, preview the completed document, rename the output file, and download the final PDF.</p>
<p>Because everything runs inside the browser, no files are uploaded to a server, helping maintain document privacy.</p>
<p>Behind the scenes, the application first renders each PDF page onto an HTML canvas using PDF.js. Rather than modifying the original PDF immediately, it records the position, size, page number, and blur intensity for every blur region that the user creates.</p>
<p>When the user clicks Apply &amp; Finalize, those stored regions are converted from browser coordinates into actual PDF page coordinates. The selected blur effect is then applied to the rendered page, and PDF-lib generates a new PDF containing the blurred content while preserving the rest of the document.</p>
<p>This workflow provides an interactive editing experience while keeping the original PDF unchanged until the final document is generated.</p>
<p>For example, each blur region can be represented as an object like this:</p>
<pre><code class="language-javascript">const blurRegion = {

    page: 2,

    x: 180,

    y: 240,

    width: 260,

    height: 90,

    intensity: 6

};
</code></pre>
<p>Each object stores all the information required to recreate the blur effect during the final PDF generation process.</p>
<h2 id="heading-project-setup">Project Setup</h2>
<p>Before writing any code, let's create a simple project structure for our PDF Blur Tool.</p>
<p>We'll use plain HTML, CSS, and JavaScript, along with two libraries:</p>
<ul>
<li><p><strong>PDF.js</strong> for rendering PDF pages inside the browser.</p>
</li>
<li><p><strong>PDF-lib</strong> for generating the final blurred PDF.</p>
</li>
</ul>
<p>Our project structure looks like this:</p>
<pre><code class="language-text">pdf-blur-tool/

│── index.html
│── style.css
│── script.js
│── pdf.worker.min.js
│── assets/
</code></pre>
<p>Keeping the project simple makes it easier to understand how each part works.</p>
<p>Add PDF.js and PDF-lib before loading your own JavaScript.</p>
<pre><code class="language-html">&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/4.4.168/pdf.min.js"&gt;&lt;/script&gt;

&lt;script src="https://unpkg.com/pdf-lib/dist/pdf-lib.min.js"&gt;&lt;/script&gt;

&lt;script src="script.js"&gt;&lt;/script&gt;
</code></pre>
<p>Configure the PDF worker.</p>
<pre><code class="language-javascript">pdfjsLib.GlobalWorkerOptions.workerSrc =
    "pdf.worker.min.js";
</code></pre>
<p>The worker processes PDF rendering in a background thread, helping keep the interface responsive while pages are rendered.</p>
<h2 id="heading-creating-the-html-layout">Creating the HTML Layout</h2>
<p>The application consists of four main sections:</p>
<ul>
<li><p>Upload area</p>
</li>
<li><p>PDF preview</p>
</li>
<li><p>Blur settings panel</p>
</li>
<li><p>Final download section</p>
</li>
</ul>
<p>Create the basic layout:</p>
<pre><code class="language-html">&lt;div id="uploadSection"&gt;&lt;/div&gt;

&lt;div id="editorSection" hidden&gt;

    &lt;div id="pdfPreview"&gt;&lt;/div&gt;

    &lt;aside id="blurSettings"&gt;&lt;/aside&gt;

&lt;/div&gt;

&lt;div id="resultSection" hidden&gt;&lt;/div&gt;
</code></pre>
<p>Initially, only the upload section is visible.</p>
<p>After a PDF is selected, the editor becomes visible.</p>
<h3 id="heading-selecting-dom-elements">Selecting DOM Elements</h3>
<p>Create references to the elements used throughout the application.</p>
<pre><code class="language-javascript">const uploadSection =
    document.getElementById(
        "uploadSection"
    );

const editorSection =
    document.getElementById(
        "editorSection"
    );

const resultSection =
    document.getElementById(
        "resultSection"
    );

const fileInput =
    document.getElementById(
        "pdfInput"
    );

const pdfCanvas =
    document.getElementById(
        "pdfCanvas"
    );

const canvasContext =
    pdfCanvas.getContext("2d");
</code></pre>
<p>These references allow the application to switch between the upload, editing, and download stages.</p>
<h2 id="heading-uploading-and-previewing-pdfs">Uploading and Previewing PDFs</h2>
<p>The upload section accepts both drag-and-drop and traditional file selection.</p>
<p>When a PDF is chosen, verify that it's actually a PDF before continuing.</p>
<pre><code class="language-javascript">async function handlePdfUpload(
    file
) {

    if (
        !file ||
        file.type !==
        "application/pdf"
    ) {

        alert(
            "Please select a PDF file."
        );

        return;

    }

    await loadPdf(file);

}
</code></pre>
<p>If validation succeeds, the document is loaded into memory.</p>
<h3 id="heading-reading-the-pdf">Reading the PDF</h3>
<p>Use the File API to convert the uploaded file into an ArrayBuffer.</p>
<pre><code class="language-javascript">async function loadPdf(
    file
) {

    const bytes =
        await file.arrayBuffer();

    pdfDocument =
        await pdfjsLib
            .getDocument({
                data: bytes
            })
            .promise;

    currentPage = 1;

    await renderPage(
        currentPage
    );

}
</code></pre>
<p>The uploaded bytes will also be reused later when generating the blurred PDF.</p>
<h3 id="heading-rendering-the-first-page">Rendering the First Page</h3>
<p>PDF.js renders each page onto an HTML canvas.</p>
<p>Start by retrieving the requested page.</p>
<pre><code class="language-javascript">async function renderPage(
    pageNumber
) {

    const page =
        await pdfDocument
            .getPage(
                pageNumber
            );

    const viewport =
        page.getViewport({
            scale: 1.5
        });
</code></pre>
<p>Resize the canvas to match the page dimensions.</p>
<pre><code class="language-javascript">pdfCanvas.width =
    viewport.width;

pdfCanvas.height =
    viewport.height;
</code></pre>
<p>Render the page.</p>
<pre><code class="language-javascript">await page.render({

    canvasContext,

    viewport

}).promise;
</code></pre>
<p>After rendering finishes, the PDF page becomes visible inside the editor.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/0dbda32b-6bfa-4403-97bd-288dfe808239.png" alt=" Uploaded PDF displayed in the preview area with page navigation controls." style="display:block;margin:0 auto" width="1256" height="515" loading="lazy">

<h3 id="heading-creating-page-navigation">Creating Page Navigation</h3>
<p>Most PDF documents contain multiple pages.</p>
<p>Allow users to move between pages using Previous and Next buttons.</p>
<pre><code class="language-javascript">let currentPage = 1;

let pdfDocument = null;
</code></pre>
<p>Move to the previous page.</p>
<pre><code class="language-javascript">previousButton
.addEventListener(
    "click",
    async () =&gt; {

        if (
            currentPage === 1
        ) {

            return;

        }

        currentPage--;

        await renderPage(
            currentPage
        );

    }
);
</code></pre>
<p>Move to the next page.</p>
<pre><code class="language-javascript">nextButton
.addEventListener(
    "click",
    async () =&gt; {

        if (
            currentPage ===
            pdfDocument.numPages
        ) {

            return;

        }

        currentPage++;

        await renderPage(
            currentPage
        );

    }
);
</code></pre>
<p>Update the page counter whenever the current page changes.</p>
<pre><code class="language-javascript">pageIndicator.textContent =
    `Page ${currentPage} of ${pdfDocument.numPages}`;
</code></pre>
<p>This provides users with clear feedback while navigating large PDF documents.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/c99410bc-7d7c-411f-a8f1-8fd9b1aaf3b8.png" alt=" PDF preview with Previous and Next buttons for navigating between pages." style="display:block;margin:0 auto" width="546" height="818" loading="lazy">

<h3 id="heading-preparing-for-blur-editing">Preparing for Blur Editing</h3>
<p>Once the current page is rendered, the application prepares a transparent layer above the PDF canvas.</p>
<p>This overlay captures mouse interactions without modifying the original page preview.</p>
<p>Create the overlay.</p>
<pre><code class="language-html">&lt;canvas
    id="overlayCanvas"&gt;
&lt;/canvas&gt;
</code></pre>
<p>Match its size to the PDF preview.</p>
<pre><code class="language-javascript">overlayCanvas.width =
    pdfCanvas.width;

overlayCanvas.height =
    pdfCanvas.height;
</code></pre>
<p>Later in the tutorial, this overlay will allow users to draw blur regions while keeping the underlying PDF page untouched.</p>
<h3 id="heading-showing-the-editor">Showing the Editor</h3>
<p>After the first page finishes rendering, switch from the upload screen to the editor interface.</p>
<pre><code class="language-javascript">uploadSection.hidden =
    true;

editorSection.hidden =
    false;
</code></pre>
<p>Users can now preview the document, navigate between pages, and begin selecting areas that should be blurred.</p>
<h2 id="heading-creating-blur-regions">Creating Blur Regions</h2>
<p>Now that the PDF preview is working, we can build the most important feature of the application: allowing users to blur sensitive information.</p>
<p>Instead of editing the PDF immediately, users first draw one or more blur regions over the page preview.</p>
<p>Each region stores its own position, size, and blur intensity. These regions are later converted into actual PDF coordinates during final processing.</p>
<h3 id="heading-creating-the-blur-area-object">Creating the Blur Area Object</h3>
<p>Every blur region is represented as a JavaScript object.</p>
<p>For example:</p>
<pre><code class="language-javascript">const blurArea = {

    page: currentPage,

    x: 0,

    y: 0,

    width: 0,

    height: 0,

    intensity: 6

};
</code></pre>
<p>Rather than modifying the PDF immediately, the application simply keeps track of these objects until the user clicks <strong>Apply &amp; Finalize</strong>.</p>
<h3 id="heading-storing-multiple-blur-regions">Storing Multiple Blur Regions</h3>
<p>Users often need to hide more than one piece of information.</p>
<p>Store all blur areas inside an array.</p>
<pre><code class="language-javascript">const blurAreas = [];
</code></pre>
<p>Whenever a new blur box is created, push it into the array.</p>
<pre><code class="language-javascript">blurAreas.push({

    page: currentPage,

    x,

    y,

    width,

    height,

    intensity:
        blurIntensity

});
</code></pre>
<p>This makes it easy to redraw, edit, or remove individual blur regions later.</p>
<h3 id="heading-starting-a-blur-selection">Starting a Blur Selection</h3>
<p>The transparent overlay canvas captures mouse interactions.</p>
<p>When the user presses the mouse button, record the starting position.</p>
<pre><code class="language-javascript">let isDrawing = false;

let startX = 0;

let startY = 0;

overlayCanvas
.addEventListener(
    "mousedown",
    event =&gt; {

        isDrawing = true;

        startX = event.offsetX;

        startY = event.offsetY;

    }
);
</code></pre>
<p>The blur rectangle begins at this point.</p>
<h3 id="heading-drawing-the-blur-rectangle">Drawing the Blur Rectangle</h3>
<p>As the mouse moves, update the rectangle dimensions.</p>
<pre><code class="language-javascript">overlayCanvas
.addEventListener(
    "mousemove",
    event =&gt; {

        if (
            !isDrawing
        ) {

            return;

        }

        drawPreviewBox(

            startX,

            startY,

            event.offsetX,

            event.offsetY

        );

    }
);
</code></pre>
<p>The preview updates continuously while the user drags the mouse.</p>
<h3 id="heading-finishing-the-selection">Finishing the Selection</h3>
<p>When the mouse button is released, save the completed blur region.</p>
<pre><code class="language-javascript">overlayCanvas
.addEventListener(
    "mouseup",
    event =&gt; {

        isDrawing = false;

        blurAreas.push({

            page:
                currentPage,

            x:
                startX,

            y:
                startY,

            width:
                event.offsetX -
                startX,

            height:
                event.offsetY -
                startY,

            intensity:
                blurIntensity

        });

        redrawBlurAreas();

        updateBlurList();

    }
);
</code></pre>
<p>Each blur region becomes part of the current editing session.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/31655d91-dcb6-412c-8762-c6aedc732b81.png" alt="User dragging a blur rectangle over sensitive information in the PDF preview." style="display:block;margin:0 auto" width="565" height="863" loading="lazy">

<h3 id="heading-drawing-existing-blur-areas">Drawing Existing Blur Areas</h3>
<p>Whenever the page changes or a blur region is added, redraw every blur box.</p>
<pre><code class="language-javascript">function redrawBlurAreas() {

    overlayContext.clearRect(

        0,

        0,

        overlayCanvas.width,

        overlayCanvas.height

    );

    blurAreas

        .filter(

            area =&gt;

                area.page ===
                currentPage

        )

        .forEach(

            drawBlurArea

        );

}
</code></pre>
<p>This ensures that previously created blur regions remain visible while editing.</p>
<h3 id="heading-displaying-blur-boxes">Displaying Blur Boxes</h3>
<p>Render every stored region with a dashed outline.</p>
<pre><code class="language-javascript">function drawBlurArea(
    area
) {

    overlayContext
        .setLineDash([6, 4]);

    overlayContext
        .strokeStyle =
        "#4f6cff";

    overlayContext
        .strokeRect(

            area.x,

            area.y,

            area.width,

            area.height

        );

}
</code></pre>
<p>The outline acts as a guide and doesn't become part of the final PDF.</p>
<h3 id="heading-blur-options">Blur Options</h3>
<p>Users can choose how the blur should be applied.</p>
<p>The tool supports two modes:</p>
<ul>
<li><p>Blur selected areas</p>
</li>
<li><p>Blur entire page(s)</p>
</li>
</ul>
<p>The selected option controls the editing behavior.</p>
<pre><code class="language-javascript">const blurMode =
document.querySelector(

    'input[name="blurMode"]:checked'

).value;
</code></pre>
<p>If <strong>Blur selected areas</strong> is chosen, users draw blur rectangles manually.</p>
<p>If <strong>Blur entire page(s)</strong> is selected, the application skips manual selection and prepares to blur the entire page during final processing.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/54004f00-c974-4d77-b385-fe3136acaf04.png" alt="Blur options showing choices for blurring selected regions or entire PDF pages." style="display:block;margin:0 auto" width="642" height="231" loading="lazy">

<h3 id="heading-adjusting-blur-intensity">Adjusting Blur Intensity</h3>
<p>Different documents require different levels of blur.</p>
<p>A slider lets users control the blur strength before applying the effect.</p>
<pre><code class="language-javascript">const blurSlider =
document.getElementById(
    "blurIntensity"
);

let blurIntensity = 6;

blurSlider
.addEventListener(
    "input",
    event =&gt; {

        blurIntensity =
        Number(
            event.target.value
        );

    }
);
</code></pre>
<p>The selected value is stored with every newly created blur region.</p>
<pre><code class="language-javascript">blurArea.intensity =
blurIntensity;
</code></pre>
<p>Higher values produce a stronger blur effect.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/097cba2c-a441-401c-9f1c-f8ce0b5a26a1.png" alt="Blur intensity slider allowing users to adjust the strength of the blur effect." style="display:block;margin:0 auto" width="861" height="131" loading="lazy">

<h2 id="heading-managing-multiple-blur-areas">Managing Multiple Blur Areas</h2>
<p>Many documents contain several pieces of confidential information.</p>
<p>Instead of limiting users to a single blur rectangle, the application displays every saved region.</p>
<p>For example:</p>
<pre><code class="language-text">Blur Area #1

Blur Area #2

Blur Area #3
</code></pre>
<p>Each entry includes a remove button.</p>
<pre><code class="language-javascript">function removeBlurArea(
    index
) {

    blurAreas.splice(
        index,
        1
    );

    redrawBlurAreas();

    updateBlurList();

}
</code></pre>
<p>This allows users to delete only the blur region they no longer need.</p>
<p>To remove all blur regions from the current page:</p>
<pre><code class="language-javascript">function clearCurrentPage() {

    const remaining =

    blurAreas.filter(

        area =&gt;

            area.page !==
            currentPage

    );

    blurAreas.length = 0;

    blurAreas.push(
        ...remaining
    );

    redrawBlurAreas();

}
</code></pre>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/ef44e87e-7c39-4e2b-8e67-7e354853501d.png" alt="Blur area manager displaying multiple blur regions with delete controls and a Clear All on This Page button." style="display:block;margin:0 auto" width="876" height="273" loading="lazy">

<h2 id="heading-applying-blur-to-pages">Applying Blur to Pages</h2>
<p>Users may want to blur only one page or several pages within a document.</p>
<p>The editor provides three options:</p>
<ul>
<li><p>Current page only</p>
</li>
<li><p>All pages</p>
</li>
<li><p>Specific pages</p>
</li>
</ul>
<pre><code class="language-javascript">const pageOption =
document.querySelector(

'input[name="pageOption"]:checked'

).value;
</code></pre>
<p>If the user selects <strong>Specific pages</strong>, they can enter values such as:</p>
<pre><code class="language-text">1, 3-5, 8
</code></pre>
<p>These values will later be converted into an array of page numbers before the final PDF is generated.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/f3e1a72c-690d-4454-8b7a-50920817cd13.png" alt="Apply to Pages section with Current Page, All Pages, and Specific Pages options." style="display:block;margin:0 auto" width="566" height="292" loading="lazy">

<h3 id="heading-applying-the-blur-effect">Applying the Blur Effect</h3>
<p>So far, users have uploaded a PDF, selected one or more blur regions, adjusted the blur intensity, and chosen which pages should be processed.</p>
<p>The final step is converting those blur regions into actual blurred content inside the generated PDF.</p>
<p>Rather than modifying the original document directly, the application creates a new PDF while preserving the original file.</p>
<h3 id="heading-loading-the-original-pdf">Loading the Original PDF</h3>
<p>Start by loading the uploaded PDF into PDF-lib.</p>
<pre><code class="language-javascript">async function applyBlur() {

    const pdfDoc =

        await PDFLib.PDFDocument.load(
            originalPdfBytes.slice()
        );

    const pages =
        pdfDoc.getPages();

}
</code></pre>
<p>Using a copy of the original bytes ensures that the uploaded document remains unchanged.</p>
<h3 id="heading-processing-the-selected-pages">Processing the Selected Pages</h3>
<p>Determine which pages should receive the blur effect.</p>
<pre><code class="language-javascript">const selectedPages =

parsePageSelection(

    pageSelection,

    pdfDoc.getPageCount()

);
</code></pre>
<p>For example:</p>
<pre><code class="language-text">Current Page

↓

[2]


All Pages

↓

[1,2,3,4]


Specific Pages

↓

[1,3,5]
</code></pre>
<p>Only these pages will be modified during processing.</p>
<h3 id="heading-rendering-each-page-as-an-image">Rendering Each Page as an Image</h3>
<p>Since blur is a pixel-based effect, each selected PDF page is rendered into an off-screen canvas.</p>
<pre><code class="language-javascript">const page =

await pdfDocument.getPage(
    pageNumber
);

const viewport =
page.getViewport({

    scale: 2

});

const canvas =
document.createElement(
    "canvas"
);

canvas.width =
viewport.width;

canvas.height =
viewport.height;
</code></pre>
<p>Render the page.</p>
<pre><code class="language-javascript">await page.render({

    canvasContext:
    canvas.getContext("2d"),

    viewport

}).promise;
</code></pre>
<p>The canvas now contains a bitmap version of the PDF page that can be edited.</p>
<h3 id="heading-applying-blur-to-selected-regions">Applying Blur to Selected Regions</h3>
<p>Retrieve all blur regions that belong to the current page.</p>
<pre><code class="language-javascript">const pageRegions =

blurAreas.filter(

    area =&gt;

        area.page === pageNumber

);
</code></pre>
<p>Loop through every blur region.</p>
<pre><code class="language-javascript">pageRegions.forEach(

    area =&gt; {

        blurCanvasRegion(

            canvas,

            area

        );

    }

);
</code></pre>
<p>Each region is blurred independently.</p>
<h3 id="heading-blurring-the-canvas-region">Blurring the Canvas Region</h3>
<p>The browser's Canvas API allows filters to be applied while drawing.</p>
<p>Set the blur filter based on the selected intensity.</p>
<pre><code class="language-javascript">context.filter =

`blur(${area.intensity}px)`;
</code></pre>
<p>Redraw only the selected region.</p>
<pre><code class="language-javascript">context.drawImage(

    canvas,

    area.x,

    area.y,

    area.width,

    area.height,

    area.x,

    area.y,

    area.width,

    area.height

);
</code></pre>
<p>After drawing, reset the filter.</p>
<pre><code class="language-javascript">context.filter = "none";
</code></pre>
<p>Only the selected rectangle becomes blurred while the rest of the page remains unchanged.</p>
<h3 id="heading-blurring-an-entire-page">Blurring an Entire Page</h3>
<p>If the user chooses <strong>Blur entire page(s)</strong>, the process is much simpler.</p>
<p>Apply the filter to the full canvas.</p>
<pre><code class="language-javascript">context.filter =

`blur(${blurIntensity}px)`;

context.drawImage(

    canvas,

    0,

    0

);

context.filter =
"none";
</code></pre>
<p>The entire rendered page receives the selected blur effect.</p>
<h3 id="heading-converting-the-canvas-back-into-a-pdf-image">Converting the Canvas Back into a PDF Image</h3>
<p>After editing the canvas, convert it into an image.</p>
<pre><code class="language-javascript">const imageData =

canvas.toDataURL(
    "image/png"
);
</code></pre>
<p>Convert the image into bytes.</p>
<pre><code class="language-javascript">const bytes =

await fetch(imageData)

.then(

response =&gt;

response.arrayBuffer()

);
</code></pre>
<p>Embed the image inside PDF-lib.</p>
<pre><code class="language-javascript">const image =

await pdfDoc.embedPng(
    bytes
);
</code></pre>
<p>Replace the page contents.</p>
<pre><code class="language-javascript">const pdfPage =

pages[
pageNumber - 1
];

const size =
pdfPage.getSize();

pdfPage.drawImage(

    image,

    {

        x: 0,

        y: 0,

        width:
        size.width,

        height:
        size.height

    }

);
</code></pre>
<p>Repeat the same process for every selected page.</p>
<h3 id="heading-showing-the-processing-state">Showing the Processing State</h3>
<p>Generating large PDF files may take a few seconds.</p>
<p>Display a loading state while processing.</p>
<pre><code class="language-javascript">applyButton.disabled =
true;

applyButton.textContent =
"Applying...";
</code></pre>
<p>After processing finishes:</p>
<pre><code class="language-javascript">applyButton.disabled =
false;

applyButton.textContent =
"Apply &amp; Finalize";
</code></pre>
<p>This gives users clear feedback that the application is working.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/75e1e622-9769-4c17-8f39-49376de81041.png" alt="Apply &amp; Finalize button used to generate the blurred PDF." style="display:block;margin:0 auto" width="582" height="92" loading="lazy">

<p>During processing:</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/68ae927f-aa3f-4e8c-8b37-2ef89ba0ad40.png" alt="Applying state displayed while the PDF blur operation is being completed." style="display:block;margin:0 auto" width="285" height="113" loading="lazy">

<h2 id="heading-generating-the-final-pdf">Generating the Final PDF</h2>
<p>After every page has been processed, save the completed document.</p>
<pre><code class="language-javascript">const pdfBytes =

await pdfDoc.save();

const outputBlob =
new Blob(

[pdfBytes],

{

type:
"application/pdf"

}

);
</code></pre>
<p>Store the result so it can be previewed and downloaded later.</p>
<pre><code class="language-javascript">generatedPdfBlob =
outputBlob;
</code></pre>
<p>At this point, the blurred PDF has been successfully generated.</p>
<h2 id="heading-previewing-the-result">Previewing the Result</h2>
<p>Hide the editing interface and display the completed document.</p>
<pre><code class="language-javascript">editorSection.hidden =
true;

resultSection.hidden =
false;
</code></pre>
<p>The result screen displays:</p>
<ul>
<li><p>Final PDF preview</p>
</li>
<li><p>Editable filename</p>
</li>
<li><p>Total pages</p>
</li>
<li><p>File size</p>
</li>
<li><p>Download button</p>
</li>
</ul>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/1e96e1ec-b114-4ebb-955e-1e277d7f98da.png" alt="Final PDF preview showing multiple blurred regions with download options displayed beside the document." style="display:block;margin:0 auto" width="857" height="816" loading="lazy">

<p>The user can review the processed document before downloading it.</p>
<h2 id="heading-renaming-and-downloading">Renaming and Downloading</h2>
<p>Before downloading, users may want to rename the generated file.</p>
<p>Create a filename field.</p>
<pre><code class="language-html">&lt;input

type="text"

id="outputFilename"

value="blurred-document.pdf"&gt;
</code></pre>
<p>Validate the filename.</p>
<pre><code class="language-javascript">function getFilename() {

    let filename =
    outputFilename.value.trim();

    if (!filename) {

        filename =
        "blurred-document.pdf";

    }

    if (
        !filename
        .toLowerCase()
        .endsWith(".pdf")
    ) {

        filename += ".pdf";

    }

    return filename;

}
</code></pre>
<p>Display additional file information.</p>
<pre><code class="language-javascript">pageCount.textContent =

`Pages:
${finalPdfDocument.numPages}`;

fileSize.textContent =

formatFileSize(
generatedPdfBlob.size
);
</code></pre>
<p>Download the processed document.</p>
<pre><code class="language-javascript">downloadButton
.addEventListener(

"click",

() =&gt; {

    const url =

    URL.createObjectURL(
        generatedPdfBlob
    );

    const link =
    document.createElement(
        "a"
    );

    link.href = url;

    link.download =
    getFilename();

    link.click();

    URL.revokeObjectURL(
        url
    );

});
</code></pre>
<p>The browser downloads the completed PDF without sending any files to a remote server.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/1cd1538e-7a90-4994-bbd0-da84365e1d34.png" alt="Download section showing editable filename, page count, file size, and Download button." style="display:block;margin:0 auto" width="272" height="166" loading="lazy">

<p><img src="align=%22center%22" alt="align=%22center%22" width="600" height="400" loading="lazy"></p>
<h2 id="heading-demo-how-the-pdf-blur-tool-works">Demo: How the PDF Blur Tool Works</h2>
<p>Let's walk through the complete workflow.</p>
<h3 id="heading-step-1-upload-the-pdf">Step 1: Upload the PDF</h3>
<p>Users upload a PDF using drag-and-drop or the <strong>Select PDF</strong> button.</p>
<p>The browser validates the file and prepares it for rendering.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/9a08c61c-814a-4c44-9a74-6f4eef7ddefc.png" alt="Upload screen for selecting a PDF file." style="display:block;margin:0 auto" width="1256" height="515" loading="lazy">

<h3 id="heading-step-2-preview-the-document">Step 2: Preview the Document</h3>
<p>The uploaded PDF appears inside the preview window.</p>
<p>Users can move through the document using the page navigation controls.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/27024335-d786-476c-9c34-e050ca4162e7.png" alt="PDF preview with Previous and Next page navigation." style="display:block;margin:0 auto" width="546" height="818" loading="lazy">

<h3 id="heading-step-3-configure-blur-settings">Step 3: Configure Blur Settings</h3>
<p>Users choose whether to blur selected areas or entire pages.</p>
<p>They can also configure the blur intensity before creating any blur regions.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/6fdcce22-b42d-48ca-94cd-c99bc679b7f5.png" alt="Blur settings panel showing available blur options." style="display:block;margin:0 auto" width="400" height="758" loading="lazy">

<h3 id="heading-step-4-draw-blur-areas">Step 4: Draw Blur Areas</h3>
<p>Users click and drag directly on the PDF preview to create blur rectangles over sensitive content.</p>
<p>Multiple blur regions can be created on the same page.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/c5404708-118c-4f7f-a768-c20ea66626a7.png" alt="User creating blur rectangles over confidential information." style="display:block;margin:0 auto" width="565" height="863" loading="lazy">

<h3 id="heading-step-5-adjust-blur-intensity">Step 5: Adjust Blur Intensity</h3>
<p>The blur intensity slider controls how strong the blur effect should appear.</p>
<p>Higher values produce a stronger blur.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/b485fa69-8952-4e4a-b91c-945c9879838a.png" alt="blur seleted option" style="display:block;margin:0 auto" width="642" height="231" loading="lazy">

<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/d0ece3b0-2c75-45a2-9286-8a38a0ef706a.png" alt="Blur intensity slider controlling the strength of the blur effect." style="display:block;margin:0 auto" width="861" height="131" loading="lazy">

<h3 id="heading-step-6-manage-blur-regions">Step 6: Manage Blur Regions</h3>
<p>Individual blur areas can be removed, or all blur regions on the current page can be cleared.</p>
<p>This makes editing much easier before generating the final PDF.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/c90a4b21-9cfe-4c99-9b11-29b862621ca1.png" alt="Blur area management panel with multiple blur regions." style="display:block;margin:0 auto" width="876" height="273" loading="lazy">

<h3 id="heading-step-7-choose-the-pages">Step 7: Choose the Pages</h3>
<p>Users decide whether the blur should be applied to:</p>
<ul>
<li><p>Current page</p>
</li>
<li><p>All pages</p>
</li>
<li><p>Specific pages</p>
</li>
</ul>
<p>For example:</p>
<pre><code class="language-text">1,3-5,8
</code></pre>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/663facb2-f312-43a0-b4c8-4a029da0ed81.png" alt="Apply to Pages section with Current Page, All Pages, and Specific Pages options." style="display:block;margin:0 auto" width="566" height="292" loading="lazy">

<h3 id="heading-step-8-apply-the-blur">Step 8: Apply the Blur</h3>
<p>After reviewing the settings, users click <strong>Apply &amp; Finalize</strong>.</p>
<p>The application generates a new PDF containing the selected blur effects.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/53eb6910-25ec-4b66-aaf1-a0c11363a3f8.png" alt="Apply &amp; Finalize button generating the blurred PDF." style="display:block;margin:0 auto" width="582" height="92" loading="lazy">

<h3 id="heading-step-9-review-the-final-document">Step 9: Review the Final Document</h3>
<p>The completed PDF appears in the preview window.</p>
<p>Users can verify every blurred region before downloading.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/834a4f6d-e12a-45ae-b977-e761f624bb34.png" alt=" Final blurred PDF preview before downloading." style="display:block;margin:0 auto" width="857" height="816" loading="lazy">

<h3 id="heading-step-10-rename-and-download">Step 10: Rename and Download</h3>
<p>Finally, users rename the output file if needed and click <strong>Download</strong>.</p>
<p>The browser saves the completed PDF locally.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/573a6966-d0af-47af-a4d3-2c99b9a79622.png" alt=" Download section showing filename editing and Download button." style="display:block;margin:0 auto" width="272" height="166" loading="lazy">

<h2 id="heading-performance-tips">Performance Tips</h2>
<p>Large PDF files can take longer to render and process, but a few simple optimizations can keep the editor responsive.</p>
<p>Render only the page the user is currently viewing instead of loading the entire document.</p>
<pre><code class="language-javascript">await renderPage(
    currentPage
);
</code></pre>
<p>Reuse the same canvas and redraw only the blur regions when changes are made.</p>
<pre><code class="language-javascript">overlayContext.clearRect(
    0,
    0,
    overlayCanvas.width,
    overlayCanvas.height
);

redrawBlurAreas();
</code></pre>
<p>During final processing, generate only the pages selected by the user.</p>
<pre><code class="language-javascript">for (const page of selectedPages) {

    await processPage(page);

}
</code></pre>
<p>Finally, release temporary resources after the download completes.</p>
<pre><code class="language-javascript">URL.revokeObjectURL(
    downloadUrl
);
</code></pre>
<p>These optimizations reduce memory usage and help the PDF Blur Tool perform smoothly, even with large multi-page documents.</p>
<h2 id="heading-common-mistakes">Common Mistakes</h2>
<p>One common issue is storing blur coordinates before accounting for the current zoom level.</p>
<p>Always convert preview coordinates into the PDF's coordinate system before generating the final document.</p>
<pre><code class="language-javascript">const scaleX =

pdfWidth /
canvas.width;

const scaleY =

pdfHeight /
canvas.height;
</code></pre>
<p>Another mistake is allowing blur regions to extend beyond the page boundaries.</p>
<p>Clamp the values before processing.</p>
<pre><code class="language-javascript">blurArea.x = Math.max(
0,
blurArea.x
);

blurArea.y = Math.max(
0,
blurArea.y
);
</code></pre>
<p>Users should also verify the final preview before downloading, especially when multiple blur regions exist across different pages.</p>
<p>Finally, remember that this project applies a <strong>visual blur effect</strong> to the rendered PDF pages. If your application requires permanent removal of sensitive content rather than visual obscuring, additional document-redaction techniques are needed.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In this tutorial, you built a browser-based PDF Blur Tool using JavaScript.</p>
<p>You learned how to upload and preview PDF documents, navigate between pages, create and manage multiple blur regions, adjust blur intensity, apply blur to selected pages, generate a new PDF with PDF-lib, preview the processed document, and download the final file –&nbsp;all without uploading data to a server.</p>
<p>By combining PDF.js, the HTML Canvas API, and PDF-lib, you created a privacy-focused PDF editing tool that runs entirely inside the browser.</p>
<p>You can explore the complete workflow using the <a href="https://allinonetools.net/blur-pdf/">PDF Blur Tool</a>.</p>
<p>From here, you could extend the project with features such as movable and resizable blur regions, undo and redo support, reusable blur presets, keyboard shortcuts, touch-device editing, or additional annotation tools for even more advanced browser-based PDF editing.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Build an AI Agent with Function Calling in Node.js Using Google Gemini ]]>
                </title>
                <description>
                    <![CDATA[ Last year, a client asked me to add a conversational interface to their internal reporting tool. Staff would type a question, and the system would pull a live answer from the database. I had the first ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-build-ai-agent-function-calling-nodejs-gemini/</link>
                <guid isPermaLink="false">6a63af5c9a1ab0289b0cbb6a</guid>
                
                    <category>
                        <![CDATA[ Node.js ]]>
                    </category>
                
                    <category>
                        <![CDATA[ AI ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Tutorial ]]>
                    </category>
                
                    <category>
                        <![CDATA[ gemini ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Zia Ullah ]]>
                </dc:creator>
                <pubDate>Fri, 24 Jul 2026 18:30:52 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/uploads/covers/5e1e335a7a1d3fcc59028c64/ef2a5058-558c-4951-abff-4d51f1c5cd15.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Last year, a client asked me to add a conversational interface to their internal reporting tool. Staff would type a question, and the system would pull a live answer from the database.</p>
<p>I had the first version running in a day. Single questions worked. But a week in, a tester typed: "What is the weather in Berlin, and how much would 500 EUR convert to in USD right now?"</p>
<p>The model called the weather function, returned that answer, and ignored the second half of the question entirely.</p>
<p>That is the gap between a chatbot and an agent. A chatbot works from training data. That's its limit. An agent doesn't have that limit. It calls a tool, reads what came back, and decides whether to keep going.</p>
<p>Most questions resolve in one or two tool calls. Multi-step ones take a few more. Remove that loop, and they all break.</p>
<p>This tutorial shows you how to build that loop with Google Gemini's function calling API and Node.js. You'll build an agent that can call real external tools: Open-Meteo for live weather, frankfurter.app for live currency rates, and a math evaluator for calculations. All three are completely free. The only API key you need is Gemini, which is also free on Google AI Studio at 1,500 requests per day.</p>
<p>Everything is on GitHub: <a href="https://github.com/ziaongit/nodejs-gemini-agent">github.com/ziaongit/nodejs-gemini-agent</a>.</p>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ul>
<li><p><a href="#heading-how-function-calling-works">How Function Calling Works</a></p>
</li>
<li><p><a href="#heading-what-were-building">What We're Building</a></p>
</li>
<li><p><a href="#heading-prerequisites">Prerequisites</a></p>
</li>
<li><p><a href="#heading-project-setup">Project Setup</a></p>
</li>
<li><p><a href="#heading-defining-the-tools">Defining the Tools</a></p>
</li>
<li><p><a href="#heading-implementing-the-tool-functions">Implementing the Tool Functions</a></p>
</li>
<li><p><a href="#heading-building-the-agentic-loop">Building the Agentic Loop</a></p>
</li>
<li><p><a href="#heading-the-cli-entry-point">The CLI Entry Point</a></p>
</li>
<li><p><a href="#heading-adding-an-express-http-server">Adding an Express HTTP Server</a></p>
</li>
<li><p><a href="#heading-testing-the-agent">Testing the Agent</a></p>
</li>
<li><p><a href="#heading-troubleshooting">Troubleshooting</a></p>
</li>
<li><p><a href="#heading-what-to-build-next">What to Build Next</a></p>
</li>
</ul>
<h2 id="heading-how-function-calling-works">How Function Calling Works</h2>
<p>Most LLM tutorials show function calling as: define a function, the model calls it, done. That framing skips the part that actually matters.</p>
<p>The model doesn't call your function. It can't. What happens is more like a negotiation.</p>
<p>You send the model a message along with a list of tool descriptions. Each description is a JSON schema: the function name, what it does, and what arguments it needs. Gemini reads those at request time to decide which tool, if any, fits what the user asked.</p>
<p>Here's the part that surprises people. Gemini doesn't run your code. It sends back a structured object that says: call <code>get_weather</code>, <code>city = Berlin</code>. Your code picks that up, runs the actual function, and sends the result back. Gemini checks whether that's enough to answer. If not, it requests another tool.</p>
<p>That exchange is the loop:</p>
<pre><code class="language-plaintext">User message
      │
      ▼
Model + tool schemas
      │
      ▼
Response: functionCall?
      │
   YES │                          NO
      ▼                            ▼
Run the function(s)         Return text answer
      │
      ▼
Send result(s) back to model
      │
      └──── loop back ────────────┘
</code></pre>
<p>The loop keeps running until the model decides it has enough to answer. That's what allows it to chain calls: check the weather, see the temperature is above 25°C, then decide it should also fetch the exchange rate before answering.</p>
<p>There's one detail that trips people up the first time. When Gemini requests multiple tools in the same response, you run all of them and return all results in a single message. Returning them one at a time in separate messages breaks the model's turn-tracking and produces unreliable output.</p>
<h2 id="heading-what-were-building">What We're Building</h2>
<p>In this tutorial, we'll build an AI agent with three working tools:</p>
<ul>
<li><p><code>get_weather</code> — fetches current weather for any city via Open-Meteo (free, no API key)</p>
</li>
<li><p><code>calculate</code> — evaluates a math expression safely in JavaScript</p>
</li>
<li><p><code>get_exchange_rate</code> — fetches live currency rates via frankfurter.app (free, no API key)</p>
</li>
</ul>
<p>There are two ways to run it: a readline CLI for quick local testing, and an Express HTTP endpoint to wire into a real application.</p>
<p>Full tech stack:</p>
<ul>
<li><p><strong>Node.js 20</strong>: runtime (Node 18 minimum for native fetch)</p>
</li>
<li><p><strong>@google/generative-ai</strong>: official Gemini SDK</p>
</li>
<li><p><strong>dotenv</strong>: environment variable loading</p>
</li>
<li><p><strong>Express</strong>: HTTP server for the API endpoint</p>
</li>
<li><p><strong>Open-Meteo API</strong>: free weather and geocoding, no key required</p>
</li>
<li><p><strong>frankfurter.app</strong>: free currency exchange rates, no key required</p>
</li>
</ul>
<p>Architecture:</p>
<pre><code class="language-plaintext">┌─────────────────────────────────────────────────┐
│                   Client                         │
│         CLI (readline) / HTTP POST               │
└──────────────────────┬──────────────────────────┘
                       │  user message
                       ▼
┌─────────────────────────────────────────────────┐
│               agent.js — Agentic Loop            │
│                                                  │
│  1. Send message + tool schemas to Gemini        │
│  2. Receive response                             │
│  3. functionCalls() present?                     │
│      YES → execute tools in parallel             │
│            send all results back                 │
│            go to step 2                          │
│      NO  → return final text answer              │
└──────────────────────┬──────────────────────────┘
                       │  tool calls
                       ▼
┌─────────────────────────────────────────────────┐
│                  Tool Handlers                   │
│                                                  │
│  get_weather(city)                               │
│    └─► geocoding-api.open-meteo.com              │
│        api.open-meteo.com                        │
│                                                  │
│  calculate(expression)                           │
│    └─► JS safe evaluator (no external call)      │
│                                                  │
│  get_exchange_rate(from, to, amount?)            │
│    └─► api.frankfurter.app                       │
└─────────────────────────────────────────────────┘
</code></pre>
<h2 id="heading-prerequisites">Prerequisites</h2>
<p>Before you start, you should have:</p>
<ul>
<li><p>Node.js 18 or higher — run <code>node --version</code> to check</p>
</li>
<li><p>A Gemini API key from <a href="https://aistudio.google.com">aistudio.google.com</a> — free, no card. The free tier gives you 1,500 requests a day.</p>
</li>
<li><p>You should also know how <code>async/await</code> works in Node.js. That's about it.</p>
</li>
</ul>
<h2 id="heading-project-setup">Project Setup</h2>
<pre><code class="language-bash">mkdir nodejs-gemini-agent &amp;&amp; cd nodejs-gemini-agent
npm init -y
npm install @google/generative-ai dotenv express
mkdir src
</code></pre>
<p>Add a <code>.gitignore</code>, as you don't want <code>.env</code> in your repo:</p>
<pre><code class="language-plaintext">node_modules/
.env
</code></pre>
<p>Drop a <code>.env</code> at the root:</p>
<pre><code class="language-plaintext">GEMINI_API_KEY=your_api_key_here
PORT=3000

# Optional: override the default model (gemini-2.0-flash)
# Uncomment if you hit free-tier quota limits
# GEMINI_MODEL=gemini-2.0-flash-lite
</code></pre>
<p>Project structure:</p>
<pre><code class="language-plaintext">nodejs-gemini-agent/
├── src/
│   ├── tools.js        ← tool schemas for Gemini
│   ├── functions.js    ← actual implementations
│   ├── agent.js        ← the agentic loop
│   ├── index.js        ← CLI entry point
│   └── server.js       ← Express HTTP server
├── .env
├── .env.example
├── .gitignore
└── package.json
</code></pre>
<h2 id="heading-defining-the-tools">Defining the Tools</h2>
<p>Gemini can't see your code. It picks tools based entirely on the JSON schemas you pass in. Each schema has a name, a description, and the parameter definitions.</p>
<p>The description is what drives routing. Gemini reads it at request time to decide which tool fits the user's question. Focus on when to call the function, not just what it does.</p>
<pre><code class="language-js">// src/tools.js

const toolDefinitions = [
  {
    name: 'get_weather',
    description:
      'Get the current weather for a city. Returns temperature in Celsius, ' +
      'humidity percentage, and wind speed. Use this when the user asks about ' +
      'weather, temperature, or climate conditions in any location.',
    parameters: {
      type: 'OBJECT',
      properties: {
        city: {
          type: 'STRING',
          description: 'The city name, e.g. Tokyo, London, New York',
        },
      },
      required: ['city'],
    },
  },
  {
    name: 'calculate',
    description:
      'Evaluate a mathematical expression and return the numeric result. ' +
      'Use this for arithmetic, percentage calculations, or any numeric computation ' +
      'the user asks for. Do not guess at math — always call this tool.',
    parameters: {
      type: 'OBJECT',
      properties: {
        expression: {
          type: 'STRING',
          description:
            'A valid mathematical expression, e.g. "47.50 * 0.18" or "1500 / 12"',
        },
      },
      required: ['expression'],
    },
  },
  {
    name: 'get_exchange_rate',
    description:
      'Get the current exchange rate between two currencies. Can also convert ' +
      'a specific amount. Use this when the user asks about currency conversion, ' +
      'exchange rates, or how much a foreign currency amount is worth.',
    parameters: {
      type: 'OBJECT',
      properties: {
        from: {
          type: 'STRING',
          description: 'The source currency code, e.g. USD, EUR, JPY, GBP',
        },
        to: {
          type: 'STRING',
          description: 'The target currency code, e.g. USD, EUR, JPY, GBP',
        },
        amount: {
          type: 'NUMBER',
          description:
            'Amount to convert. Optional — defaults to 1 if not provided.',
        },
      },
      required: ['from', 'to'],
    },
  },
];

module.exports = { toolDefinitions };
</code></pre>
<p>Vague descriptions work most of the time. The problems show up at the edges. "Does currency stuff" routes fine on a simple question. It falls apart on anything ambiguous. "Get the current exchange rate between two currencies. Use this when the user asks about currency conversion." holds up. The extra words cost basically nothing. Debugging bad routing costs a lot more.</p>
<h2 id="heading-implementing-the-tool-functions">Implementing the Tool Functions</h2>
<p>These are the actual functions that run when the model requests them. Each receives the arguments the model decided to pass, does real work, and returns a plain JavaScript object.</p>
<pre><code class="language-js">// src/functions.js

async function get_weather({ city }) {
  // Open-Meteo uses a two-step approach: geocode the city first, then fetch weather.
  // Both APIs are free with no key required.
  const geoUrl = `https://geocoding-api.open-meteo.com/v1/search?name=${encodeURIComponent(city)}&amp;count=1`;
  const geoRes  = await fetch(geoUrl);
  const geoData = await geoRes.json();

  if (!geoData.results?.length) {
    return { error: `City not found: ${city}` };
  }

  const { latitude, longitude, name, country } = geoData.results[0];

  const weatherUrl =
    `https://api.open-meteo.com/v1/forecast` +
    `?latitude=${latitude}&amp;longitude=${longitude}` +
    `&amp;current=temperature_2m,relative_humidity_2m,wind_speed_10m,weather_code`;

  const weatherRes  = await fetch(weatherUrl);
  const weatherData = await weatherRes.json();
  const current     = weatherData.current;

  return {
    city:        `${name}, ${country}`,
    temperature: `${current.temperature_2m}°C`,
    humidity:    `${current.relative_humidity_2m}%`,
    wind_speed:  `${current.wind_speed_10m} km/h`,
  };
}

function calculate({ expression }) {
  try {
    // Strip anything that is not a number or basic operator before eval.
    // This is not a complete sandbox — use a proper math parser like mathjs
    // in production if expressions come from untrusted users.
    const safe = expression.replace(/[^0-9+\-*/.() %]/g, '');
    if (!safe.trim()) return { error: 'Invalid or empty expression' };

    const result = Function('"use strict"; return (' + safe + ')')();
    return { expression, result };
  } catch {
    return { error: `Could not evaluate: ${expression}` };
  }
}

async function get_exchange_rate({ from, to, amount = 1 }) {
  const url  = `https://api.frankfurter.app/latest?from=${from.toUpperCase()}&amp;to=${to.toUpperCase()}`;
  const res  = await fetch(url);
  const data = await res.json();

  if (data.error) return { error: data.error };

  const rate      = data.rates[to.toUpperCase()];
  if (!rate) return { error: `No rate found for ${from} → ${to}` };

  const converted = parseFloat((amount * rate).toFixed(4));

  return { from: from.toUpperCase(), to: to.toUpperCase(), rate, amount, converted };
}

module.exports = { get_weather, calculate, get_exchange_rate };
</code></pre>
<p>There are a few things worth pointing out here.</p>
<p>Open-Meteo uses geocoding before the weather fetch. Passing latitude and longitude directly to the weather endpoint is more reliable than a city name string, and the geocoding API handles misspellings reasonably well. There are two fetch calls, but that's the trade-off for accuracy.</p>
<p>The <code>calculate</code> function strips everything except digits and operators before evaluation. It narrows the injection risk, but it's not a real sandbox. If you expose this over HTTP with anonymous users, use a proper math parser like <a href="https://mathjs.org">mathjs</a>.</p>
<p>Frankfurter converts currency codes to uppercase before the request. Users will type "usd" or "Usd" and both should work. The API is case-sensitive on its end.</p>
<h2 id="heading-building-the-agentic-loop">Building the Agentic Loop</h2>
<p>This is the file everything else supports. The loop itself is about 20 lines. The rest is logging and error handling.</p>
<pre><code class="language-js">// src/agent.js
const { GoogleGenerativeAI } = require('@google/generative-ai');
const { toolDefinitions }    = require('./tools');
const { get_weather, calculate, get_exchange_rate } = require('./functions');

const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);

// Map tool names to handler functions
const toolHandlers = { get_weather, calculate, get_exchange_rate };

async function runAgent(userMessage) {
  const model = genAI.getGenerativeModel({
    model: process.env.GEMINI_MODEL || 'gemini-2.0-flash',
    tools: [{ functionDeclarations: toolDefinitions }],
  });

  const chat = model.startChat();

  console.log(`\nUser: ${userMessage}`);
  console.log('---');

  let response = await chat.sendMessage(userMessage);
  let iterations = 0;
  const MAX_ITERATIONS = 10; // safety cap against infinite loops

  // Agentic loop
  while (iterations &lt; MAX_ITERATIONS) {
    iterations++;
    const calls = response.response.functionCalls();

    // No tool calls — model is done, return the answer
    if (!calls || calls.length === 0) break;

    // Run all requested tools, collect results
    const toolResults = await Promise.allSettled(
      calls.map(async (call) =&gt; {
        console.log(`Calling tool: ${call.name}(${JSON.stringify(call.args)})`);

        const handler = toolHandlers[call.name];

        if (!handler) {
          return {
            functionResponse: {
              name:     call.name,
              response: { error: `Unknown tool: ${call.name}` },
            },
          };
        }

        try {
          const result = await handler(call.args);
          console.log(`Tool result: ${JSON.stringify(result)}`);
          return {
            functionResponse: {
              name:     call.name,
              response: result,
            },
          };
        } catch (err) {
          return {
            functionResponse: {
              name:     call.name,
              response: { error: err.message },
            },
          };
        }
      })
    );

    // Extract values from allSettled (fulfilled only — errors already caught above)
    const parts = toolResults
      .filter(r =&gt; r.status === 'fulfilled')
      .map(r =&gt; r.value);

    // Send all results back to the model in one message
    response = await chat.sendMessage(parts);
  }

  return response.response.text();
}

module.exports = { runAgent };
</code></pre>
<p>The <code>MAX_ITERATIONS</code> cap isn't paranoia. A model can get into a loop if a tool keeps returning an error and the model keeps retrying. 10 iterations is more than enough for any real query. A complex multi-tool question typically resolves in two or three turns.</p>
<p><code>Promise.allSettled</code> runs all requested tools in parallel rather than sequentially. When the model requests both weather and exchange rate in the same response, they fetch simultaneously. Individual tool failures get caught inside the map rather than letting one failure abort the others.</p>
<p>The logging is intentional. When you're building and testing an agent, watching the tool calls happen in real time tells you whether the routing is working. Production code would route these to a structured logger instead of console output, but the information is the same.</p>
<h2 id="heading-the-cli-entry-point">The CLI Entry Point</h2>
<p><code>require('dotenv').config()</code> runs first so your API key loads before anything else. After that it's a standard readline loop: each line you type goes to <code>runAgent</code>, the response prints, and the loop waits for the next input.</p>
<pre><code class="language-js">// src/index.js
require('dotenv').config();
const readline     = require('readline');
const { runAgent } = require('./agent');

const rl = readline.createInterface({
  input:  process.stdin,
  output: process.stdout,
});

function ask(prompt) {
  return new Promise(resolve =&gt; rl.question(prompt, resolve));
}

async function main() {
  console.log('Gemini Agent — type your question, or "exit" to quit\n');

  while (true) {
    const input = await ask('You: ');
    if (input.toLowerCase() === 'exit') break;
    if (!input.trim()) continue;

    try {
      const answer = await runAgent(input);
      console.log(`\nAgent: ${answer}\n`);
    } catch (err) {
      console.error(`Error: ${err.message}`);
    }
  }

  rl.close();
}

main();
</code></pre>
<h2 id="heading-adding-an-express-http-server">Adding an Express HTTP Server</h2>
<p>The CLI is useful for testing. For integrating into an app, you need an HTTP endpoint.</p>
<pre><code class="language-js">// src/server.js
require('dotenv').config();
const express      = require('express');
const { runAgent } = require('./agent');

const app  = express();
const PORT = process.env.PORT || 3000;

app.use(express.json());

app.post('/agent', async (req, res) =&gt; {
  const { message } = req.body;

  if (!message || typeof message !== 'string') {
    return res.status(400).json({ error: 'message is required and must be a string' });
  }

  try {
    const answer = await runAgent(message);
    res.json({ answer });
  } catch (err) {
    console.error('[agent error]', err.message);
    res.status(500).json({ error: 'Agent failed to process the request' });
  }
});

app.listen(PORT, () =&gt; {
  console.log(`Agent server running on http://localhost:${PORT}`);
});
</code></pre>
<p>Start it:</p>
<pre><code class="language-bash">node src/server.js
</code></pre>
<p>Call it:</p>
<pre><code class="language-bash">curl -X POST http://localhost:3000/agent \
  -H "Content-Type: application/json" \
  -d '{"message": "What is the weather in Paris?"}'
</code></pre>
<p>Response:</p>
<pre><code class="language-json">{
  "answer": "The current weather in Paris, France is 19°C with 65% humidity and wind speeds of 12 km/h."
}
</code></pre>
<p>The POST body is a plain <code>{ message }</code> object. The response is a plain <code>{ answer }</code> string. The agent handles the rest.</p>
<h2 id="heading-testing-the-agent">Testing the Agent</h2>
<p>Start the CLI:</p>
<pre><code class="language-bash">node src/index.js
</code></pre>
<p>The <code>You:</code> prompt comes from the readline in <code>index.js</code>. The <code>User:</code> line and <code>---</code> separator are logged by <code>agent.js</code> at the start of each run. This is the same logging discussed in the agentic loop section.</p>
<p>Single tool — weather:</p>
<pre><code class="language-plaintext">You: What's the weather in Tokyo?

User: What's the weather in Tokyo?
---
Calling tool: get_weather({"city":"Tokyo"})
Tool result: {"city":"Tokyo, JP","temperature":"31°C","humidity":"72%","wind_speed":"8 km/h"}

Agent: The current weather in Tokyo, Japan is 31°C with 72% humidity and wind speeds of 8 km/h.
</code></pre>
<p>Two tools — chained reasoning:</p>
<pre><code class="language-plaintext">You: What is the weather in Tokyo? If it's above 20°C, convert 10000 JPY to EUR.

User: What is the weather in Tokyo? If it's above 20°C, convert 10000 JPY to EUR.
---
Calling tool: get_weather({"city":"Tokyo"})
Tool result: {"city":"Tokyo, JP","temperature":"31°C","humidity":"72%","wind_speed":"8 km/h"}

Calling tool: get_exchange_rate({"from":"JPY","to":"EUR","amount":10000})
Tool result: {"from":"JPY","to":"EUR","rate":0.006,"amount":10000,"converted":60.0}

Agent: The current temperature in Tokyo is 31°C, which is above 20°C.
Converting 10,000 JPY to EUR at the current exchange rate gives approximately 60.00 EUR.
</code></pre>
<p>Notice what happened: the model called <code>get_weather</code>, read the result (31°C), applied the conditional logic from the user's question on its own, and then called <code>get_exchange_rate</code>. You didn't write any of that branching logic. The model handled it from the description alone.</p>
<p>Calculator:</p>
<pre><code class="language-plaintext">You: How much is 18% tip on a $47.50 restaurant bill?

User: How much is 18% tip on a $47.50 restaurant bill?
---
Calling tool: calculate({"expression":"47.50 * 0.18"})
Tool result: {"expression":"47.50 * 0.18","result":8.55}

Agent: An 18% tip on a $47.50 bill is $8.55, making your total $56.05.
</code></pre>
<p>Three tools in one query:</p>
<pre><code class="language-plaintext">You: What's the weather in London and Berlin? And what is 250 GBP in EUR?

User: What's the weather in London and Berlin? And what is 250 GBP in EUR?
---
Calling tool: get_weather({"city":"London"})
Calling tool: get_weather({"city":"Berlin"})
Calling tool: get_exchange_rate({"from":"GBP","to":"EUR","amount":250})
Tool result: {"city":"London, GB","temperature":"16°C","humidity":"78%","wind_speed":"20 km/h"}
Tool result: {"city":"Berlin, DE","temperature":"22°C","humidity":"55%","wind_speed":"14 km/h"}
Tool result: {"from":"GBP","to":"EUR","rate":1.17,"amount":250,"converted":292.5}

Agent: London is currently 16°C with 78% humidity and 20 km/h winds.
Berlin is warmer at 22°C with 55% humidity and lighter winds of 14 km/h.
250 GBP converts to approximately 292.50 EUR at the current exchange rate.
</code></pre>
<p>All three tools ran in parallel. <code>Promise.allSettled</code> is why. A sequential loop would have made three serial network requests. Parallel gives you the same result in roughly the time of the slowest single request.</p>
<h2 id="heading-troubleshooting">Troubleshooting</h2>
<p>Here are a few common issues you might encounter, and how to fix them:</p>
<h3 id="heading-1-404-not-found-modelsgemini-15-flash-is-not-found-for-api-version-v1beta">1. <code>[404 Not Found] models/gemini-1.5-flash is not found for API version v1beta</code></h3>
<p>The model name is outdated. Google deprecates older aliases over time. Swap it out for <code>gemini-2.0-flash</code> in <code>agent.js</code>. To check what models your key can actually access, run:</p>
<pre><code class="language-bash">node -e "
const { GoogleGenerativeAI } = require('@google/generative-ai');
require('dotenv').config();
const g = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);
g.listModels().then(r =&gt; r.models.forEach(m =&gt; console.log(m.name)));
"
</code></pre>
<h3 id="heading-2-429-too-many-requests-you-exceeded-your-current-quota">2. <code>[429 Too Many Requests] You exceeded your current quota</code></h3>
<p>The <code>gemini-2.0-flash</code> free tier caps you at 1,500 requests a day. Hit that and every call returns a 429 until midnight Pacific resets the counter.</p>
<p>The error names the quota ID directly. <code>GenerateRequestsPerDayPerProjectPerModel-FreeTier</code> means you hit the daily cap. <code>GenerateRequestsPerMinutePerProjectPerModel-FreeTier</code> means the per-minute rate.</p>
<p>For the per-minute limit, the error includes a <code>retryDelay</code> field. Wait that many seconds and retry. For the daily limit, the quota is per-project. All models under the same project share it.</p>
<p>There are three ways out:</p>
<ul>
<li><p><strong>New project</strong> (fastest): head to <a href="https://aistudio.google.com">aistudio.google.com</a>, spin up a new project, grab a new API key, and swap it into <code>.env</code>. You get a fresh quota immediately.</p>
</li>
<li><p><strong>Enable billing</strong>: billing-enabled projects get much higher limits while keeping the free usage tier. Set up at <a href="https://aistudio.google.com">aistudio.google.com</a>.</p>
</li>
<li><p><strong>Wait</strong>: resets daily at midnight Pacific.</p>
</li>
</ul>
<p>Because <code>agent.js</code> reads the model name from <code>process.env.GEMINI_MODEL</code>, you can also switch models without touching code. Add this to your <code>.env</code> to test with a lighter model:</p>
<pre><code class="language-plaintext">GEMINI_MODEL=gemini-2.0-flash-lite
</code></pre>
<p>Remove the line when your quota resets and the agent goes back to <code>gemini-2.0-flash</code>.</p>
<h3 id="heading-3-error-geminiapikey-is-not-set">3. <code>Error: GEMINI_API_KEY is not set</code></h3>
<p>Nine times out of ten, <code>require('dotenv').config()</code> is either missing or buried below other requires. Drag it to the very top of <code>index.js</code>. Your <code>.env</code> also needs to live at the project root with your actual key in it, not <code>your_api_key_here</code>.</p>
<h3 id="heading-4-googlegenerativeaierror-400-invalidargument">4. <code>GoogleGenerativeAIError: 400 INVALID_ARGUMENT</code></h3>
<p>Almost always a malformed tool schema. Gemini uses uppercase type strings: <code>'OBJECT'</code>, <code>'STRING'</code>, <code>'NUMBER'</code>. JSON Schema uses lowercase. Check your <code>parameters.type</code> values.</p>
<h3 id="heading-5-model-answers-without-calling-any-tools">5. Model Answers Without Calling Any Tools</h3>
<p>The description is too vague or the user's question doesn't match well enough for the model to route it. Add more context to the description about when the tool should be used. The phrase "use this when the user asks about X" directly improves routing accuracy.</p>
<h3 id="heading-6-typeerror-fetch-is-not-a-function">6. <code>TypeError: fetch is not a function</code></h3>
<p>Node 17 and below don't have native <code>fetch</code>. It was added in Node 18. Run <code>node --version</code> to check yours.</p>
<p>If you can't upgrade, install it with <code>npm install node-fetch</code>. Every file that calls <code>fetch</code> then needs <code>const fetch = require('node-fetch')</code> as its first line.</p>
<h3 id="heading-7-tool-works-in-isolation-but-agent-loop-doesnt-call-it">7. Tool Works in Isolation but Agent Loop Doesn't Call it</h3>
<p>The name in <code>toolDefinitions</code> must exactly match the key in <code>toolHandlers</code>. Case matters in JavaScript. <code>get_Weather</code> and <code>get_weather</code> are two different things.</p>
<h3 id="heading-8-exchange-rate-returns-no-rate-found">8. Exchange Rate Returns <code>No rate found</code></h3>
<p>The currency code you passed isn't supported by frankfurter.app. The API covers ~30 major currencies. Check supported codes at <a href="https://www.frankfurter.app/docs/">frankfurter.app</a>.</p>
<h2 id="heading-what-to-build-next">What to Build Next</h2>
<p>The three tools here are a foundation. The loop works the same way regardless of how many tools you add.</p>
<p><strong>Database lookup tool:</strong> A <code>search_products</code> function that queries your PostgreSQL table turns the agent into a product assistant. Point it at your catalog, and it can answer questions about availability, pricing, and specs without you writing any routing logic.</p>
<p><strong>Write tools:</strong> <code>get_*</code> functions make the agent read-only. Add a <code>create_ticket</code> or <code>send_notification</code> function and the agent can take actions: file a support request, trigger a workflow, update a record. Once you add write tools, think carefully about <strong>which queries should require confirmation before executing</strong>.</p>
<p><strong>Memory across sessions:</strong> Right now <code>model.startChat()</code> creates a fresh conversation on every call. Pass a <code>history</code> array when starting the chat and the model remembers prior turns. Store that history in PostgreSQL or Redis keyed to the user ID, and the agent carries context across sessions.</p>
<p><strong>Streaming responses:</strong> For a UI that shows the answer as it types rather than waiting for the full response, replace <code>chat.sendMessage</code> with <code>chat.sendMessageStream</code>. The tool call loop stays the same. Only the final response delivery changes.</p>
<p><strong>Swap the model:</strong> The <code>model</code> string in <code>getGenerativeModel</code> is the only thing that pins you to Gemini 2.0 Flash. <code>gemini-2.0-flash-lite</code> is lighter and faster for simpler queries. For stronger reasoning on complex tasks, run the <code>listModels</code> script from the Troubleshooting section to find the latest available models. The function calling interface is identical across all Gemini models, so swapping takes one line.</p>
<p>The full source code for this article is on GitHub at <a href="https://github.com/ziaongit/nodejs-gemini-agent">github.com/ziaongit/nodejs-gemini-agent</a>.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Build a Browser-Based PDF Signature Tool Using JavaScript ]]>
                </title>
                <description>
                    <![CDATA[ PDF documents are commonly used for agreements, forms, approvals, invoices, reports, applications, and other documents that may need a signature or additional text before they are shared. A traditiona ]]>
                </description>
                <link>https://www.freecodecamp.org/news/build-pdf-signature-tool-javascript/</link>
                <guid isPermaLink="false">6a5e89518186f4c5817d466b</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ pdf ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Tutorial ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Hashnode ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Bhavin Sheth ]]>
                </dc:creator>
                <pubDate>Mon, 20 Jul 2026 20:47:13 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/uploads/covers/5e1e335a7a1d3fcc59028c64/175ab1f9-2917-4588-9e67-50607f6fa5a1.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>PDF documents are commonly used for agreements, forms, approvals, invoices, reports, applications, and other documents that may need a signature or additional text before they are shared.</p>
<p>A traditional workflow often involves printing the document, signing it by hand, scanning it again, and sending the new file. For a simple electronic signature, that process adds unnecessary steps.</p>
<p>In this tutorial, you'll build a browser-based PDF Signature Tool using JavaScript. Users will be able to upload a PDF, preview and navigate its pages, and add content directly to the document.</p>
<p>The application will support two main element types: <strong>Signature</strong> and <strong>Text/Stamp</strong>.</p>
<p>For signatures, users can draw directly in the browser, type their name and choose a signature style, or upload an existing signature image. For text-based elements, they can enter custom text or use preset stamps such as <strong>APPROVED</strong>, <strong>CONFIDENTIAL</strong>, <strong>DRAFT</strong>, and <strong>PAID</strong>.</p>
<p>After creating an element, users can position it on the PDF preview and adjust properties such as scale, rotation, opacity, font size, and color. The element can then be applied to the current page, every page, or a specific set of pages.</p>
<p>Once processing is complete, the application generates a new PDF for review. Users can preview the result, rename the output file, check its page count and file size, and download it directly from the browser.</p>
<p>The project uses PDF.js for document rendering and PDF-lib for modifying and generating the final PDF.</p>
<p>By the end of this tutorial, you'll understand how to build an interactive PDF editing workflow that combines canvas-based input, image embedding, text placement, coordinate conversion, page selection, and client-side file generation.</p>
<h3 id="heading-what-well-cover">What We'll Cover:</h3>
<ul>
<li><p><a href="#heading-what-this-pdf-signature-tool-can-do">What This PDF Signature Tool Can Do</a></p>
</li>
<li><p><a href="#heading-electronic-signatures-vs-digital-signatures">Electronic Signatures vs Digital Signatures</a></p>
</li>
<li><p><a href="#heading-how-the-browser-based-workflow-works">How the Browser-Based Workflow Works</a></p>
</li>
<li><p><a href="#heading-project-setup">Project Setup</a></p>
</li>
<li><p><a href="#heading-what-libraries-are-we-using">What Libraries Are We Using?</a></p>
</li>
<li><p><a href="#heading-uploading-and-previewing-the-pdf">Uploading and Previewing the PDF</a></p>
</li>
<li><p><a href="#heading-choosing-an-element-to-add">Choosing an Element to Add</a></p>
</li>
<li><p><a href="#heading-creating-a-signature">Creating a Signature</a></p>
</li>
<li><p><a href="#heading-drawing-a-signature">Drawing a Signature</a></p>
</li>
<li><p><a href="#heading-typing-a-signature">Typing a Signature</a></p>
</li>
<li><p><a href="#heading-uploading-a-signature-image">Uploading a Signature Image</a></p>
</li>
<li><p><a href="#heading-adding-text-and-preset-stamps">Adding Text and Preset Stamps</a></p>
</li>
<li><p><a href="#heading-positioning-and-styling-the-element">Positioning and Styling the Element</a></p>
</li>
<li><p><a href="#heading-applying-the-element-to-selected-pages">Applying the Element to Selected Pages</a></p>
</li>
<li><p><a href="#heading-applying-and-finalizing-the-pdf">Applying and Finalizing the PDF</a></p>
</li>
<li><p><a href="#heading-generating-the-signed-pdf">Generating the Signed PDF</a></p>
</li>
<li><p><a href="#heading-previewing-the-final-pdf">Previewing the Final PDF</a></p>
</li>
<li><p><a href="#heading-renaming-and-downloading-the-final-pdf">Renaming and Downloading the Final PDF</a></p>
</li>
<li><p><a href="#heading-demo-how-the-pdf-signature-tool-works">Demo: How the PDF Signature Tool Works</a></p>
</li>
<li><p><a href="#heading-handling-signature-transparency">Handling Signature Transparency</a></p>
</li>
<li><p><a href="#heading-important-notes-and-common-mistakes">Important Notes and Common Mistakes</a></p>
</li>
<li><p><a href="#heading-conclusion">Conclusion</a></p>
</li>
</ul>
<h2 id="heading-what-this-pdf-signature-tool-can-do">What This PDF Signature Tool Can Do</h2>
<p>The application provides a single editing workflow for adding signatures, text, and common document stamps to PDF pages.</p>
<p>When <strong>Signature</strong> is selected, users can create the signature in three different ways.</p>
<ol>
<li><p>The <strong>Draw</strong> option provides a canvas where the user can write a signature using a mouse, trackpad, stylus, or touch input.</p>
</li>
<li><p>The <strong>Type</strong> option converts entered text into a signature-style element. Users can type their name, adjust the size, and choose from the available signature styles.</p>
</li>
<li><p>The <strong>Upload</strong> option accepts an existing signature image. This is useful for someone who already has a transparent PNG or another supported image of their handwritten signature.</p>
</li>
</ol>
<p>The second element type is <strong>Text/Stamp</strong>. Users can enter custom text such as:</p>
<pre><code class="language-text">Signed on: 08-09-2025
</code></pre>
<p>They can also quickly choose a predefined stamp:</p>
<pre><code class="language-text">APPROVED
CONFIDENTIAL
DRAFT
PAID
</code></pre>
<p>After an element has been created, the application provides controls for its placement and appearance. Users can move it to the required location and adjust its scale, rotation, opacity, and position.</p>
<p>Text and stamp elements can additionally use configurable font sizes and colors.</p>
<p>The page controls determine where the selected element will be applied. A signature may belong only on the final page of a contract, while a <code>CONFIDENTIAL</code> stamp may need to appear on every page.</p>
<p>The application therefore supports:</p>
<pre><code class="language-text">Current page only
All pages
Specific pages
</code></pre>
<p>The goal is to provide one consistent workflow for several common PDF editing tasks without requiring separate tools for each element type.</p>
<h2 id="heading-electronic-signatures-vs-digital-signatures">Electronic Signatures vs Digital Signatures</h2>
<p>Before building the application, it's important to distinguish between an <strong>electronic signature</strong> and a <strong>digital signature</strong>.</p>
<p>The tool in this tutorial creates an electronic signature workflow.</p>
<p>A drawn signature, typed signature, or uploaded signature image is placed visually onto the PDF page. This is similar to signing a document by hand and inserting a visible representation of that signature into the file.</p>
<p>For example, a user might draw a signature on a canvas:</p>
<pre><code class="language-javascript">const signatureImage =
    signatureCanvas.toDataURL("image/png");
</code></pre>
<p>The generated image can then be embedded into the PDF.</p>
<p>A digital signature is technically different.</p>
<p>Certificate-based digital signatures use cryptographic methods to help verify document integrity and the identity associated with a signing certificate. They may involve digital certificates, private keys, signature validation, and trust chains.</p>
<p>Simply placing a handwritten signature image on a PDF doesn't create that type of cryptographic verification.</p>
<p>This distinction matters because the terms are sometimes used interchangeably in everyday conversation even though the underlying technologies are different.</p>
<p>The project we're building focuses on <strong>visual electronic signatures and document elements</strong>. It doesn't create certificate-based cryptographic digital signatures.</p>
<p>Keeping that distinction clear makes it easier to understand exactly what the application does and what would require a more advanced signing system.</p>
<h2 id="heading-how-the-browser-based-workflow-works">How the Browser-Based Workflow Works</h2>
<p>The process begins when a user selects a PDF file.</p>
<p>PDF.js loads the document and renders the current page into a browser canvas. Previous and next buttons allow the user to navigate through the PDF before choosing where to place an element.</p>
<p>The user then selects one of two element types:</p>
<pre><code class="language-text">Signature
Text/Stamp
</code></pre>
<p>If <strong>Signature</strong> is selected, the application provides three creation methods:</p>
<pre><code class="language-text">Draw
Type
Upload
</code></pre>
<p>The selected signature is converted into an element that can be displayed over the PDF preview.</p>
<p>If <strong>Text/Stamp</strong> is selected, the application instead creates a text element using either custom content or one of the predefined stamp values.</p>
<p>The complete workflow looks like this:</p>
<pre><code class="language-text">Upload PDF
    ↓
Render and Navigate Pages
    ↓
Choose Signature or Text/Stamp
    ↓
Create the Element
    ↓
Position and Style It
    ↓
Choose Target Pages
    ↓
Apply &amp; Finalize
    ↓
Generate the New PDF
    ↓
Preview the Result
    ↓
Rename and Download
</code></pre>
<p>During editing, the element displayed over the PDF preview is only a browser-side representation. Its position must later be translated into coordinates that match the actual PDF page.</p>
<p>For example, the application may store an element like this:</p>
<pre><code class="language-javascript">const element = {
    type: "signature",
    x: 622,
    y: 496,
    scale: 1.14,
    rotation: 0,
    opacity: 1
};
</code></pre>
<p>When the user clicks <strong>Apply &amp; Finalize</strong>, those values are used to calculate the final placement inside the PDF.</p>
<p>This separation between the interactive preview and the final PDF generation is the foundation of the project. It allows users to visually prepare the document first and create the modified PDF only after the placement is ready.</p>
<h2 id="heading-project-setup">Project Setup</h2>
<p>To keep the project easy to understand, we'll use three main files:</p>
<pre><code class="language-text">pdf-signature-tool/
│
├── index.html
├── style.css
└── script.js
</code></pre>
<p>The HTML file contains the upload interface, PDF preview, editing controls, final preview, and download section.</p>
<p>The CSS file handles the layout and visual states.</p>
<p>The JavaScript file manages PDF loading, page rendering, signature creation, text and stamp elements, positioning, final PDF generation, and downloading.</p>
<p>Start with the basic HTML structure:</p>
<pre><code class="language-html">&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;

    &lt;meta charset="UTF-8"&gt;

    &lt;meta
        name="viewport"
        content="width=device-width, initial-scale=1.0"&gt;

    &lt;title&gt;PDF Signature Tool&lt;/title&gt;

    &lt;link
        rel="stylesheet"
        href="style.css"&gt;

&lt;/head&gt;

&lt;body&gt;

    &lt;main class="pdf-signature-tool"&gt;

        &lt;section id="uploadSection"&gt;

            &lt;h1&gt;PDF Signature Tool&lt;/h1&gt;

            &lt;p&gt;
                Upload your PDF to add your
                electronic signature.
            &lt;/p&gt;

            &lt;div id="dropZone"&gt;

                &lt;p&gt;Drag &amp; Drop PDF Here&lt;/p&gt;

                &lt;p&gt;Or click to browse file&lt;/p&gt;

                &lt;button id="selectPdfButton"&gt;
                    Select PDF
                &lt;/button&gt;

                &lt;input
                    type="file"
                    id="pdfInput"
                    accept="application/pdf"
                    hidden&gt;

            &lt;/div&gt;

        &lt;/section&gt;

        &lt;section
            id="editorSection"
            hidden&gt;

            &lt;div class="pdf-preview"&gt;

                &lt;div id="previewContainer"&gt;

                    &lt;canvas id="pdfCanvas"&gt;&lt;/canvas&gt;

                    &lt;div id="elementLayer"&gt;&lt;/div&gt;

                &lt;/div&gt;

                &lt;div class="page-navigation"&gt;

                    &lt;button id="previousPage"&gt;
                        &amp;lt;
                    &lt;/button&gt;

                    &lt;span id="pageInfo"&gt;
                        Page 1 of 1
                    &lt;/span&gt;

                    &lt;button id="nextPage"&gt;
                        &amp;gt;
                    &lt;/button&gt;

                &lt;/div&gt;

            &lt;/div&gt;

            &lt;aside id="editorControls"&gt;

                &lt;!-- Signature and text controls
                     will be added here --&gt;

            &lt;/aside&gt;

        &lt;/section&gt;

        &lt;section
            id="resultSection"
            hidden&gt;

            &lt;!-- Final preview and download
                 controls will be added here --&gt;

        &lt;/section&gt;

    &lt;/main&gt;

    &lt;script src="script.js"&gt;&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p>The <code>previewContainer</code> is especially important.</p>
<p>It contains two layers:</p>
<pre><code class="language-text">PDF Canvas
    +
Interactive Element Layer
</code></pre>
<p>The PDF page is rendered onto the canvas, while signatures, text, and stamps are displayed in a separate overlay.</p>
<p>This allows users to move and style an element without modifying the original PDF every time they make a small adjustment.</p>
<p>The overlay should match the dimensions and position of the PDF canvas.</p>
<pre><code class="language-css">#previewContainer {
    position: relative;
    display: inline-block;
}

#pdfCanvas {
    display: block;
}

#elementLayer {
    position: absolute;
    inset: 0;
    pointer-events: none;
}
</code></pre>
<p>Individual signature and text elements can later enable their own pointer interactions.</p>
<pre><code class="language-css">.pdf-element {
    position: absolute;
    cursor: move;
    pointer-events: auto;
    transform-origin: center;
}
</code></pre>
<p>This layered structure becomes the foundation of the interactive editor.</p>
<h2 id="heading-what-libraries-are-we-using">What Libraries Are We Using?</h2>
<p>This project uses two JavaScript libraries for different parts of the PDF workflow.</p>
<h3 id="heading-pdfjs-for-rendering-and-previewing">PDF.js for Rendering and Previewing</h3>
<p>PDF.js is responsible for reading the uploaded document and rendering its pages inside the browser.</p>
<p>A page can be loaded like this:</p>
<pre><code class="language-javascript">const page =
    await pdfDocument.getPage(
        currentPage
    );
</code></pre>
<p>The page is then rendered to a canvas:</p>
<pre><code class="language-javascript">const viewport =
    page.getViewport({
        scale: 1.5
    });

const context =
    pdfCanvas.getContext("2d");

pdfCanvas.width =
    viewport.width;

pdfCanvas.height =
    viewport.height;

await page.render({

    canvasContext: context,

    viewport

}).promise;
</code></pre>
<p>PDF.js handles the visual preview.</p>
<h3 id="heading-pdf-lib-for-modifying-the-pdf">PDF-lib for Modifying the PDF</h3>
<p>PDF-lib is used later when the user clicks <strong>Apply &amp; Finalize</strong>.</p>
<p>It allows us to load the original PDF bytes and add content to its pages.</p>
<p>For example:</p>
<pre><code class="language-javascript">const pdfDoc =
    await PDFLib.PDFDocument.load(
        originalPdfBytes
    );
</code></pre>
<p>An uploaded PNG signature can then be embedded:</p>
<pre><code class="language-javascript">const signatureImage =
    await pdfDoc.embedPng(
        signatureBytes
    );
</code></pre>
<p>Text can also be drawn directly onto a PDF page:</p>
<pre><code class="language-javascript">page.drawText(
    "APPROVED",
    {
        x: 100,
        y: 100,
        size: 18
    }
);
</code></pre>
<p>The two libraries therefore have separate responsibilities:</p>
<pre><code class="language-text">PDF.js
→ Load and visually render PDF pages

PDF-lib
→ Modify pages and generate the final PDF
</code></pre>
<p>Separating these responsibilities keeps the editor easier to manage.</p>
<p>Include both libraries in the project before <code>script.js</code>.</p>
<pre><code class="language-html">&lt;script
    src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.min.js"&gt;
&lt;/script&gt;

&lt;script
    src="https://unpkg.com/pdf-lib/dist/pdf-lib.min.js"&gt;
&lt;/script&gt;

&lt;script src="script.js"&gt;&lt;/script&gt;
</code></pre>
<p>Configure the PDF.js worker as well:</p>
<pre><code class="language-javascript">pdfjsLib.GlobalWorkerOptions.workerSrc =
    "https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.worker.min.js";
</code></pre>
<p>For a production project, pin and test the exact library versions you use rather than automatically loading an unspecified latest release.</p>
<h2 id="heading-uploading-and-previewing-the-pdf">Uploading and Previewing the PDF</h2>
<p>The first interactive step is accepting the user's PDF.</p>
<p>Get references to the required elements:</p>
<pre><code class="language-javascript">const pdfInput =
    document.getElementById(
        "pdfInput"
    );

const selectPdfButton =
    document.getElementById(
        "selectPdfButton"
    );

const dropZone =
    document.getElementById(
        "dropZone"
    );

const uploadSection =
    document.getElementById(
        "uploadSection"
    );

const editorSection =
    document.getElementById(
        "editorSection"
    );

const pdfCanvas =
    document.getElementById(
        "pdfCanvas"
    );
</code></pre>
<p>We also need a few variables to store the current document state.</p>
<pre><code class="language-javascript">let pdfDocument = null;

let originalPdfBytes = null;

let currentPage = 1;

let totalPages = 0;
</code></pre>
<p>Clicking the custom button opens the hidden file input.</p>
<pre><code class="language-javascript">selectPdfButton.addEventListener(
    "click",
    () =&gt; {

        pdfInput.click();

    }
);
</code></pre>
<p>When a file is selected, pass it to the PDF loading function.</p>
<pre><code class="language-javascript">pdfInput.addEventListener(
    "change",
    event =&gt; {

        const file =
            event.target.files[0];

        if (file) {

            loadPdf(file);

        }

    }
);
</code></pre>
<p>Before processing the file, validate its type.</p>
<pre><code class="language-javascript">async function loadPdf(file) {

    if (
        file.type !==
        "application/pdf"
    ) {

        alert(
            "Please select a valid PDF file."
        );

        return;

    }

}
</code></pre>
<p>Read the file as an <code>ArrayBuffer</code>.</p>
<pre><code class="language-javascript">const arrayBuffer =
    await file.arrayBuffer();
</code></pre>
<p>Keep a copy of the original bytes because PDF.js and PDF-lib will use the document at different stages.</p>
<pre><code class="language-javascript">originalPdfBytes =
    new Uint8Array(
        arrayBuffer
    );
</code></pre>
<p>Now load the document with PDF.js.</p>
<pre><code class="language-javascript">pdfDocument =
    await pdfjsLib
        .getDocument({
            data:
                originalPdfBytes.slice()
        })
        .promise;
</code></pre>
<p>Store the number of pages.</p>
<pre><code class="language-javascript">totalPages =
    pdfDocument.numPages;

currentPage = 1;
</code></pre>
<p>Switch from the upload interface to the editor.</p>
<pre><code class="language-javascript">uploadSection.hidden = true;

editorSection.hidden = false;
</code></pre>
<p>Finally, render the first page.</p>
<pre><code class="language-javascript">await renderPage(currentPage);
</code></pre>
<p>The complete loading function becomes:</p>
<pre><code class="language-javascript">async function loadPdf(file) {

    if (
        file.type !==
        "application/pdf"
    ) {

        alert(
            "Please select a valid PDF file."
        );

        return;

    }

    const arrayBuffer =
        await file.arrayBuffer();

    originalPdfBytes =
        new Uint8Array(
            arrayBuffer
        );

    pdfDocument =
        await pdfjsLib
            .getDocument({
                data:
                    originalPdfBytes.slice()
            })
            .promise;

    totalPages =
        pdfDocument.numPages;

    currentPage = 1;

    uploadSection.hidden = true;

    editorSection.hidden = false;

    await renderPage(currentPage);

}
</code></pre>
<p>For drag-and-drop support, prevent the browser's default behavior.</p>
<pre><code class="language-javascript">dropZone.addEventListener(
    "dragover",
    event =&gt; {

        event.preventDefault();

        dropZone.classList.add(
            "drag-active"
        );

    }
);
</code></pre>
<p>Remove the active state when the file leaves the drop area.</p>
<pre><code class="language-javascript">dropZone.addEventListener(
    "dragleave",
    () =&gt; {

        dropZone.classList.remove(
            "drag-active"
        );

    }
);
</code></pre>
<p>Handle the dropped file:</p>
<pre><code class="language-javascript">dropZone.addEventListener(
    "drop",
    event =&gt; {

        event.preventDefault();

        dropZone.classList.remove(
            "drag-active"
        );

        const file =
            event.dataTransfer.files[0];

        if (file) {

            loadPdf(file);

        }

    }
);
</code></pre>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/6605c5ed-31d3-4a2a-a80d-95a77040d890.png" alt="PDF Signature Tool upload area with drag-and-drop support and Select PDF button." style="display:block;margin:0 auto" width="639" height="652" loading="lazy">

<h2 id="heading-rendering-the-current-pdf-page">Rendering the Current PDF Page</h2>
<p>The <code>renderPage()</code> function loads one page from the PDF and displays it on the canvas.</p>
<pre><code class="language-javascript">async function renderPage(
    pageNumber
) {

    const page =
        await pdfDocument.getPage(
            pageNumber
        );

    const viewport =
        page.getViewport({
            scale: 1.5
        });

    const context =
        pdfCanvas.getContext("2d");

    pdfCanvas.width =
        viewport.width;

    pdfCanvas.height =
        viewport.height;

    await page.render({

        canvasContext: context,

        viewport

    }).promise;

    updatePageInfo();

}
</code></pre>
<p>Because the interactive element layer sits above the canvas, it must use the same dimensions.</p>
<pre><code class="language-javascript">const elementLayer =
    document.getElementById(
        "elementLayer"
    );

elementLayer.style.width =
    `${viewport.width}px`;

elementLayer.style.height =
    `${viewport.height}px`;
</code></pre>
<p>Add those lines inside <code>renderPage()</code> after setting the canvas dimensions.</p>
<p>The page information can then be updated:</p>
<pre><code class="language-javascript">function updatePageInfo() {

    pageInfo.textContent =
        `Page ${currentPage} of ${totalPages}`;

}
</code></pre>
<p>At this point, the uploaded PDF page is visible, but users still need a way to move through multi-page documents.</p>
<h2 id="heading-adding-pdf-page-navigation">Adding PDF Page Navigation</h2>
<p>Get the navigation controls:</p>
<pre><code class="language-javascript">const previousPage =
    document.getElementById(
        "previousPage"
    );

const nextPage =
    document.getElementById(
        "nextPage"
    );

const pageInfo =
    document.getElementById(
        "pageInfo"
    );
</code></pre>
<p>The previous button decreases the page number.</p>
<pre><code class="language-javascript">previousPage.addEventListener(
    "click",
    async () =&gt; {

        if (currentPage &lt;= 1) {
            return;
        }

        currentPage--;

        await renderPage(
            currentPage
        );

    }
);
</code></pre>
<p>The next button moves forward.</p>
<pre><code class="language-javascript">nextPage.addEventListener(
    "click",
    async () =&gt; {

        if (
            currentPage &gt;=
            totalPages
        ) {
            return;
        }

        currentPage++;

        await renderPage(
            currentPage
        );

    }
);
</code></pre>
<p>The boundary checks prevent navigation outside the document.</p>
<p>For a 12-page PDF, the interface may display:</p>
<pre><code class="language-text">Page 12 of 12
</code></pre>
<p>The previous button remains available, while the next action can be disabled because the user is already on the final page.</p>
<pre><code class="language-javascript">function updateNavigationState() {

    previousPage.disabled =
        currentPage === 1;

    nextPage.disabled =
        currentPage ===
        totalPages;

}
</code></pre>
<p>Call this function whenever a new page is rendered.</p>
<pre><code class="language-javascript">function updatePageInfo() {

    pageInfo.textContent =
        `Page ${currentPage} of ${totalPages}`;

    updateNavigationState();

}
</code></pre>
<p>The user can now upload a PDF, preview its pages, and navigate to the exact page where a signature, custom text, or document stamp needs to be placed.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/e1b4c1e5-f088-4789-af4c-7cb9bb08bdd6.png" alt="Uploaded PDF displayed in the PDF Signature Tool with previous and next page navigation controls." style="display:block;margin:0 auto" width="708" height="550" loading="lazy">

<h2 id="heading-choosing-an-element-to-add">Choosing an Element to Add</h2>
<p>Once the PDF is loaded and the correct page is visible, the user can choose what type of element to place on the document.</p>
<p>The editor provides two options:</p>
<pre><code class="language-text">Signature
Text/Stamp
</code></pre>
<p>Create the element selector:</p>
<pre><code class="language-html">&lt;div class="element-selector"&gt;

    &lt;h3&gt;1. Choose Element&lt;/h3&gt;

    &lt;label&gt;
        &lt;input
            type="radio"
            name="elementType"
            value="signature"
            checked&gt;
        Signature
    &lt;/label&gt;

    &lt;label&gt;
        &lt;input
            type="radio"
            name="elementType"
            value="text"&gt;
        Text/Stamp
    &lt;/label&gt;

&lt;/div&gt;
</code></pre>
<p>Get the controls in JavaScript:</p>
<pre><code class="language-javascript">const elementTypeInputs =
    document.querySelectorAll(
        'input[name="elementType"]'
    );

const signatureControls =
    document.getElementById(
        "signatureControls"
    );

const textControls =
    document.getElementById(
        "textControls"
    );
</code></pre>
<p>Listen for changes:</p>
<pre><code class="language-javascript">elementTypeInputs.forEach(
    input =&gt; {

        input.addEventListener(
            "change",
            event =&gt; {

                const type =
                    event.target.value;

                if (
                    type ===
                    "signature"
                ) {

                    signatureControls.hidden =
                        false;

                    textControls.hidden =
                        true;

                } else {

                    signatureControls.hidden =
                        true;

                    textControls.hidden =
                        false;

                }

            }
        );

    }
);
</code></pre>
<p>This keeps the interface focused. Signature-specific controls appear only when the user is creating a signature, while text and stamp controls appear when that element type is selected.</p>
<h2 id="heading-creating-a-signature">Creating a Signature</h2>
<p>The signature workflow supports three methods:</p>
<pre><code class="language-text">Draw
Type
Upload
</code></pre>
<p>Create the method selector:</p>
<pre><code class="language-html">&lt;div id="signatureControls"&gt;

    &lt;h3&gt;2. Create Signature&lt;/h3&gt;

    &lt;div class="signature-tabs"&gt;

        &lt;button
            data-method="draw"
            class="active"&gt;
            Draw
        &lt;/button&gt;

        &lt;button
            data-method="type"&gt;
            Type
        &lt;/button&gt;

        &lt;button
            data-method="upload"&gt;
            Upload
        &lt;/button&gt;

    &lt;/div&gt;

    &lt;div id="drawPanel"&gt;&lt;/div&gt;

    &lt;div
        id="typePanel"
        hidden&gt;
    &lt;/div&gt;

    &lt;div
        id="uploadPanel"
        hidden&gt;
    &lt;/div&gt;

&lt;/div&gt;
</code></pre>
<p>Track the currently selected method:</p>
<pre><code class="language-javascript">let signatureMethod =
    "draw";
</code></pre>
<p>Switch between the three panels:</p>
<pre><code class="language-javascript">const signatureTabs =
    document.querySelectorAll(
        ".signature-tabs button"
    );

signatureTabs.forEach(
    button =&gt; {

        button.addEventListener(
            "click",
            () =&gt; {

                signatureMethod =
                    button.dataset.method;

                showSignatureMethod(
                    signatureMethod
                );

            }
        );

    }
);
</code></pre>
<p>The panel switching function can hide the inactive methods:</p>
<pre><code class="language-javascript">function showSignatureMethod(
    method
) {

    drawPanel.hidden =
        method !== "draw";

    typePanel.hidden =
        method !== "type";

    uploadPanel.hidden =
        method !== "upload";

}
</code></pre>
<p>Each method creates the same type of final element (a signature) but the source of that signature is different.</p>
<h2 id="heading-drawing-a-signature">Drawing a Signature</h2>
<p>The <strong>Draw</strong> option allows users to create a handwritten signature directly in the browser.</p>
<p>Add a canvas to the Draw panel:</p>
<pre><code class="language-html">&lt;div id="drawPanel"&gt;

    &lt;canvas
        id="signatureCanvas"
        width="500"
        height="180"&gt;
    &lt;/canvas&gt;

    &lt;button id="clearSignature"&gt;
        Clear
    &lt;/button&gt;

&lt;/div&gt;
</code></pre>
<p>Get the drawing context:</p>
<pre><code class="language-javascript">const signatureCanvas =
    document.getElementById(
        "signatureCanvas"
    );

const signatureContext =
    signatureCanvas.getContext(
        "2d"
    );

let isDrawing = false;
</code></pre>
<p>Begin drawing when the pointer touches the canvas:</p>
<pre><code class="language-javascript">signatureCanvas.addEventListener(
    "pointerdown",
    event =&gt; {

        isDrawing = true;

        const rect =
            signatureCanvas
                .getBoundingClientRect();

        signatureContext.beginPath();

        signatureContext.moveTo(

            event.clientX -
                rect.left,

            event.clientY -
                rect.top

        );

    }
);
</code></pre>
<p>Continue the line while the pointer moves:</p>
<pre><code class="language-javascript">signatureCanvas.addEventListener(
    "pointermove",
    event =&gt; {

        if (!isDrawing) {
            return;
        }

        const rect =
            signatureCanvas
                .getBoundingClientRect();

        signatureContext.lineTo(

            event.clientX -
                rect.left,

            event.clientY -
                rect.top

        );

        signatureContext.stroke();

    }
);
</code></pre>
<p>Stop drawing when the pointer is released:</p>
<pre><code class="language-javascript">signatureCanvas.addEventListener(
    "pointerup",
    () =&gt; {

        isDrawing = false;

    }
);

signatureCanvas.addEventListener(
    "pointerleave",
    () =&gt; {

        isDrawing = false;

    }
);
</code></pre>
<p>Set a few drawing properties:</p>
<pre><code class="language-javascript">signatureContext.lineWidth = 2;

signatureContext.lineCap =
    "round";

signatureContext.lineJoin =
    "round";
</code></pre>
<p>For touch devices, prevent the browser from interpreting drawing gestures as page scrolling:</p>
<pre><code class="language-css">#signatureCanvas {
    touch-action: none;
    cursor: crosshair;
}
</code></pre>
<p>The Clear button resets the drawing canvas:</p>
<pre><code class="language-javascript">clearSignature.addEventListener(
    "click",
    () =&gt; {

        signatureContext.clearRect(

            0,
            0,

            signatureCanvas.width,
            signatureCanvas.height

        );

    }
);
</code></pre>
<p>Once the signature is ready, convert the canvas into a PNG data URL:</p>
<pre><code class="language-javascript">const drawnSignature =
    signatureCanvas.toDataURL(
        "image/png"
    );
</code></pre>
<p>Because the canvas can preserve transparency, the resulting signature can be placed over the PDF without adding an unwanted rectangular background.</p>
<p>The generated image can now be displayed inside the interactive element layer.</p>
<pre><code class="language-javascript">function useDrawnSignature() {

    const image =
        new Image();

    image.src =
        signatureCanvas.toDataURL(
            "image/png"
        );

    image.onload =
        () =&gt; {

            createSignatureElement(
                image.src
            );

        };

}
</code></pre>
<p>This gives the user a visual signature element that can later be positioned over the PDF page.</p>
<h2 id="heading-typing-a-signature">Typing a Signature</h2>
<p>Not every user has a touchscreen, stylus, or existing signature image.</p>
<p>The <strong>Type</strong> option allows users to enter their name and choose a signature-style appearance.</p>
<p>Add the input controls:</p>
<pre><code class="language-html">&lt;div id="typePanel" hidden&gt;

    &lt;input
        type="text"
        id="typedSignature"
        placeholder="Type your name"&gt;

    &lt;label for="signatureFontSize"&gt;
        Font Size
    &lt;/label&gt;

    &lt;input
        type="number"
        id="signatureFontSize"
        value="45"
        min="12"
        max="120"&gt;

    &lt;div id="signatureStyles"&gt;
    &lt;/div&gt;

&lt;/div&gt;
</code></pre>
<p>Listen for text changes:</p>
<pre><code class="language-javascript">typedSignature.addEventListener(
    "input",
    updateTypedSignatures
);
</code></pre>
<p>Create several style previews:</p>
<pre><code class="language-javascript">const signatureFonts = [

    "cursive",

    "'Brush Script MT', cursive",

    "'Segoe Script', cursive"

];
</code></pre>
<p>Render the available options:</p>
<pre><code class="language-javascript">function updateTypedSignatures() {

    const value =
        typedSignature.value.trim();

    signatureStyles.innerHTML = "";

    if (!value) {
        return;
    }

    signatureFonts.forEach(
        font =&gt; {

            const option =
                document.createElement(
                    "button"
                );

            option.textContent =
                value;

            option.style.fontFamily =
                font;

            option.style.fontSize =
                `${signatureFontSize.value}px`;

            option.addEventListener(
                "click",
                () =&gt; {

                    createTypedSignature(
                        value,
                        font
                    );

                }
            );

            signatureStyles.appendChild(
                option
            );

        }
    );

}
</code></pre>
<p>A typed signature can be converted to an image using another canvas.</p>
<pre><code class="language-javascript">function createTypedSignature(
    text,
    fontFamily
) {

    const canvas =
        document.createElement(
            "canvas"
        );

    const context =
        canvas.getContext("2d");

    const fontSize =
        Number(
            signatureFontSize.value
        );

    context.font =
        `${fontSize}px ${fontFamily}`;

    const width =
        context.measureText(
            text
        ).width;

    canvas.width =
        Math.ceil(width + 40);

    canvas.height =
        Math.ceil(fontSize * 2);

    context.font =
        `${fontSize}px ${fontFamily}`;

    context.textBaseline =
        "middle";

    context.fillText(
        text,
        20,
        canvas.height / 2
    );

    const imageUrl =
        canvas.toDataURL(
            "image/png"
        );

    createSignatureElement(
        imageUrl
    );

}
</code></pre>
<p>The typed signature is now treated like the drawn signature: it becomes an image element that can be positioned and later embedded into the PDF.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/8ebb9405-53f4-480d-b71e-cfb746f6fd5a.png" alt="Typed signature option showing a name, font size control, and multiple signature-style previews." style="display:block;margin:0 auto" width="334" height="682" loading="lazy">

<h2 id="heading-uploading-a-signature-image">Uploading a Signature Image</h2>
<p>The third option allows users to upload an existing signature image.</p>
<p>Add a file input:</p>
<pre><code class="language-html">&lt;div id="uploadPanel" hidden&gt;

    &lt;input
        type="file"
        id="signatureUpload"
        accept="image/png,image/jpeg"&gt;

    &lt;p id="selectedSignatureFile"&gt;
        No file chosen
    &lt;/p&gt;

&lt;/div&gt;
</code></pre>
<p>Listen for file selection:</p>
<pre><code class="language-javascript">signatureUpload.addEventListener(
    "change",
    event =&gt; {

        const file =
            event.target.files[0];

        if (!file) {
            return;
        }

        loadSignatureImage(file);

    }
);
</code></pre>
<p>Validate the image:</p>
<pre><code class="language-javascript">function loadSignatureImage(
    file
) {

    const allowedTypes = [

        "image/png",

        "image/jpeg"

    ];

    if (
        !allowedTypes.includes(
            file.type
        )
    ) {

        alert(
            "Please upload a PNG or JPEG image."
        );

        return;

    }

}
</code></pre>
<p>Read the selected image:</p>
<pre><code class="language-javascript">const reader =
    new FileReader();

reader.onload =
    event =&gt; {

        createSignatureElement(
            event.target.result
        );

};

reader.readAsDataURL(file);
</code></pre>
<p>Display the selected filename:</p>
<pre><code class="language-javascript">selectedSignatureFile.textContent =
    `Selected: ${file.name}`;
</code></pre>
<p>The complete function becomes:</p>
<pre><code class="language-javascript">function loadSignatureImage(
    file
) {

    const allowedTypes = [

        "image/png",

        "image/jpeg"

    ];

    if (
        !allowedTypes.includes(
            file.type
        )
    ) {

        alert(
            "Please upload a PNG or JPEG image."
        );

        return;

    }

    selectedSignatureFile.textContent =
        `Selected: ${file.name}`;

    const reader =
        new FileReader();

    reader.onload =
        event =&gt; {

            createSignatureElement(
                event.target.result
            );

        };

    reader.readAsDataURL(file);

}
</code></pre>
<p>A transparent PNG usually works particularly well because only the signature strokes remain visible over the document.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/64561974-9b33-4391-a612-9966707433a1.png" alt="Upload signature option with a selected signature image displayed and positioned on the PDF preview." style="display:block;margin:0 auto" width="345" height="532" loading="lazy">

<h2 id="heading-creating-the-signature-preview-element">Creating the Signature Preview Element</h2>
<p>All three signature methods eventually call the same function:</p>
<pre><code class="language-javascript">createSignatureElement(imageUrl);
</code></pre>
<p>This means the rest of the editor doesn't need separate positioning logic for drawn, typed, and uploaded signatures.</p>
<p>Create the preview element:</p>
<pre><code class="language-javascript">let activeElement = null;

function createSignatureElement(
    imageUrl
) {

    elementLayer.innerHTML = "";

    const image =
        document.createElement(
            "img"
        );

    image.src =
        imageUrl;

    image.className =
        "pdf-element signature-element";

    image.style.left =
        "100px";

    image.style.top =
        "100px";

    image.style.width =
        "180px";

    elementLayer.appendChild(
        image
    );

    activeElement = {

        type: "signature",

        source: imageUrl,

        element: image,

        x: 100,

        y: 100,

        scale: 1,

        rotation: 0,

        opacity: 1

    };

}
</code></pre>
<p>The preview now represents the signature that will eventually be written into the PDF.</p>
<p>The same state object can later be updated when the user changes the signature's position, scale, rotation, or opacity.</p>
<h2 id="heading-adding-text-and-preset-stamps">Adding Text and Preset Stamps</h2>
<p>The second main element type is <strong>Text/Stamp</strong>.</p>
<p>This mode is useful when a document needs a short label, status, date, or other text rather than a handwritten signature.</p>
<p>Create the controls:</p>
<pre><code class="language-html">&lt;div id="textControls" hidden&gt;

    &lt;h3&gt;2. Add Text or Stamp&lt;/h3&gt;

    &lt;input
        type="text"
        id="customText"
        placeholder="Enter text"&gt;

    &lt;div class="stamp-options"&gt;

        &lt;button data-stamp="APPROVED"&gt;
            APPROVED
        &lt;/button&gt;

        &lt;button data-stamp="CONFIDENTIAL"&gt;
            CONFIDENTIAL
        &lt;/button&gt;

        &lt;button data-stamp="DRAFT"&gt;
            DRAFT
        &lt;/button&gt;

        &lt;button data-stamp="PAID"&gt;
            PAID
        &lt;/button&gt;

    &lt;/div&gt;

    &lt;label&gt;
        Size

        &lt;input
            type="number"
            id="textSize"
            value="16"
            min="8"
            max="120"&gt;
    &lt;/label&gt;

    &lt;label&gt;
        Color

        &lt;input
            type="color"
            id="textColor"
            value="#000000"&gt;
    &lt;/label&gt;

&lt;/div&gt;
</code></pre>
<p>Custom text can be displayed as the user types:</p>
<pre><code class="language-javascript">customText.addEventListener(
    "input",
    () =&gt; {

        createTextElement(
            customText.value
        );

    }
);
</code></pre>
<p>Preset stamps can update the same text input:</p>
<pre><code class="language-javascript">const stampButtons =
    document.querySelectorAll(
        "[data-stamp]"
    );

stampButtons.forEach(
    button =&gt; {

        button.addEventListener(
            "click",
            () =&gt; {

                const stamp =
                    button.dataset.stamp;

                customText.value =
                    stamp;

                createTextElement(
                    stamp
                );

            }
        );

    }
);
</code></pre>
<p>Create the text preview:</p>
<pre><code class="language-javascript">function createTextElement(
    text
) {

    if (!text.trim()) {

        elementLayer.innerHTML = "";

        activeElement = null;

        return;

    }

    elementLayer.innerHTML = "";

    const textElement =
        document.createElement(
            "div"
        );

    textElement.className =
        "pdf-element text-element";

    textElement.textContent =
        text;

    textElement.style.left =
        "100px";

    textElement.style.top =
        "100px";

    textElement.style.fontSize =
        `${textSize.value}px`;

    textElement.style.color =
        textColor.value;

    elementLayer.appendChild(
        textElement
    );

    activeElement = {

        type: "text",

        text,

        element:
            textElement,

        x: 100,

        y: 100,

        fontSize:
            Number(
                textSize.value
            ),

        color:
            textColor.value,

        rotation: 0,

        opacity: 1

    };

}
</code></pre>
<p>When the size changes, update the current element:</p>
<pre><code class="language-javascript">textSize.addEventListener(
    "input",
    () =&gt; {

        if (
            activeElement?.type !==
            "text"
        ) {
            return;
        }

        activeElement.fontSize =
            Number(
                textSize.value
            );

        activeElement
            .element
            .style
            .fontSize =
                `${textSize.value}px`;

    }
);
</code></pre>
<p>Do the same for the color:</p>
<pre><code class="language-javascript">textColor.addEventListener(
    "input",
    () =&gt; {

        if (
            activeElement?.type !==
            "text"
        ) {
            return;
        }

        activeElement.color =
            textColor.value;

        activeElement
            .element
            .style
            .color =
                textColor.value;

    }
);
</code></pre>
<p>The user can now enter custom content such as:</p>
<pre><code class="language-text">Signed on: 08-09-2025
</code></pre>
<p>or quickly select a predefined document stamp.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/18e1bb24-c802-4a40-b7dc-87772fbdbf0c.png" alt="Text and stamp editor showing custom text, Approved, Confidential, Draft, and Paid preset options with font size and color controls." style="display:block;margin:0 auto" width="1062" height="568" loading="lazy">

<p>At this point, the application can create content using all of the available input methods: a drawn signature, typed signature, uploaded signature image, custom text, or preset document stamp.</p>
<h2 id="heading-positioning-and-styling-the-element">Positioning and Styling the Element</h2>
<p>After creating a signature, text label, or preset stamp, the next step is positioning it correctly on the PDF page.</p>
<p>The preview element sits inside the <code>elementLayer</code> created earlier. Because this layer matches the PDF canvas dimensions, users can move the element visually before anything is written into the final PDF.</p>
<p>The editor also provides controls for:</p>
<ul>
<li><p>Scale</p>
</li>
<li><p>Rotation</p>
</li>
<li><p>Opacity</p>
</li>
<li><p>X position</p>
</li>
<li><p>Y position</p>
</li>
</ul>
<p>The exact controls can vary depending on the active element. For example, scale is particularly useful for signatures, while text size and color are handled by the Text/Stamp controls from the previous section.</p>
<p>Create the placement controls:</p>
<pre><code class="language-html">&lt;div id="placementControls"&gt;

    &lt;h3&gt;3. Placement &amp; Style&lt;/h3&gt;

    &lt;label&gt;
        Rotation (°)

        &lt;input
            type="number"
            id="rotationInput"
            value="0"&gt;
    &lt;/label&gt;

    &lt;label&gt;
        Opacity

        &lt;input
            type="range"
            id="opacityInput"
            min="0"
            max="100"
            value="100"&gt;
    &lt;/label&gt;

    &lt;label&gt;
        X Position

        &lt;input
            type="number"
            id="xPosition"
            value="100"&gt;
    &lt;/label&gt;

    &lt;label&gt;
        Y Position

        &lt;input
            type="number"
            id="yPosition"
            value="100"&gt;
    &lt;/label&gt;

&lt;/div&gt;
</code></pre>
<p>For signature elements, add a scale control:</p>
<pre><code class="language-html">&lt;label&gt;
    Scale (&lt;span id="scaleValue"&gt;100%&lt;/span&gt;)

    &lt;input
        type="range"
        id="scaleInput"
        min="25"
        max="250"
        value="100"&gt;
&lt;/label&gt;
</code></pre>
<p>Get the controls in JavaScript:</p>
<pre><code class="language-javascript">const scaleInput =
    document.getElementById(
        "scaleInput"
    );

const scaleValue =
    document.getElementById(
        "scaleValue"
    );

const rotationInput =
    document.getElementById(
        "rotationInput"
    );

const opacityInput =
    document.getElementById(
        "opacityInput"
    );

const xPosition =
    document.getElementById(
        "xPosition"
    );

const yPosition =
    document.getElementById(
        "yPosition"
    );
</code></pre>
<p>We'll use a single function to update the visual transformation.</p>
<pre><code class="language-javascript">function updateElementTransform() {

    if (!activeElement) {
        return;
    }

    activeElement.element.style.transform =
        `
            scale(${activeElement.scale})
            rotate(${activeElement.rotation}deg)
        `;

    activeElement.element.style.opacity =
        activeElement.opacity;

}
</code></pre>
<p>For text elements, initialize <code>scale</code> as <code>1</code> so the same transformation function can still be used.</p>
<pre><code class="language-javascript">activeElement = {

    type: "text",

    text,

    element: textElement,

    x: 100,

    y: 100,

    scale: 1,

    rotation: 0,

    opacity: 1

};
</code></pre>
<h3 id="heading-changing-the-element-scale">Changing the Element Scale</h3>
<p>When the user moves the scale slider, convert the percentage into a decimal value.</p>
<pre><code class="language-javascript">scaleInput.addEventListener(
    "input",
    () =&gt; {

        if (!activeElement) {
            return;
        }

        const percentage =
            Number(
                scaleInput.value
            );

        activeElement.scale =
            percentage / 100;

        scaleValue.textContent =
            `${percentage}%`;

        updateElementTransform();

    }
);
</code></pre>
<p>A value of <code>100%</code> represents the original preview size.</p>
<pre><code class="language-text">50%  → 0.5
100% → 1
114% → 1.14
200% → 2
</code></pre>
<p>This makes it easy to enlarge or reduce an uploaded, drawn, or typed signature without creating a new image.</p>
<h3 id="heading-rotating-the-element">Rotating the Element</h3>
<p>The rotation input stores the angle in degrees.</p>
<pre><code class="language-javascript">rotationInput.addEventListener(
    "input",
    () =&gt; {

        if (!activeElement) {
            return;
        }

        activeElement.rotation =
            Number(
                rotationInput.value
            );

        updateElementTransform();

    }
);
</code></pre>
<p>A rotation of <code>0</code> keeps the element horizontal, while positive or negative values rotate it around its center.</p>
<h3 id="heading-adjusting-opacity">Adjusting Opacity</h3>
<p>Opacity can be useful for stamps, watermarks, and other document labels.</p>
<p>Convert the percentage slider to a value between <code>0</code> and <code>1</code>.</p>
<pre><code class="language-javascript">opacityInput.addEventListener(
    "input",
    () =&gt; {

        if (!activeElement) {
            return;
        }

        activeElement.opacity =
            Number(
                opacityInput.value
            ) / 100;

        updateElementTransform();

    }
);
</code></pre>
<p>For example:</p>
<pre><code class="language-text">100% → 1
75%  → 0.75
50%  → 0.5
</code></pre>
<p>The same opacity value will later be used when generating the final PDF.</p>
<h2 id="heading-dragging-an-element-across-the-pdf-preview">Dragging an Element Across the PDF Preview</h2>
<p>Typing X and Y coordinates manually is useful for precise adjustments, but most users will prefer to drag the element directly to the required location.</p>
<p>Track the dragging state:</p>
<pre><code class="language-javascript">let isDragging = false;

let dragOffsetX = 0;

let dragOffsetY = 0;
</code></pre>
<p>When a signature or text element is created, attach the dragging behavior.</p>
<pre><code class="language-javascript">function enableDragging(
    element
) {

    element.addEventListener(
        "pointerdown",
        event =&gt; {

            isDragging = true;

            const elementRect =
                element
                    .getBoundingClientRect();

            dragOffsetX =
                event.clientX -
                elementRect.left;

            dragOffsetY =
                event.clientY -
                elementRect.top;

            element.setPointerCapture(
                event.pointerId
            );

        }
    );

}
</code></pre>
<p>Call this function when creating an element.</p>
<pre><code class="language-javascript">enableDragging(image);
</code></pre>
<p>or:</p>
<pre><code class="language-javascript">enableDragging(textElement);
</code></pre>
<p>Next, listen for pointer movement.</p>
<pre><code class="language-javascript">elementLayer.addEventListener(
    "pointermove",
    event =&gt; {

        if (
            !isDragging ||
            !activeElement
        ) {
            return;
        }

        const layerRect =
            elementLayer
                .getBoundingClientRect();

        const x =
            event.clientX -
            layerRect.left -
            dragOffsetX;

        const y =
            event.clientY -
            layerRect.top -
            dragOffsetY;

        moveActiveElement(
            x,
            y
        );

    }
);
</code></pre>
<p>Create a reusable movement function:</p>
<pre><code class="language-javascript">function moveActiveElement(
    x,
    y
) {

    if (!activeElement) {
        return;
    }

    activeElement.x = x;

    activeElement.y = y;

    activeElement.element.style.left =
        `${x}px`;

    activeElement.element.style.top =
        `${y}px`;

    xPosition.value =
        Math.round(x);

    yPosition.value =
        Math.round(y);

}
</code></pre>
<p>Stop dragging when the pointer is released.</p>
<pre><code class="language-javascript">elementLayer.addEventListener(
    "pointerup",
    () =&gt; {

        isDragging = false;

    }
);

elementLayer.addEventListener(
    "pointercancel",
    () =&gt; {

        isDragging = false;

    }
);
</code></pre>
<p>Now the signature or text element can be moved directly over the document.</p>
<p>For example, an uploaded signature may be positioned near the bottom-right corner of the final page.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/77fa703f-4636-4423-bb05-b97fc163d6de.png" alt="Uploaded signature image positioned on a PDF page with scale, rotation, opacity, X position, and Y position controls." style="display:block;margin:0 auto" width="1047" height="532" loading="lazy">

<h2 id="heading-updating-the-position-manually">Updating the Position Manually</h2>
<p>The X and Y fields provide another way to position the element.</p>
<p>Listen for changes to the X coordinate:</p>
<pre><code class="language-javascript">xPosition.addEventListener(
    "input",
    () =&gt; {

        if (!activeElement) {
            return;
        }

        const x =
            Number(
                xPosition.value
            );

        moveActiveElement(
            x,
            activeElement.y
        );

    }
);
</code></pre>
<p>Do the same for Y:</p>
<pre><code class="language-javascript">yPosition.addEventListener(
    "input",
    () =&gt; {

        if (!activeElement) {
            return;
        }

        const y =
            Number(
                yPosition.value
            );

        moveActiveElement(
            activeElement.x,
            y
        );

    }
);
</code></pre>
<p>Dragging and manual coordinate entry remain synchronized. Moving the element updates the fields, while changing the fields moves the preview element.</p>
<h2 id="heading-keeping-the-element-inside-the-page">Keeping the Element Inside the Page</h2>
<p>Without boundaries, users could accidentally drag an element completely outside the PDF preview.</p>
<p>We can limit the position before saving it.</p>
<pre><code class="language-javascript">function clampPosition(
    x,
    y
) {

    const element =
        activeElement.element;

    const maxX =
        elementLayer.clientWidth -
        element.offsetWidth;

    const maxY =
        elementLayer.clientHeight -
        element.offsetHeight;

    return {

        x:
            Math.max(
                0,
                Math.min(x, maxX)
            ),

        y:
            Math.max(
                0,
                Math.min(y, maxY)
            )

    };

}
</code></pre>
<p>Use it inside <code>moveActiveElement()</code>:</p>
<pre><code class="language-javascript">const position =
    clampPosition(
        x,
        y
    );

activeElement.x =
    position.x;

activeElement.y =
    position.y;
</code></pre>
<p>When scale or rotation is applied, the element's transformed visual bounds can extend beyond its original box. A production editor can use <code>getBoundingClientRect()</code> for more precise transformed-boundary calculations.</p>
<p>The basic clamp shown here is sufficient to demonstrate the positioning workflow.</p>
<h2 id="heading-applying-the-element-to-selected-pages">Applying the Element to Selected Pages</h2>
<p>After positioning the element, the user decides which PDF pages should receive it.</p>
<p>The interface provides three options:</p>
<pre><code class="language-text">Current page only
All pages
Specific pages
</code></pre>
<p>Create the controls:</p>
<pre><code class="language-html">&lt;div id="pageApplication"&gt;

    &lt;h3&gt;4. Apply to Pages&lt;/h3&gt;

    &lt;label&gt;
        &lt;input
            type="radio"
            name="applyMode"
            value="current"
            checked&gt;
        Current page only
    &lt;/label&gt;

    &lt;label&gt;
        &lt;input
            type="radio"
            name="applyMode"
            value="all"&gt;
        All pages
    &lt;/label&gt;

    &lt;label&gt;
        &lt;input
            type="radio"
            name="applyMode"
            value="specific"&gt;
        Specific pages
    &lt;/label&gt;

    &lt;input
        type="text"
        id="specificPages"
        placeholder="e.g., 1, 3-5, 10"&gt;

&lt;/div&gt;
</code></pre>
<p>Read the selected mode:</p>
<pre><code class="language-javascript">function getTargetPages() {

    const mode =
        document.querySelector(
            'input[name="applyMode"]:checked'
        ).value;

    if (
        mode ===
        "current"
    ) {

        return [
            currentPage
        ];

    }

    if (
        mode ===
        "all"
    ) {

        return Array.from(

            {
                length:
                    totalPages
            },

            (_, index) =&gt;
                index + 1

        );

    }

    return parsePageRange(
        specificPages.value
    );

}
</code></pre>
<p>Parse custom values such as:</p>
<pre><code class="language-text">1, 3-5, 10
</code></pre>
<p>with:</p>
<pre><code class="language-javascript">function parsePageRange(
    value
) {

    const pages =
        new Set();

    value
        .split(",")
        .forEach(part =&gt; {

            const item =
                part.trim();

            if (!item) {
                return;
            }

            if (
                item.includes("-")
            ) {

                const [
                    start,
                    end
                ] =
                    item
                        .split("-")
                        .map(Number);

                for (
                    let page = start;
                    page &lt;= end;
                    page++
                ) {

                    if (
                        page &gt;= 1 &amp;&amp;
                        page &lt;= totalPages
                    ) {

                        pages.add(page);

                    }

                }

            } else {

                const page =
                    Number(item);

                if (
                    page &gt;= 1 &amp;&amp;
                    page &lt;= totalPages
                ) {

                    pages.add(page);

                }

            }

        });

    return [...pages];

}
</code></pre>
<p>The result becomes:</p>
<pre><code class="language-javascript">[
    1,
    3,
    4,
    5,
    10
]
</code></pre>
<p>This allows a signature or stamp to be placed once and then applied to multiple target pages.</p>
<p>Just keep in mind that page dimensions may differ within the same PDF. Applying the same coordinates across pages works best when those pages use a consistent size and layout.</p>
<h2 id="heading-applying-and-finalizing-the-pdf">Applying and Finalizing the PDF</h2>
<p>Once the element is created, positioned, styled, and assigned to the correct pages, the user can click <strong>Apply &amp; Finalize</strong>.</p>
<p>Create the action buttons:</p>
<pre><code class="language-html">&lt;div class="editor-actions"&gt;

    &lt;button id="applyButton"&gt;
        Apply &amp; Finalize
    &lt;/button&gt;

    &lt;button id="startOverButton"&gt;
        Start Over
    &lt;/button&gt;

&lt;/div&gt;
</code></pre>
<p>Get the buttons:</p>
<pre><code class="language-javascript">const applyButton =
    document.getElementById(
        "applyButton"
    );

const startOverButton =
    document.getElementById(
        "startOverButton"
    );
</code></pre>
<p>Before generating the final PDF, make sure an element exists.</p>
<pre><code class="language-javascript">applyButton.addEventListener(
    "click",
    async () =&gt; {

        if (!activeElement) {

            alert(
                "Please add a signature, text, or stamp first."
            );

            return;

        }

        const targetPages =
            getTargetPages();

        if (
            targetPages.length === 0
        ) {

            alert(
                "Please select at least one valid page."
            );

            return;

        }

        await generateFinalPdf(
            targetPages
        );

    }
);
</code></pre>
<p>The <code>generateFinalPdf()</code> function will handle the actual PDF modification in the next section.</p>
<p>The <strong>Start Over</strong> button clears the current document and resets the application.</p>
<pre><code class="language-javascript">startOverButton.addEventListener(
    "click",
    resetTool
);
</code></pre>
<p>Create the reset function:</p>
<pre><code class="language-javascript">function resetTool() {

    pdfDocument = null;

    originalPdfBytes = null;

    currentPage = 1;

    totalPages = 0;

    activeElement = null;

    pdfInput.value = "";

    elementLayer.innerHTML = "";

    editorSection.hidden = true;

    resultSection.hidden = true;

    uploadSection.hidden = false;

}
</code></pre>
<p>This returns the application to its original upload state.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/e98a951a-609b-4e1e-b2a6-5cd2e31a6ac1.png" alt="Apply and Finalize button for adding the selected signature or text element to the PDF, with a Start Over option." style="display:block;margin:0 auto" width="362" height="86" loading="lazy">

<p>The interactive editing stage is now complete. Users can create a signature or text element, position it directly over the PDF, adjust its appearance, choose the target pages, and prepare the document for final processing.</p>
<h2 id="heading-generating-the-signed-pdf">Generating the Signed PDF</h2>
<p>The element shown over the browser preview hasn't yet been added to the actual PDF. When users click <strong>Apply &amp; Finalize</strong>, the application loads the original document with PDF-lib and writes the selected signature, text, or stamp onto the target pages.</p>
<p>Start by loading the original PDF bytes:</p>
<pre><code class="language-javascript">async function generateFinalPdf(
    targetPages
) {

    const pdfDoc =
        await PDFLib.PDFDocument.load(
            originalPdfBytes.slice()
        );

    const pages =
        pdfDoc.getPages();

}
</code></pre>
<p>Before placing the element, we need to convert its browser coordinates into PDF coordinates.</p>
<p>The preview canvas may be displayed at a different size from the actual PDF page. The coordinate systems also use different Y-axis origins.</p>
<p>For each target page, calculate the scale:</p>
<pre><code class="language-javascript">const {
    width: pdfWidth,
    height: pdfHeight
} = page.getSize();

const scaleX =
    pdfWidth /
    pdfCanvas.width;

const scaleY =
    pdfHeight /
    pdfCanvas.height;
</code></pre>
<p>Convert the preview position:</p>
<pre><code class="language-javascript">const pdfX =
    activeElement.x *
    scaleX;

const pdfY =
    pdfHeight -
    (
        activeElement.y +
        activeElement.element.offsetHeight
    ) * scaleY;
</code></pre>
<p>This conversion maps the element from the browser's top-left coordinate system to the PDF page's coordinate system.</p>
<h3 id="heading-embedding-a-signature">Embedding a Signature</h3>
<p>Drawn, typed, and uploaded signatures are all represented as images by the time they reach the final processing stage.</p>
<p>Convert the signature data URL into bytes:</p>
<pre><code class="language-javascript">async function dataUrlToBytes(
    dataUrl
) {

    const response =
        await fetch(dataUrl);

    return await response.arrayBuffer();

}
</code></pre>
<p>Embed the signature image:</p>
<pre><code class="language-javascript">const signatureBytes =
    await dataUrlToBytes(
        activeElement.source
    );

const signatureImage =
    await pdfDoc.embedPng(
        signatureBytes
    );
</code></pre>
<p>If uploaded JPEG signatures are supported, the application should preserve the original image format and use <code>embedJpg()</code> when appropriate.</p>
<p>Calculate the final dimensions:</p>
<pre><code class="language-javascript">const previewWidth =
    activeElement
        .element
        .offsetWidth *
    activeElement.scale;

const previewHeight =
    activeElement
        .element
        .offsetHeight *
    activeElement.scale;

const finalWidth =
    previewWidth *
    scaleX;

const finalHeight =
    previewHeight *
    scaleY;
</code></pre>
<p>Then draw the signature:</p>
<pre><code class="language-javascript">page.drawImage(
    signatureImage,
    {
        x: pdfX,

        y:
            pdfHeight -
            (
                activeElement.y *
                scaleY
            ) -
            finalHeight,

        width:
            finalWidth,

        height:
            finalHeight,

        rotate:
            PDFLib.degrees(
                activeElement.rotation
            ),

        opacity:
            activeElement.opacity
    }
);
</code></pre>
<p>The same processing logic works whether the signature was drawn, typed, or uploaded because all three methods produce an image element before finalization.</p>
<h3 id="heading-adding-text-or-a-stamp">Adding Text or a Stamp</h3>
<p>Text and preset stamps are written directly onto the PDF page.</p>
<p>First, convert the selected color from hexadecimal to RGB values.</p>
<pre><code class="language-javascript">function hexToRgb(
    hex
) {

    const value =
        hex.replace(
            "#",
            ""
        );

    return {

        r:
            parseInt(
                value.substring(0, 2),
                16
            ) / 255,

        g:
            parseInt(
                value.substring(2, 4),
                16
            ) / 255,

        b:
            parseInt(
                value.substring(4, 6),
                16
            ) / 255

    };

}
</code></pre>
<p>Apply the text:</p>
<pre><code class="language-javascript">const color =
    hexToRgb(
        activeElement.color
    );

page.drawText(
    activeElement.text,
    {
        x:
            activeElement.x *
            scaleX,

        y:
            pdfHeight -
            (
                activeElement.y *
                scaleY
            ) -
            activeElement.fontSize,

        size:
            activeElement.fontSize *
            scaleY,

        color:
            PDFLib.rgb(
                color.r,
                color.g,
                color.b
            ),

        rotate:
            PDFLib.degrees(
                activeElement.rotation
            ),

        opacity:
            activeElement.opacity
    }
);
</code></pre>
<p>After processing every target page, save the modified document:</p>
<pre><code class="language-javascript">const finalPdfBytes =
    await pdfDoc.save();

const finalPdfBlob =
    new Blob(
        [finalPdfBytes],
        {
            type:
                "application/pdf"
        }
    );

await showFinalPreview(
    finalPdfBlob
);
</code></pre>
<p>At this point, the selected signature, text, or stamp has been added to the generated PDF.</p>
<h2 id="heading-previewing-the-final-pdf">Previewing the Final PDF</h2>
<p>Before downloading the document, the application displays the completed PDF in a separate preview area.</p>
<p>This allows users to confirm that the element appears on the correct page and in the expected position.</p>
<p>Load the generated file with PDF.js:</p>
<pre><code class="language-javascript">let finalPdfDocument = null;

let finalPage = 1;

async function showFinalPreview(
    blob
) {

    const bytes =
        await blob.arrayBuffer();

    finalPdfDocument =
        await pdfjsLib
            .getDocument({
                data: bytes
            })
            .promise;

    finalPage = 1;

    editorSection.hidden =
        true;

    resultSection.hidden =
        false;

    await renderFinalPage(
        finalPage
    );

}
</code></pre>
<p>Render the current result page:</p>
<pre><code class="language-javascript">async function renderFinalPage(
    pageNumber
) {

    const page =
        await finalPdfDocument
            .getPage(
                pageNumber
            );

    const viewport =
        page.getViewport({
            scale: 1.4
        });

    finalCanvas.width =
        viewport.width;

    finalCanvas.height =
        viewport.height;

    await page.render({

        canvasContext:
            finalCanvas
                .getContext("2d"),

        viewport

    }).promise;

    finalPageInfo.textContent =
        `Page ${pageNumber} of ${finalPdfDocument.numPages}`;

}
</code></pre>
<p>Previous and next controls can use the same navigation pattern as the original PDF preview.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/8ca71960-392a-4c49-a138-f462786682a0.png" alt="Final PDF preview showing the applied signature or text element before downloading the document." style="display:block;margin:0 auto" width="704" height="547" loading="lazy">

<h2 id="heading-renaming-and-downloading-the-final-pdf">Renaming and Downloading the Final PDF</h2>
<p>After reviewing the processed document, users can rename the file before downloading it.</p>
<p>For example:</p>
<pre><code class="language-text">document_signed.pdf
</code></pre>
<p>Create the filename input:</p>
<pre><code class="language-html">&lt;input
    type="text"
    id="outputFilename"
    value="document_signed.pdf"&gt;
</code></pre>
<p>Make sure the filename has the correct extension:</p>
<pre><code class="language-javascript">function getOutputFilename() {

    let filename =
        outputFilename
            .value
            .trim();

    if (!filename) {

        filename =
            "document_signed.pdf";

    }

    if (
        !filename
            .toLowerCase()
            .endsWith(".pdf")
    ) {

        filename += ".pdf";

    }

    return filename;

}
</code></pre>
<p>The result section can also display the total page count and generated file size.</p>
<pre><code class="language-javascript">function formatFileSize(
    bytes
) {

    if (
        bytes &lt;
        1024 * 1024
    ) {

        return (
            bytes / 1024
        ).toFixed(2) + " KB";

    }

    return (
        bytes /
        1024 /
        1024
    ).toFixed(2) + " MB";

}
</code></pre>
<p>Update the file information:</p>
<pre><code class="language-javascript">filePageCount.textContent =
    `Total Pages: ${finalPdfDocument.numPages}`;

fileSize.textContent =
    `File Size: ${
        formatFileSize(
            finalPdfBlob.size
        )
    }`;
</code></pre>
<p>Download the file using a temporary object URL:</p>
<pre><code class="language-javascript">downloadButton.addEventListener(
    "click",
    () =&gt; {

        const url =
            URL.createObjectURL(
                finalPdfBlob
            );

        const link =
            document.createElement(
                "a"
            );

        link.href =
            url;

        link.download =
            getOutputFilename();

        link.click();

        URL.revokeObjectURL(
            url
        );

    }
);
</code></pre>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/96212b85-3504-4420-bfba-819c20ca5a6e.png" alt="Signed PDF download section with editable filename, total page count, file size, and Download button." style="display:block;margin:0 auto" width="355" height="280" loading="lazy">

<p>After downloading, the <strong>Start Over</strong> button resets the application so another PDF can be processed.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/fd08536f-4b47-4b7b-930c-5943685ad58e.png" alt="Start Over button for clearing the current PDF signing session and uploading another document." style="display:block;margin:0 auto" width="150" height="54" loading="lazy">

<h2 id="heading-demo-how-the-pdf-signature-tool-works">Demo: How the PDF Signature Tool Works</h2>
<p>Let's walk through the complete workflow from upload to download.</p>
<h3 id="heading-step-1-upload-the-pdf">Step 1: Upload the PDF</h3>
<p>Users begin by dragging a PDF into the upload area or clicking <strong>Select PDF</strong>.</p>
<p>The browser reads the document and prepares it for local processing.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/aa49b177-d750-4432-80db-51101eaa5650.png" alt="PDF Signature Tool upload area with drag-and-drop support and Select PDF button." style="display:block;margin:0 auto" width="639" height="652" loading="lazy">

<h3 id="heading-step-2-preview-and-navigate-the-document">Step 2: Preview and Navigate the Document</h3>
<p>After upload, the current page appears in the PDF preview.</p>
<p>Previous and next controls allow users to navigate through multi-page documents and find the page where an element needs to be added.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/61827efd-9872-4499-8d61-96f7a7c6a821.png" alt="Uploaded PDF preview with previous and next page navigation controls." style="display:block;margin:0 auto" width="708" height="550" loading="lazy">

<h3 id="heading-step-3-choose-what-to-add">Step 3: Choose What to Add</h3>
<p>The user chooses between <strong>Signature</strong> and <strong>Text/Stamp</strong>.</p>
<p>This determines which creation controls appear in the editor.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/ba697ba0-2e86-4c35-9510-39503e706ac8.png" alt="PDF editing controls for choosing between a signature and a text or stamp element." style="display:block;margin:0 auto" width="342" height="755" loading="lazy">

<h3 id="heading-step-4-create-the-signature">Step 4: Create the Signature</h3>
<p>If Signature is selected, users can choose <strong>Draw</strong>, <strong>Type</strong>, or <strong>Upload</strong>.</p>
<p>Drawing works directly inside the signature canvas. The Type option creates a signature-style element from entered text, while Upload accepts an existing signature image.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/971b15a8-4322-45c3-83fb-cbb8eae3172d.png" alt="PDF signature creation controls with Draw, Type, and Upload options." style="display:block;margin:0 auto" width="543" height="350" loading="lazy">

<h3 id="heading-step-5-add-custom-text-or-a-preset-stamp">Step 5: Add Custom Text or a Preset Stamp</h3>
<p>Instead of a signature, users can select <strong>Text/Stamp</strong>.</p>
<p>They can enter custom content or choose a preset such as <strong>APPROVED</strong>, <strong>CONFIDENTIAL</strong>, <strong>DRAFT</strong>, or <strong>PAID</strong>.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/572bbac7-2b4d-49bd-8cae-399ca82c74b7.png" alt="Text and stamp controls with custom text and preset document stamp options." style="display:block;margin:0 auto" width="560" height="423" loading="lazy">

<h3 id="heading-step-6-position-and-style-the-element">Step 6: Position and Style the Element</h3>
<p>The created element appears over the PDF preview.</p>
<p>Users can drag it to the required position and adjust properties such as scale, rotation, opacity, X position, and Y position.</p>
<p>Text elements also support configurable font size and color.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/d3424bda-ada1-4acd-8ce8-c55a631efc27.png" alt="Signature positioned over a PDF page with placement and styling controls." style="display:block;margin:0 auto" width="1047" height="532" loading="lazy">

<h3 id="heading-step-7-choose-the-target-pages">Step 7: Choose the Target Pages</h3>
<p>The element can be applied to the current page, every page, or a specific page selection.</p>
<p>For example:</p>
<pre><code class="language-text">1, 3-5, 10
</code></pre>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/10b0061f-57f2-4314-a7cb-41758bc00be6.png" alt="Choose the Target Pages or applied pages" style="display:block;margin:0 auto" width="329" height="252" loading="lazy">

<p>This is useful when the same stamp or document label needs to appear on several pages.</p>
<h3 id="heading-step-8-apply-and-finalize">Step 8: Apply and Finalize</h3>
<p>After checking the element and target pages, users click <strong>Apply &amp; Finalize</strong>.</p>
<p>The browser converts the preview position into PDF coordinates and generates the modified document.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/8ad34b20-777d-49c9-8900-4876e22ac8c1.png" alt="Apply and Finalize button for generating the PDF with the selected signature or text element." style="display:block;margin:0 auto" width="204" height="73" loading="lazy">

<h3 id="heading-step-9-preview-the-completed-pdf">Step 9: Preview the Completed PDF</h3>
<p>The generated document appears in a final preview.</p>
<p>Users can navigate through the pages and verify that the signature, text, or stamp appears correctly before downloading.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/f7d699e6-bbb3-4692-b2bf-d2e1bb44bdc5.png" alt=" Completed PDF preview showing an applied signature before download." style="display:block;margin:0 auto" width="704" height="547" loading="lazy">

<h3 id="heading-step-10-rename-and-download">Step 10: Rename and Download</h3>
<p>The final section allows users to change the output filename and review the total number of pages and file size.</p>
<p>Clicking <strong>Download</strong> saves the generated PDF locally.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/58186c5d-7f10-4860-b2c3-87f5862bd025.png" alt="Final PDF download section with filename editing, page count, file size, and Download button." style="display:block;margin:0 auto" width="355" height="280" loading="lazy">

<p>Afterward, <strong>Start Over</strong> clears the session and returns to the upload interface.</p>
<h2 id="heading-handling-signature-transparency">Handling Signature Transparency</h2>
<p>Uploaded signatures often look best when the background is transparent.</p>
<p>A transparent PNG contains only the visible signature strokes, allowing the original PDF content to remain visible around the signature.</p>
<p>A JPEG image, by comparison, usually includes a solid background. If the image was scanned from white paper, placing it on a colored PDF area may create a visible white rectangle.</p>
<p>For uploaded signatures, transparent PNG files are therefore usually the better option.</p>
<p>The same principle applies to drawn and typed signatures. When converting a canvas to PNG, avoid filling the canvas with a background color unless that background is intentionally required.</p>
<pre><code class="language-javascript">const signatureImage =
    signatureCanvas.toDataURL(
        "image/png"
    );
</code></pre>
<p>The transparent canvas can then be embedded directly into the PDF.</p>
<h2 id="heading-important-notes-and-common-mistakes">Important Notes and Common Mistakes</h2>
<p>One common mistake is assuming that the browser preview and the actual PDF use identical coordinates.</p>
<p>Always calculate the relationship between the canvas dimensions and the target PDF page before placing the final element.</p>
<pre><code class="language-javascript">const scaleX =
    pdfWidth /
    pdfCanvas.width;

const scaleY =
    pdfHeight /
    pdfCanvas.height;
</code></pre>
<p>Another issue occurs when the same element is applied to pages with different dimensions. A position that looks correct on an A4 page may not appear in the same visual location on a landscape or differently sized page.</p>
<p>Uploaded signature images should also be validated before processing.</p>
<pre><code class="language-javascript">const allowedTypes = [
    "image/png",
    "image/jpeg"
];

if (
    !allowedTypes.includes(
        file.type
    )
) {

    alert(
        "Please upload a PNG or JPEG image."
    );

    return;

}
</code></pre>
<p>Very large image files should be resized before embedding to avoid unnecessarily increasing the final PDF size.</p>
<p>Users should also review the completed document before downloading it. Rotation, scaling, or coordinate conversion errors are much easier to identify in the final preview than after the file has already been shared.</p>
<p>Finally, remember that this project adds a <strong>visual electronic signature</strong> to a PDF. It does not create a certificate-based cryptographic digital signature or provide automatic identity verification.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In this tutorial, you built a browser-based PDF Signature Tool using JavaScript.</p>
<p>You learned how to upload and preview PDF documents, navigate between pages, create signatures by drawing, typing, or uploading an image, add custom text and preset stamps, position elements directly over a PDF preview, adjust their appearance, choose target pages, and generate the completed document with PDF-lib.</p>
<p>You also learned how browser coordinates are converted into PDF coordinates and why signature transparency matters when embedding images into a document.</p>
<p>The final workflow allows users to preview the completed PDF, rename the output file, review its page count and size, and download it directly from the browser.</p>
<p>You can explore the complete workflow using the <a href="https://allinonetools.net/sign-pdf/">PDF Signature Tool</a>.</p>
<p>The project can be extended further with multiple elements per page, reusable signature profiles, date fields, initials, custom fonts, signature removal before finalization, or certificate-based digital signing through a dedicated signing infrastructure.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Build a RAG Chatbot for Your Docs with Node.js, Google Gemini, and pgvector ]]>
                </title>
                <description>
                    <![CDATA[ I was helping a team that had a 200-page API documentation PDF. Every new engineer spent their first two weeks Ctrl+F-ing through it, asking the same questions in Slack, getting redirected to the same ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-build-rag-chatbot-nodejs-gemini-pgvector/</link>
                <guid isPermaLink="false">6a57a6aa328507d0d4d46169</guid>
                
                    <category>
                        <![CDATA[ Node.js ]]>
                    </category>
                
                    <category>
                        <![CDATA[ AI ]]>
                    </category>
                
                    <category>
                        <![CDATA[ PostgreSQL ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Tutorial ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ RAG  ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Zia Ullah ]]>
                </dc:creator>
                <pubDate>Wed, 15 Jul 2026 15:26:34 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/uploads/covers/5e1e335a7a1d3fcc59028c64/9aa3d8d3-9c51-42a7-8e78-907802394ea1.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>I was helping a team that had a 200-page API documentation PDF. Every new engineer spent their first two weeks Ctrl+F-ing through it, asking the same questions in Slack, getting redirected to the same paragraphs on page 47.</p>
<p>The doc was accurate. It was even well-written. But nobody could find anything in it fast enough for it to be useful.</p>
<p>That's the problem RAG, or Retrieval-Augmented Generation, solves.</p>
<p>The naïve approach is to stuff your entire PDF into a prompt and let the model figure it out. That breaks down fast: context windows overflow, costs spike on every request, and the model loses the thread somewhere in the wall of text.</p>
<p>RAG takes a different approach. Your documents get broken into small chunks upfront. Ask it a question and it digs out the 3 or 4 chunks that best match it — those are what the model actually sees. The model gets a tight, focused context. The answer comes from what your document actually says — not from whatever the LLM memorized during training.</p>
<p>In this tutorial, you'll build that from scratch. Upload any PDF — an API reference, an internal spec, a research paper — and ask questions about it in plain English. The system finds the relevant sections and answers from the document itself, not from general training data.</p>
<p>The stack: Node.js with Express, Google Gemini for embeddings, Groq for text generation, and pgvector running in Docker. Every piece of it is free — no credit card, no trial period.</p>
<p>The complete code is on GitHub at <a href="https://github.com/ziaongit/nodejs-rag-chatbot">nodejs-rag-chatbot</a>.</p>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ul>
<li><p><a href="#heading-how-rag-works">How RAG Works</a></p>
</li>
<li><p><a href="#heading-what-were-building">What We're Building</a></p>
</li>
<li><p><a href="#heading-prerequisites">Prerequisites</a></p>
</li>
<li><p><a href="#heading-project-setup">Project Setup</a></p>
</li>
<li><p><a href="#heading-set-up-postgres-with-pgvector-using-docker">Set Up Postgres with pgvector Using Docker</a></p>
</li>
<li><p><a href="#heading-connect-to-the-database">Connect to the Database</a></p>
</li>
<li><p><a href="#heading-build-the-ingestion-pipeline">Build the Ingestion Pipeline</a></p>
</li>
<li><p><a href="#heading-build-the-query-pipeline">Build the Query Pipeline</a></p>
</li>
<li><p><a href="#heading-build-the-chat-api-with-express">Build the Chat API with Express</a></p>
</li>
<li><p><a href="#heading-test-the-chatbot">Test the Chatbot</a></p>
</li>
<li><p><a href="#heading-troubleshooting">Troubleshooting</a></p>
</li>
<li><p><a href="#heading-how-to-swap-in-openai">How to Swap in OpenAI</a></p>
</li>
<li><p><a href="#heading-what-to-build-next">What to Build Next</a></p>
</li>
</ul>
<h2 id="heading-how-rag-works">How RAG Works</h2>
<p>RAG has two phases, and the code maps directly to both.</p>
<p><strong>Ingestion phase</strong> — runs once when you upload a document:</p>
<ol>
<li><p>Pull the raw text out of the PDF</p>
</li>
<li><p>Break it into chunks of 400 to 600 characters each, with a bit of overlap so nothing important gets cut at a boundary</p>
</li>
<li><p>Run each chunk through an embedding model, which turns it into a vector (a long list of numbers that captures what the text means)</p>
</li>
<li><p>Store each chunk and its vector in Postgres</p>
</li>
</ol>
<p><strong>Query phase</strong> — runs every time someone asks a question:</p>
<ol>
<li><p>Embed the user's question using the same model</p>
</li>
<li><p>Search the database for chunks whose vectors are closest to the question vector</p>
</li>
<li><p>Take the top 5 matching chunks and assemble them into a context block</p>
</li>
<li><p>Send <code>context + question</code> to the LLM and return its answer</p>
</li>
</ol>
<p>The reason this works better than keyword search: the embedding model captures <em>meaning</em>, not just exact words. If your doc says "terminate the process" and the user asks "how do I stop it?", vector similarity finds that match. Regular string matching doesn't.</p>
<p>One thing that trips people up: you must use the same embedding model at query time as you did at ingestion. The model defines the geometric space those vectors live in. Switch models halfway through and the coordinates stop meaning the same thing — you'd be comparing apples to completely different apples.</p>
<h2 id="heading-what-were-building">What We're Building</h2>
<p>The architecture is intentionally minimal: two endpoints, with nothing you don't need:</p>
<ul>
<li><p><code>POST /ingest</code>: accepts a PDF upload, chunks it, embeds each chunk, stores vectors in pgvector</p>
</li>
<li><p><code>POST /chat</code>: accepts a question, retrieves the most relevant chunks, returns an LLM-generated answer</p>
</li>
</ul>
<p>The full tech stack:</p>
<ul>
<li><p><strong>Node.js + Express</strong> — API layer</p>
</li>
<li><p><strong>Google Gemini free API</strong> — <code>gemini-embedding-001</code> for embeddings (3,072 dimensions per chunk)</p>
</li>
<li><p><strong>Groq free API</strong> — <code>llama-3.1-8b-instant</code> for text generation</p>
</li>
<li><p><strong>PostgreSQL + pgvector</strong> — vector storage and cosine similarity search, running in Docker</p>
</li>
<li><p><strong>pdf-parse</strong> — extracts raw text from PDF buffers</p>
</li>
</ul>
<p>Gemini handles embeddings and Groq handles generation. Splitting them across two providers isn't arbitrary. Gemini's generation API has a quota limit of zero in certain regions (including Pakistan), while Groq works everywhere with no restrictions. Using Groq for generation means this tutorial runs the same way regardless of where you are.</p>
<h2 id="heading-prerequisites">Prerequisites</h2>
<p>Before you start:</p>
<ul>
<li><p>Node.js 20+ installed on your machine</p>
</li>
<li><p>Docker Desktop running (this is how we'll run Postgres locally)</p>
</li>
<li><p>A free Google Gemini API key (for embeddings)</p>
</li>
<li><p>A free Groq API key (for text generation)</p>
</li>
</ul>
<h3 id="heading-how-to-get-your-free-gemini-api-key">How to Get Your Free Gemini API Key</h3>
<ol>
<li><p>Go to <a href="https://aistudio.google.com/app/apikey">aistudio.google.com/app/apikey</a> and sign in with a Google account</p>
</li>
<li><p>Click "Create API key"</p>
</li>
<li><p>Select "Create API key in new project"</p>
</li>
<li><p>Copy the key — it starts with <code>AIzaSy...</code></p>
</li>
</ol>
<p>No credit card or billing required.</p>
<h3 id="heading-how-to-get-your-free-groq-api-key">How to Get Your Free Groq API Key</h3>
<ol>
<li><p>Go to <a href="https://console.groq.com">console.groq.com</a> and sign up with Google</p>
</li>
<li><p>Click "API Keys" in the left sidebar</p>
</li>
<li><p>Click "Create API Key", give it a name, copy the key — it starts with <code>gsk_...</code></p>
</li>
</ol>
<p>Groq is free with generous rate limits and works in all regions.</p>
<h2 id="heading-project-setup">Project Setup</h2>
<p>Create the project directory and initialize it:</p>
<pre><code class="language-bash">mkdir nodejs-rag-chatbot
cd nodejs-rag-chatbot
npm init -y
</code></pre>
<p>Install dependencies:</p>
<pre><code class="language-bash">npm install express pg pdf-parse uuid dotenv multer
npm install --save-dev nodemon
</code></pre>
<p>A quick note on the packages: <code>multer</code> is what makes file uploads work on the <code>/ingest</code> endpoint. Without it, Express can't parse multipart form data.</p>
<p><code>pdf-parse</code> does the heavy lifting on PDFs, though watch out for scanned PDFs. Those are just images with no text layer underneath, so you'll get back an empty string.</p>
<p><code>pg</code> talks to Postgres, <code>uuid</code> gives each row a unique ID, and <code>dotenv</code> loads your keys before the app does anything.</p>
<p>Create a <code>.env</code> in the project root. It needs seven values:</p>
<pre><code class="language-plaintext">GEMINI_API_KEY=AIzaSy...         ← your Gemini key from Google AI Studio
GROQ_API_KEY=gsk_...             ← your Groq key from console.groq.com
POSTGRES_USER=rag_user
POSTGRES_PASSWORD=rag_pass       ← choose any password, this is local only
POSTGRES_DB=rag_db
DATABASE_URL=postgresql://rag_user:rag_pass@localhost:5432/rag_db
PORT=3000
</code></pre>
<p>One thing: the password in <code>POSTGRES_PASSWORD</code> and the one in <code>DATABASE_URL</code> must match exactly. I changed just one of them once and spent way too long debugging a "password authentication failed" error before realising the two values were out of sync.</p>
<p>Update <code>package.json</code> scripts:</p>
<pre><code class="language-json">"scripts": {
  "start": "node src/index.js",
  "dev": "nodemon src/index.js"
}
</code></pre>
<p>Create the <code>src</code> directory:</p>
<pre><code class="language-bash">mkdir src
</code></pre>
<p>Your final folder structure will look like this:</p>
<pre><code class="language-plaintext">nodejs-rag-chatbot/
├── src/
│   ├── index.js        ← Express app entry point
│   ├── db.js           ← Postgres connection and schema setup
│   ├── embeddings.js   ← Gemini embedding + Groq generation
│   ├── ingest.js       ← Document ingestion pipeline
│   └── query.js        ← RAG query pipeline
├── docker-compose.yml
├── .env
└── package.json
</code></pre>
<h2 id="heading-set-up-postgres-with-pgvector-using-docker">Set Up Postgres with pgvector Using Docker</h2>
<p>pgvector adds a <code>vector</code> column type to Postgres and the operators needed to search it by similarity. Normally you'd have to install it yourself, but the <code>pgvector/pgvector</code> Docker image ships with it already baked in. Just pull the image and you're good.</p>
<p>Now add <code>docker-compose.yml</code> to the project root:</p>
<pre><code class="language-yaml">services:
  postgres:
    image: pgvector/pgvector:pg16
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
    ports:
      - "5432:5432"
    volumes:
      - pgdata:/var/lib/postgresql/data

volumes:
  pgdata:
</code></pre>
<p>Those <code>${VARIABLE}</code> references get swapped out from <code>.env</code> when Compose starts — so <code>docker-compose.yml</code> itself stays clean. This is worth doing from day one. I've seen people skip this and regret it after a repo goes public.</p>
<p>Start it:</p>
<pre><code class="language-bash">docker compose up -d
</code></pre>
<h2 id="heading-connect-to-the-database">Connect to the Database</h2>
<p>Create <code>src/db.js</code>. This sets up the connection pool and creates the <code>documents</code> table on first run:</p>
<pre><code class="language-javascript">const { Pool } = require('pg');

const pool = new Pool({
  connectionString: process.env.DATABASE_URL,
});

async function initDb() {
  await pool.query(`CREATE EXTENSION IF NOT EXISTS vector`);

  await pool.query(`
    CREATE TABLE IF NOT EXISTS documents (
      id UUID PRIMARY KEY,
      content TEXT NOT NULL,
      source TEXT NOT NULL,
      embedding VECTOR(3072)
    )
  `);

  console.log('Database ready');
}

module.exports = { pool, initDb };
</code></pre>
<p>The <code>VECTOR(3072)</code> dimension matches the output of Gemini's <code>gemini-embedding-001</code> model exactly. If you use a different embedding model in the future, check its output dimensions and update this number to match.</p>
<h2 id="heading-build-the-ingestion-pipeline">Build the Ingestion Pipeline</h2>
<p>Start with <code>embeddings.js</code>. This file is the bridge to both external APIs — Gemini for turning text into vectors, Groq for generating the final answer. Keeping both in one place means a single file to touch if you ever swap providers.</p>
<p><strong>src/embeddings.js:</strong></p>
<pre><code class="language-javascript">const GEMINI_KEY = process.env.GEMINI_API_KEY;
const GEMINI_BASE = 'https://generativelanguage.googleapis.com/v1/models';

async function embedText(text) {
  const res = await fetch(
    `${GEMINI_BASE}/gemini-embedding-001:embedContent?key=${GEMINI_KEY}`,
    {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ content: { parts: [{ text }] } }),
    }
  );
  const data = await res.json();
  if (!res.ok) throw new Error(JSON.stringify(data));
  return data.embedding.values;
}

async function generateAnswer(context, question) {
  const res = await fetch(
    'https://api.groq.com/openai/v1/chat/completions',
    {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${process.env.GROQ_API_KEY}`,
      },
      body: JSON.stringify({
        model: 'llama-3.1-8b-instant',
        messages: [
          {
            role: 'system',
            content: 'You are a helpful assistant. Answer the question using only the context provided. If the context does not contain enough information, say so clearly.',
          },
          {
            role: 'user',
            content: `Context:\n${context}\n\nQuestion: ${question}`,
          },
        ],
      }),
    }
  );
  const data = await res.json();
  if (!res.ok) throw new Error(JSON.stringify(data));
  return data.choices[0].message.content;
}

module.exports = { embedText, generateAnswer };
</code></pre>
<p>We're calling both APIs directly with Node.js's built-in <code>fetch</code> rather than the official SDKs. The reason is practical: Google's Node.js SDK routes requests through the <code>v1beta</code> endpoint by default, and <code>gemini-embedding-001</code> isn't available there — only on <code>v1</code>. Direct fetch sidesteps that entirely and keeps the dependency count low.</p>
<p><strong>src/ingest.js:</strong></p>
<pre><code class="language-javascript">const pdfParse = require('pdf-parse');
const { v4: uuidv4 } = require('uuid');
const { pool } = require('./db');
const { embedText } = require('./embeddings');

function chunkText(text, chunkSize = 500, overlap = 50) {
  const chunks = [];
  let start = 0;

  while (start &lt; text.length) {
    const end = Math.min(start + chunkSize, text.length);
    chunks.push(text.slice(start, end).trim());
    start += chunkSize - overlap;
  }

  return chunks.filter(chunk =&gt; chunk.length &gt; 50);
}

async function ingestDocument(buffer, filename) {
  const { text } = await pdfParse(buffer);
  const chunks = chunkText(text);

  console.log(`Processing ${chunks.length} chunks from "${filename}"`);

  for (const chunk of chunks) {
    const embedding = await embedText(chunk);

    await pool.query(
      `INSERT INTO documents (id, content, source, embedding)
       VALUES ($1, $2, $3, $4::vector)`,
      [uuidv4(), chunk, filename, JSON.stringify(embedding)]
    );
  }

  return chunks.length;
}

module.exports = { ingestDocument };
</code></pre>
<p>500 characters per chunk, with 50 characters of overlap between neighbours.</p>
<p>Why the overlap? Without it, a sentence that straddles a boundary gets split, half in one chunk, half in the next — and neither piece makes sense on its own when retrieved. The overlap keeps those boundary sentences intact.</p>
<p>For most technical docs, 500 is a good starting point. Dense legal or financial text tends to need something closer to 300.</p>
<h2 id="heading-build-the-query-pipeline">Build the Query Pipeline</h2>
<p><strong>src/query.js:</strong></p>
<pre><code class="language-javascript">const { pool } = require('./db');
const { embedText, generateAnswer } = require('./embeddings');

async function queryDocuments(question) {
  const questionEmbedding = await embedText(question);

  const { rows } = await pool.query(
    `SELECT content, source,
            1 - (embedding &lt;=&gt; $1::vector) AS similarity
     FROM documents
     ORDER BY embedding &lt;=&gt; $1::vector
     LIMIT 5`,
    [JSON.stringify(questionEmbedding)]
  );

  if (rows.length === 0) {
    return { answer: 'No relevant documents found.', sources: [] };
  }

  const context = rows.map(r =&gt; r.content).join('\n\n---\n\n');
  const answer = await generateAnswer(context, question);

  return {
    answer,
    sources: [...new Set(rows.map(r =&gt; r.source))],
    topSimilarity: parseFloat(rows[0].similarity).toFixed(3),
  };
}

module.exports = { queryDocuments };
</code></pre>
<p>The <code>&lt;=&gt;</code> operator is pgvector's cosine distance. Semantically similar text produces vectors that point in the same direction — so the distance between them is small. Flip that with <code>1 - distance</code> and you get a similarity score, where anything close to 1 means a strong match.</p>
<p>I found 0.7 to be a reliable threshold in my testing — chunks above that were almost always relevant. Anything below 0.5 and the retrieval was really stretching, pulling chunks that shared a keyword or two but weren't actually answering the question.</p>
<p>When that happens, the system prompt instruction ("if the context does not contain enough information, say so clearly") becomes important. A well-behaved model will tell the user it doesn't know rather than guess.</p>
<p>We also surface the source filename. Once you've ingested more than one document, users need to know whether that answer came from the architecture spec or the incident report.</p>
<h2 id="heading-build-the-chat-api-with-express">Build the Chat API with Express</h2>
<p><strong>src/index.js:</strong></p>
<pre><code class="language-javascript">require('dotenv').config();
const express = require('express');
const multer = require('multer');
const { initDb } = require('./db');
const { ingestDocument } = require('./ingest');
const { queryDocuments } = require('./query');

const app = express();
const upload = multer({ storage: multer.memoryStorage() });

app.use(express.json());

app.post('/ingest', upload.single('file'), async (req, res) =&gt; {
  if (!req.file) {
    return res.status(400).json({ error: 'No file uploaded' });
  }

  if (!req.file.mimetype.includes('pdf')) {
    return res.status(400).json({ error: 'Only PDF files are supported' });
  }

  try {
    const count = await ingestDocument(req.file.buffer, req.file.originalname);
    res.json({ message: `Ingested ${count} chunks from "${req.file.originalname}"` });
  } catch (err) {
    console.error(err);
    res.status(500).json({ error: 'Ingestion failed', detail: err.message });
  }
});

app.post('/chat', async (req, res) =&gt; {
  const { question } = req.body;

  if (!question || typeof question !== 'string') {
    return res.status(400).json({ error: 'question is required' });
  }

  try {
    const result = await queryDocuments(question);
    res.json(result);
  } catch (err) {
    console.error(err);
    res.status(500).json({ error: 'Query failed', detail: err.message });
  }
});

const PORT = process.env.PORT || 3000;

initDb().then(() =&gt; {
  app.listen(PORT, () =&gt; {
    console.log(`RAG chatbot running on port ${PORT}`);
  });
});
</code></pre>
<p><code>memoryStorage()</code> keeps the uploaded file in a buffer instead of writing it to disk. We parse it and store the chunks immediately, so there's nothing to save.</p>
<h2 id="heading-test-the-chatbot">Test the Chatbot</h2>
<p>Start the server:</p>
<pre><code class="language-bash">npm run dev
</code></pre>
<p>You should see:</p>
<pre><code class="language-plaintext">Database ready
RAG chatbot running on port 3000
</code></pre>
<p>Upload a PDF. Any PDF works. I tested with a copy of a Node.js best practices guide:</p>
<pre><code class="language-bash"># Linux / macOS
curl -X POST http://localhost:3000/ingest -F "file=@your-document.pdf"

# Windows PowerShell
curl.exe -X POST http://localhost:3000/ingest -F "file=@your-document.pdf"
</code></pre>
<p>Response:</p>
<pre><code class="language-json">{ "message": "Ingested 142 chunks from \"your-document.pdf\"" }
</code></pre>
<p>Now ask a question:</p>
<pre><code class="language-bash"># Linux / macOS
curl -X POST http://localhost:3000/chat \
  -H "Content-Type: application/json" \
  -d '{ "question": "How should I handle errors in async functions?" }'

# Windows PowerShell
curl.exe -X POST http://localhost:3000/chat -H "Content-Type: application/json" -d "{\"question\": \"How should I handle errors in async functions?\"}"
</code></pre>
<p>Response:</p>
<pre><code class="language-json">{
  "answer": "For async functions in Node.js, wrap your logic in a try/catch block to handle rejected promises. In Express, pass the caught error to next(err) to trigger your error-handling middleware. Alternatively, you can create a wrapper function that wraps any async route handler in a promise and calls next on rejection, keeping your route handlers clean...",
  "sources": ["your-document.pdf"],
  "topSimilarity": "0.841"
}
</code></pre>
<p>The <code>topSimilarity</code> score tells you how well the retrieval went. Above 0.7 and the chunks pulled were genuinely relevant. Below 0.5, and the search was struggling: it found something, but not something that actually answers the question.</p>
<p>Try asking about something your PDF doesn't mention. If the system prompt is doing its job, the model should say it doesn't have enough information rather than making something up. That's the behaviour you want in production.</p>
<p>The repo includes two diagnostic scripts that are useful if anything isn't working:</p>
<ul>
<li><p><code>node test-keys.js</code> — tests both API keys live and reports whether each one succeeds</p>
</li>
<li><p><code>node list-models.js</code> — fetches the full list of Gemini models available to your API key</p>
</li>
</ul>
<p>Run these before diving into the troubleshooting section below.</p>
<h2 id="heading-troubleshooting">Troubleshooting</h2>
<p>Everything in this section is a real error I hit while building this. Nothing hypothetical.</p>
<h3 id="heading-port-5432-is-already-in-use">Port 5432 is already in use</h3>
<pre><code class="language-plaintext">Error: bind: address already in use
</code></pre>
<p>Something else — probably a local Postgres install — is already on that port. Two fixes are needed. First, in <code>docker-compose.yml</code>:</p>
<pre><code class="language-yaml">ports:
  - "5433:5432"
</code></pre>
<p>Second, update <code>DATABASE_URL</code> in <code>.env</code>:</p>
<pre><code class="language-plaintext">DATABASE_URL=postgresql://rag_user:rag_pass@localhost:5433/rag_db
</code></pre>
<p>The container itself still listens on 5432 internally. You're just changing which port your machine uses to reach it.</p>
<h3 id="heading-password-authentication-failed-for-user-raguser">Password authentication failed for user "rag_user"</h3>
<pre><code class="language-plaintext">Error: password authentication failed for user "rag_user"
</code></pre>
<p>The password Postgres was initialized with doesn't match what your app is sending. Open <code>.env</code> and compare <code>POSTGRES_PASSWORD</code> with the password embedded in <code>DATABASE_URL</code>. They need to be character-for-character identical.</p>
<p>After fixing the mismatch, the old volume still has the wrong password baked into it. You must destroy it and start fresh:</p>
<pre><code class="language-bash">docker compose down -v
docker compose up -d
</code></pre>
<p>The <code>-v</code> flag deletes the data volume. Postgres reinitializes on the next start with the credentials from your current <code>.env</code>.</p>
<h3 id="heading-gemini-model-not-found-404">Gemini model not found (404)</h3>
<pre><code class="language-json">{ "error": { "code": 404, "message": "models/text-embedding-004 is not found" } }
</code></pre>
<p>The Google AI model naming has changed. Older tutorials and blog posts reference model names that no longer exist on the v1 endpoint. The correct model for this stack is <code>gemini-embedding-001</code>. That's what this repo uses.</p>
<p>If you want to see every model available to your API key, run:</p>
<pre><code class="language-bash">node list-models.js
</code></pre>
<p>That script fetches the live list directly from the API so you're not guessing.</p>
<h3 id="heading-vector-dimension-mismatch">Vector dimension mismatch</h3>
<pre><code class="language-plaintext">ERROR: expected 768 dimensions, not 3072
</code></pre>
<p>This error appears when your database table was created with a different dimension count than what your embedding model produces. <code>gemini-embedding-001</code> outputs 3,072-dimensional vectors. The <code>documents</code> table in this tutorial uses <code>VECTOR(3072)</code> to match.</p>
<p>If you get this error, it means either an old table exists with the wrong dimension, or you changed embedding models without recreating the table. Drop the data volume and restart:</p>
<pre><code class="language-bash">docker compose down -v
docker compose up -d
</code></pre>
<h3 id="heading-vector-index-dimension-limit">Vector index dimension limit</h3>
<pre><code class="language-plaintext">ERROR: ivfflat index type only supports up to 2000 dimensions
</code></pre>
<p>pgvector's <code>ivfflat</code> and <code>hnsw</code> index types have a maximum dimension of 2000. Since <code>gemini-embedding-001</code> produces 3,072-dimensional vectors, neither index type works.</p>
<p>This tutorial drops the index and lets pgvector do a full scan — fine for development and any reasonably sized corpus. Scaling to thousands of documents in production? Pick a model under 2000 dimensions. OpenAI's <code>text-embedding-3-small</code> outputs 1536 and plays nicely with both index types.</p>
<h3 id="heading-port-3000-is-already-in-use">Port 3000 is already in use</h3>
<pre><code class="language-plaintext">Error: EADDRINUSE: address already in use :::3000
</code></pre>
<p>Some other process got there first. Swap the port number in <code>.env</code>:</p>
<pre><code class="language-plaintext">PORT=3002
</code></pre>
<p>Save it and restart the server.</p>
<h3 id="heading-gemini-generation-returns-quota-exceeded-limit-0">Gemini generation returns quota exceeded (limit: 0)</h3>
<pre><code class="language-json">{ "error": { "status": "RESOURCE_EXHAUSTED", "message": "Quota exceeded for quota metric ... with limit 0" } }
</code></pre>
<p>That <code>limit 0</code> means Google has switched off free generation in your country entirely — not that you've used it up. I hit this myself while testing from Pakistan.</p>
<p>That's exactly why this tutorial uses Groq instead. Make sure <code>GROQ_API_KEY</code> is in your <code>.env</code> and that <code>generateAnswer</code> in <code>src/embeddings.js</code> is pointing at <code>api.groq.com</code>.</p>
<p>To verify both keys work, run:</p>
<pre><code class="language-bash">node test-keys.js
</code></pre>
<p>It tests the Gemini embedding endpoint and the Groq generation endpoint independently and reports whether each succeeds.</p>
<h3 id="heading-nodemon-doesnt-pick-up-changes-to-env">nodemon doesn't pick up changes to <code>.env</code></h3>
<p>nodemon only watches <code>.js</code> files — <code>.env</code> changes don't trigger a restart. Switch to the terminal running the server and type <code>rs</code>, then hit Enter. That forces a restart and picks up whatever you changed.</p>
<h3 id="heading-curl-doesnt-work-in-windows-powershell"><code>curl</code> doesn't work in Windows PowerShell</h3>
<pre><code class="language-plaintext">curl : The term 'curl' is not recognized
</code></pre>
<p>or</p>
<pre><code class="language-plaintext">curl : Cannot bind parameter because parameter 'Method' is specified more than once
</code></pre>
<p>PowerShell has a built-in <code>curl</code> alias that points to <code>Invoke-WebRequest</code> — completely different flags, completely different behaviour. Add <code>.exe</code> and you bypass the alias and hit the real binary.</p>
<p>So instead of <code>curl</code>, type <code>curl.exe</code>:</p>
<pre><code class="language-powershell"># Ingest
curl.exe -X POST http://localhost:3000/ingest -F "file=@your-document.pdf"

# Chat
curl.exe -X POST http://localhost:3000/chat -H "Content-Type: application/json" -d "{\"question\": \"How do I handle async errors?\"}"
</code></pre>
<p>That <code>.exe</code> is the whole fix.</p>
<h3 id="heading-docker-desktop-stopped-running">Docker Desktop stopped running</h3>
<p>Docker Desktop doesn't start automatically after a reboot on most setups. If your Docker commands suddenly fail with connection errors, that's probably why. Open Docker Desktop, wait until it says "Engine running", then try again.</p>
<h2 id="heading-how-to-swap-in-openai">How to Swap in OpenAI</h2>
<p>If you want to use the OpenAI API instead of Gemini, it's three changes.</p>
<p>1. Install the OpenAI SDK:</p>
<pre><code class="language-bash">npm install openai
</code></pre>
<p>2. Replace <code>src/embeddings.js</code> entirely:</p>
<pre><code class="language-javascript">const OpenAI = require('openai');

const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });

async function embedText(text) {
  const result = await client.embeddings.create({
    model: 'text-embedding-3-small',
    input: text,
  });
  return result.data[0].embedding;
}

async function generateAnswer(context, question) {
  const result = await client.chat.completions.create({
    model: 'gpt-4o-mini',
    messages: [
      { role: 'system', content: 'Answer only from the context provided. If the context is insufficient, say so.' },
      { role: 'user', content: `Context:\n${context}\n\nQuestion: ${question}` },
    ],
  });
  return result.choices[0].message.content;
}

module.exports = { embedText, generateAnswer };
</code></pre>
<p>3. Update the vector dimension in <code>src/db.js</code>:</p>
<p>Open <code>db.js</code> and swap <code>VECTOR(3072)</code> for <code>VECTOR(1536)</code> — that's the output size of <code>text-embedding-3-small</code>. Then kill the volume so the table gets recreated with the right dimensions:</p>
<pre><code class="language-bash">docker compose down -v
docker compose up -d
</code></pre>
<p>Nothing else needs touching. The ingestion and query logic works the same regardless of which model you plugged in.</p>
<h2 id="heading-what-to-build-next">What to Build Next</h2>
<p>What you've built works. But there are some gaps that come up quickly once you put it in front of real users.</p>
<p>The most noticeable one is <strong>streaming</strong>. Right now <code>/chat</code> holds the connection open until Groq finishes generating the full answer, then returns everything at once. On a short question that's fine. On a longer one, the user stares at nothing for a few seconds and wonders if the request hung.</p>
<p>The Groq API supports streaming — add <code>stream: true</code> to the request body and tokens start coming back as they're generated. Piping those through Express with <code>res.write()</code> is maybe 15 minutes of work and the difference in feel is immediate.</p>
<p><strong>Metadata filtering</strong> is the second thing you'll want. Once you've loaded more than a few documents, queries bleed across everything: ask about the API spec and you'll get chunks from the onboarding guide too.</p>
<p>The fix is a <code>metadata JSONB</code> column where you store the document ID on ingest, then add <code>WHERE metadata-&gt;&gt;'doc_id' = $1</code> to the similarity query. Expose it as an optional body field on <code>/chat</code>: <code>{ "question": "...", "docId": "api-spec-v2" }</code>. Users get scoped results, and you get much cleaner answers.</p>
<p>When your corpus grows into the hundreds of documents, look at <strong>re-ranking</strong>. Vector similarity retrieval is fast but approximate — it finds chunks that are semantically close to the question, not necessarily the ones that most directly answer it.</p>
<p>The pattern is: retrieve the top 20 by cosine distance, then run a cross-encoder over them to re-score by actual relevance, then take the best 5 from that second pass. LangChain.js has a cross-encoder wrapper if you don't want to implement it yourself.</p>
<p>The last thing most people forget until they actually need it is <strong>document management</strong> — the ability to list what's ingested, delete a specific file, and re-ingest an updated version.</p>
<p>A <code>DELETE FROM documents WHERE source = $1</code> handles the delete case. Add a <code>GET /documents</code> endpoint that queries <code>SELECT DISTINCT source FROM documents</code> and you have a complete enough API for real use.</p>
<p>RAG isn't magic. It's a well-scoped retrieval problem combined with a language model that's been told to stay within its lane.</p>
<p>The quality of your answers depends on three things: how cleanly your PDFs parse, how well your chunk size fits the content type, and how clearly your system prompt instructs the model to say "I don't know" rather than guess. Get those right and you've built something genuinely useful: the kind of thing that saves a new engineer's first two weeks.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Containerize a Node.js Application with Docker and Deploy with GitHub Actions ]]>
                </title>
                <description>
                    <![CDATA[ If you've been building Node.js projects, you've probably had an experience like this. The project runs fine on your machine, but when you push it to a server, something breaks. Maybe it's a different ]]>
                </description>
                <link>https://www.freecodecamp.org/news/containerize-a-node-js-app-with-docker-and-deploy-with-github-actions/</link>
                <guid isPermaLink="false">6a569b9cbd138d774dee2042</guid>
                
                    <category>
                        <![CDATA[ Docker ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Node.js ]]>
                    </category>
                
                    <category>
                        <![CDATA[ GitHub Actions ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Devops ]]>
                    </category>
                
                    <category>
                        <![CDATA[ ci-cd ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Docker compose ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Docker-compose.yml ]]>
                    </category>
                
                    <category>
                        <![CDATA[ containerization ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Backend Development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Tutorial ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Zia Ullah ]]>
                </dc:creator>
                <pubDate>Tue, 14 Jul 2026 20:27:08 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/uploads/covers/5e1e335a7a1d3fcc59028c64/343864e6-5319-4378-a2b1-4955e38ad6d8.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>If you've been building <a href="https://www.freecodecamp.org/news/role-based-access-control-nodejs-rest-api-jwt/">Node.js projects</a>, you've probably had an experience like this. The project runs fine on your machine, but when you push it to a server, something breaks.</p>
<p>Maybe it's a different Node version, maybe an environment variable is missing, or maybe a system dependency doesn't match. You spend an hour debugging something that was never actually a code problem.</p>
<p>Docker fixes this at the root. With Docker, you stop shipping just code. The Node version, dependencies, and config all travel inside the container. Your laptop, a CI server, a production VM — it behaves the same on all of them. No more environment surprises.</p>
<p>In this tutorial, we'll go through all this step by step: a multi-stage Dockerfile, using Docker Compose with PostgreSQL for local development, and a GitHub Actions workflow that pushes a fresh image to Docker Hub on every merge to <code>main</code>.</p>
<p>The complete code for this tutorial is available on <a href="https://github.com/ziaongit/nodejs-docker-cicd">GitHub</a>.</p>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ol>
<li><p><a href="#heading-prerequisites">Prerequisites</a></p>
</li>
<li><p><a href="#heading-the-sample-application">The Sample Application</a></p>
</li>
<li><p><a href="#heading-writing-the-dockerfile">Writing the Dockerfile</a></p>
</li>
<li><p><a href="#heading-the-dockerignore-file">The .dockerignore File</a></p>
</li>
<li><p><a href="#heading-the-gitignore-file">The .gitignore File</a></p>
</li>
<li><p><a href="#heading-build-and-test-the-image-locally">Build and Test the Image Locally</a></p>
</li>
<li><p><a href="#heading-docker-compose-for-local-development">Docker Compose for Local Development</a></p>
</li>
<li><p><a href="#heading-automate-the-build-with-github-actions">Automate the Build with GitHub Actions</a></p>
</li>
<li><p><a href="#heading-deploying-the-image">Deploying the Image</a></p>
</li>
<li><p><a href="#heading-wrapping-up">Wrapping Up</a></p>
</li>
</ol>
<h2 id="heading-prerequisites">Prerequisites</h2>
<ul>
<li><p>Node.js 18+</p>
</li>
<li><p>Docker Desktop, which you can download at <a href="https://docs.docker.com/get-docker/">docs.docker.com/get-docker</a>. Windows users need WSL 2 before Docker starts. Open PowerShell as Administrator and run <code>wsl --install</code>. After the restart, Docker Desktop will install without issues.</p>
</li>
<li><p>A GitHub account</p>
</li>
<li><p>A Docker Hub account (free at <a href="https://hub.docker.com">hub.docker.com</a>)</p>
</li>
<li><p>Some Express.js experience helps, but isn't required</p>
</li>
</ul>
<h2 id="heading-the-sample-application">The Sample Application</h2>
<p>We're building a task management API with Express and PostgreSQL. Keep in mind the app is just a vehicle to teach you how this works. The Dockerfile and pipeline we set up here work the same way for any Node.js project.</p>
<p>Create the project:</p>
<pre><code class="language-bash">mkdir nodejs-docker-cicd &amp;&amp; cd nodejs-docker-cicd
npm init -y
npm install express pg dotenv
npm install --save-dev nodemon
</code></pre>
<p>Create <code>src/index.js</code>:</p>
<pre><code class="language-javascript">const express = require('express');
const { Pool } = require('pg');
require('dotenv').config();

const app = express();
app.use(express.json());

const pool = new Pool({
  host: process.env.DB_HOST,
  port: process.env.DB_PORT,
  database: process.env.DB_NAME,
  user: process.env.DB_USER,
  password: process.env.DB_PASSWORD,
});

// Create table on startup
pool.query(`
  CREATE TABLE IF NOT EXISTS tasks (
    id SERIAL PRIMARY KEY,
    title VARCHAR(255) NOT NULL,
    completed BOOLEAN DEFAULT FALSE,
    created_at TIMESTAMP DEFAULT NOW()
  )
`).catch(console.error);

// Health check — required for Docker HEALTHCHECK and load balancers
app.get('/health', (req, res) =&gt; {
  res.json({ status: 'ok', timestamp: new Date().toISOString() });
});

app.get('/tasks', async (req, res) =&gt; {
  try {
    const result = await pool.query('SELECT * FROM tasks ORDER BY created_at DESC');
    res.json(result.rows);
  } catch (err) {
    res.status(500).json({ error: err.message });
  }
});

app.post('/tasks', async (req, res) =&gt; {
  const { title } = req.body;
  if (!title) return res.status(400).json({ error: 'Title is required' });
  try {
    const result = await pool.query(
      'INSERT INTO tasks (title) VALUES ($1) RETURNING *',
      [title]
    );
    res.status(201).json(result.rows[0]);
  } catch (err) {
    res.status(500).json({ error: err.message });
  }
});

app.patch('/tasks/:id', async (req, res) =&gt; {
  const { id } = req.params;
  const { completed } = req.body;
  try {
    const result = await pool.query(
      'UPDATE tasks SET completed = $1 WHERE id = $2 RETURNING *',
      [completed, id]
    );
    if (result.rows.length === 0) return res.status(404).json({ error: 'Task not found' });
    res.json(result.rows[0]);
  } catch (err) {
    res.status(500).json({ error: err.message });
  }
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () =&gt; console.log(`Server running on port ${PORT}`));
</code></pre>
<p>Open <code>package.json</code> and update the <code>"scripts"</code> section:</p>
<pre><code class="language-json">"scripts": {
  "start": "node src/index.js",
  "dev": "nodemon src/index.js"
}
</code></pre>
<p><code>npm start</code> runs the app directly with Node. <code>npm run dev</code> uses nodemon so the server restarts automatically when you edit a file.</p>
<p>For running without Docker, create a <code>.env</code> file:</p>
<pre><code class="language-plaintext">DB_HOST=localhost
DB_PORT=5432
DB_NAME=tasksdb
DB_USER=postgres
DB_PASSWORD=yourpassword
PORT=3000
</code></pre>
<p>Notice that all database credentials come from environment variables rather than being hardcoded. Swap the variables, and the same image runs against your local database or a production one — no code changes needed. The <code>/health</code> endpoint is what Docker pings to know the app is actually handling requests.</p>
<h2 id="heading-writing-the-dockerfile">Writing the Dockerfile</h2>
<p>Before touching the Dockerfile, there are two terms you'll keep seeing. An <strong>image</strong> is a packaged, immutable version of your app — Node runtime, code, dependencies, everything together in one artifact. A <strong>container</strong> is a running instance of that image. One image, many containers, any machine.</p>
<p>Here's the Dockerfile we'll use:</p>
<pre><code class="language-dockerfile"># ── Stage 1: Install dependencies ──────────────────────────────────────────
FROM node:18-alpine AS builder

WORKDIR /app

# Copy package files first — Docker caches this layer separately.
# If you only change src code (not package.json), Docker skips npm ci on rebuild.
COPY package*.json ./
RUN npm ci

COPY . .


# ── Stage 2: Production image ───────────────────────────────────────────────
FROM node:18-alpine AS production

# Create a non-root user — running as root inside a container is a security risk
RUN addgroup -g 1001 -S nodejs &amp;&amp; \
    adduser -S nodeuser -u 1001

WORKDIR /app

COPY package*.json ./
RUN npm ci --only=production

# Copy only the source code from the builder stage (not node_modules or dev files)
COPY --from=builder /app/src ./src

RUN chown -R nodeuser:nodejs /app
USER nodeuser

EXPOSE 3000

# Docker will ping /health every 30s. If it fails 3 times, the container is marked unhealthy.
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
  CMD wget --no-verbose --tries=1 --spider http://localhost:3000/health || exit 1

CMD ["node", "src/index.js"]
</code></pre>
<p>This is a multi-stage build. The first stage (<code>builder</code>) installs everything, including dev dependencies. The second stage (<code>production</code>) starts fresh and only copies what the app needs to run. Nodemon, test frameworks, and anything else dev-only never make it into the final image.</p>
<p>The size difference is real. A <code>node:18</code> Debian image is over 950MB. Switch to <code>node:18-alpine</code> and cut out the dev dependencies, and the final image lands around 150–200MB instead. A smaller image means faster pushes and faster deploys.</p>
<p><code>npm ci</code> instead of <code>npm install</code> is a deliberate choice for CI/CD. It reads exact versions from <code>package-lock.json</code> and fails hard if the lockfile doesn't match <code>package.json</code>. Every build on every machine installs the exact same versions — no surprises from a dependency that quietly updated overnight.</p>
<p>The <code>nodeuser</code> account exists because containers run as root by default. That's fine until something goes wrong. A non-root user means that an attacker who gets inside the container can't just do whatever they want.</p>
<h2 id="heading-the-dockerignore-file">The <code>.dockerignore</code> File</h2>
<p>Create <code>.dockerignore</code> before building:</p>
<pre><code class="language-plaintext">node_modules
npm-debug.log
.env
.git
.gitignore
README.md
Dockerfile
.dockerignore
</code></pre>
<p>The <code>node_modules</code> exclusion is the critical one. Your local modules were compiled for your operating system — macOS or Windows binaries won't work inside a Linux container. Excluding them means Docker installs fresh modules during the build, compiled for the correct platform. Without this exclusion, you'd either copy broken binaries into the image or waste time uploading hundreds of megabytes to the build context.</p>
<p>Never put <code>.env</code> in an image. Passwords, API keys, anything sensitive — those go in at runtime as environment variables, never inside the image itself.</p>
<h2 id="heading-the-gitignore-file">The <code>.gitignore</code> File</h2>
<p>One more thing before the first commit: a <code>.gitignore</code>. You don't want <code>node_modules</code> or <code>.env</code> tracked:</p>
<pre><code class="language-plaintext">node_modules/
.env
.env.local
npm-debug.log*
logs/
.DS_Store
Thumbs.db
.vscode/
.idea/
dist/
build/
</code></pre>
<h2 id="heading-build-and-test-the-image-locally">Build and Test the Image Locally</h2>
<p>Open Docker Desktop first and give it a moment. On Windows, you'll see a whale icon in the taskbar that animates while the engine is starting up. Once it goes still, you're good to run Docker commands. If you try to run Docker before the engine is up, you'll hit this:</p>
<pre><code class="language-plaintext">ERROR: Error response from daemon: Docker Desktop is unable to start
</code></pre>
<p>If that happens, quit Docker Desktop. Open PowerShell as Administrator, run <code>wsl --update</code>, and restart. Then go to Control Panel → Programs → Turn Windows features on or off. Both Hyper-V and Virtual Machine Platform need to be checked. After the restart, Docker Desktop should come up fine.</p>
<p>It's worth knowing about this error too:</p>
<pre><code class="language-plaintext">docker : The term 'docker' is not recognized as the name of a cmdlet, function,
script file, or operable program.
</code></pre>
<p>This means that Docker Desktop isn't running or isn't installed. Open it from the Start menu and wait.</p>
<p>Run the build:</p>
<pre><code class="language-bash">docker build -t nodejs-docker-cicd:latest .
</code></pre>
<p>The first time takes roughly 30 seconds since Docker has to pull <code>node:18-alpine</code> from the internet. Once that's cached, subsequent builds are much quicker. Both stages will scroll by:</p>
<pre><code class="language-plaintext">[+] Building 33.1s (17/17) FINISHED
 =&gt; [builder 1/5] FROM docker.io/library/node:18-alpine       20.9s
 =&gt; [builder 4/5] RUN npm ci                                   3.5s
 =&gt; [production 5/7] RUN npm ci --only=production              3.2s
 =&gt; [production 7/7] RUN chown -R nodeuser:nodejs /app         3.2s
 =&gt; exporting to image                                         1.5s
 =&gt; =&gt; naming to docker.io/library/nodejs-docker-cicd:latest     0.0s
</code></pre>
<p>When you see <code>(17/17) FINISHED</code> the image is built. Check the size:</p>
<pre><code class="language-bash">docker images nodejs-docker-cicd
</code></pre>
<pre><code class="language-plaintext">IMAGE                     ID             DISK USAGE   CONTENT SIZE
nodejs-docker-cicd:latest   c9eed311d999        198MB         47.5MB
</code></pre>
<p><strong>CONTENT SIZE</strong> (47.5MB) is the compressed size that gets pushed to Docker Hub. <strong>DISK USAGE</strong> (198MB) is what it takes up on disk locally. Compare that to a <code>node:18</code> Debian image at 950MB+, and you can see why the Alpine base and multi-stage approach matter.</p>
<p>On subsequent builds, Docker reuses cached layers. Edit only your source files without touching <code>package.json</code> and the <code>npm ci</code> step gets skipped completely. That 33-second first build becomes 3 seconds.</p>
<h2 id="heading-docker-compose-for-local-development">Docker Compose for Local Development</h2>
<p>The app needs a database. Setting up PostgreSQL locally means every developer who clones the repo has to do it, too. Docker Compose handles this: one file defines both services, and one command starts them.</p>
<p>Create <code>docker-compose.yml</code>:</p>
<pre><code class="language-yaml">services:
  app:
    build:
      context: .
      target: production
    ports:
      - '3000:3000'
    environment:
      DB_HOST: postgres
      DB_PORT: 5432
      DB_NAME: tasksdb
      DB_USER: postgres
      DB_PASSWORD: postgres
      PORT: 3000
    depends_on:
      postgres:
        condition: service_healthy
    restart: unless-stopped

  postgres:
    image: postgres:15-alpine
    environment:
      POSTGRES_DB: tasksdb
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
    ports:
      - '5432:5432'
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ['CMD-SHELL', 'pg_isready -U postgres']
      interval: 5s
      timeout: 5s
      retries: 5

volumes:
  postgres_data:
</code></pre>
<p>A few things worth pointing out. <code>DB_HOST</code> is set to <code>postgres</code>. That's the service name, not <code>localhost</code>. Containers on the same Docker network reach each other by service name. Put <code>localhost</code> there and the app tries to connect to itself.</p>
<p><code>depends_on</code> with <code>condition: service_healthy</code> holds the app back until Postgres actually passes its health check. Skip this and the app starts, tries to connect to a database that isn't ready yet, and crashes. The health check pings <code>pg_isready</code> every 5 seconds. Once it gets a green response, the app container starts.</p>
<p>The named volume <code>postgres_data</code> keeps your data alive between restarts. Run <code>docker compose down</code> and the data is still there next time. Add <code>--volumes</code> to wipe it clean.</p>
<p>Start both services:</p>
<pre><code class="language-bash">docker compose up --build
</code></pre>
<p>You'll see PostgreSQL initialize and then the app start. Once you see <code>Server running on port 3000</code> in the logs, the stack is up.</p>
<p>Open a second terminal to test — leave the compose logs running in the first one.</p>
<p><strong>Linux/macOS:</strong></p>
<pre><code class="language-bash">curl -X POST http://localhost:3000/tasks \
  -H "Content-Type: application/json" \
  -d '{"title": "Learn Docker"}'

curl http://localhost:3000/tasks

curl http://localhost:3000/health
</code></pre>
<p><strong>Windows PowerShell:</strong> Typing <code>curl</code> in PowerShell runs <code>Invoke-WebRequest</code>, not actual curl. Run <code>curl.exe</code> instead. For JSON bodies, write to a file first:</p>
<pre><code class="language-powershell">'{"title": "Learn Docker"}' | Set-Content body.json
curl.exe -X POST http://localhost:3000/tasks -H "Content-Type: application/json" --data `@body.json

curl.exe http://localhost:3000/tasks

curl.exe http://localhost:3000/health
</code></pre>
<p>The backtick before <code>@body.json</code> is necessary. PowerShell would otherwise try to interpret <code>@</code> as a splatting operator rather than passing it to curl as a filename prefix.</p>
<p>You should see responses like these:</p>
<pre><code class="language-json"># POST /tasks
{"id":1,"title":"Learn Docker","completed":false,"created_at":"2026-07-09T22:21:17.073Z"}

# GET /tasks
[{"id":1,"title":"Learn Docker","completed":false,"created_at":"2026-07-09T22:21:17.073Z"}]

# GET /health
{"status":"ok","timestamp":"2026-07-09T22:11:44.700Z"}
</code></pre>
<p>The task hit PostgreSQL in one container and came back through the app. <code>Ctrl+C</code> in the compose terminal stops both.</p>
<h2 id="heading-automate-the-build-with-github-actions">Automate the Build with GitHub Actions</h2>
<p>The image works locally, so it's time to stop doing this by hand.</p>
<h3 id="heading-step-1-create-a-docker-hub-access-token">Step 1: Create a Docker Hub Access Token</h3>
<p>Go to <a href="https://hub.docker.com">hub.docker.com</a> and then Account Settings → Security → New Access Token. Set permission to Read &amp; Write, as read-only breaks the push. The token appears once, so copy it before closing the page.</p>
<p><strong>Security warning:</strong> Don't paste this token into a chat, email, or commit. If you expose it by accident, delete it immediately, then make a new one.</p>
<h3 id="heading-step-2-add-secrets-to-your-github-repository">Step 2: Add Secrets to Your GitHub Repository</h3>
<p>Head to Settings → Secrets and variables → Actions in your repo and add:</p>
<ul>
<li><p><code>DOCKERHUB_USERNAME</code> — your Docker Hub username</p>
</li>
<li><p><code>DOCKERHUB_TOKEN</code> — paste the token here, nowhere else</p>
</li>
</ul>
<p>If you ran into <code>Error: Username and password required</code>, the secrets either aren't saved yet or the names are typed wrong. Both are case-sensitive.</p>
<p>A Node 20 deprecation warning in the logs is normal. It comes from the Docker actions internally, not your code.</p>
<h3 id="heading-step-3-create-the-workflow-file">Step 3: Create the Workflow File</h3>
<p>Create <code>.github/workflows/docker-publish.yml</code>:</p>
<pre><code class="language-yaml">name: Build and Push Docker Image

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

env:
  IMAGE_NAME: ${{ secrets.DOCKERHUB_USERNAME }}/nodejs-docker-cicd

jobs:
  build-and-push:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Log in to Docker Hub
        if: github.event_name != 'pull_request'
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Extract metadata
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ${{ env.IMAGE_NAME }}
          tags: |
            type=sha,prefix=sha-
            type=raw,value=latest,enable={{is_default_branch}}

      - name: Build and push
        uses: docker/build-push-action@v5
        with:
          context: .
          target: production
          push: ${{ github.event_name != 'pull_request' }}
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          cache-from: type=gha
          cache-to: type=gha,mode=max
</code></pre>
<p>The login step has <code>if: github.event_name != 'pull_request'</code>. This skips authentication on pull requests. PRs from forks don't have access to your secrets, so trying to log in would just fail. The build still runs on PRs to validate your Dockerfile, but the image isn't pushed.</p>
<p>The metadata action generates two tags on every merge to <code>main</code>: <code>latest</code> and a short commit SHA like <code>sha-a1b2c3d</code>. The SHA tag is what makes rollbacks practical. If <code>latest</code> breaks in production, you can pull any previous <code>sha-</code> tag and you're back to a known-good state in seconds.</p>
<p>The <code>cache-from/cache-to: type=gha</code> lines store Docker's layer cache in GitHub Actions' built-in cache. The first run builds everything from scratch. After that, unchanged layers are pulled from cache rather than rebuilt. On a typical Node.js app this brings build time from 2–3 minutes down to under 30 seconds.</p>
<h3 id="heading-push-and-watch-it-run">Push and Watch it Run</h3>
<pre><code class="language-bash">git add .
git commit -m "Add Docker configuration and GitHub Actions workflow"
git push origin main
</code></pre>
<p>Go to your repo's <strong>Actions</strong> tab. You'll see the workflow running in real time. Each step turns green as it completes:</p>
<pre><code class="language-plaintext">✅ Checkout code
✅ Set up Docker Buildx
✅ Log in to Docker Hub
✅ Extract metadata
✅ Build and push
</code></pre>
<p>Green across the board means your image is live on Docker Hub — two tags, <code>latest</code> and a commit SHA like <code>sha-a1b2c3d</code>. Every push to <code>main</code> from here builds and ships automatically.</p>
<h2 id="heading-deploying-the-image">Deploying the Image</h2>
<p>With your image on Docker Hub, you can deploy it to any infrastructure:</p>
<p><strong>Any VPS or server:</strong></p>
<pre><code class="language-bash">docker pull yourusername/nodejs-docker-cicd:latest
docker run -d -p 3000:3000 \
  -e DB_HOST=your-db-host \
  -e DB_NAME=tasksdb \
  -e DB_USER=postgres \
  -e DB_PASSWORD=yourpassword \
  yourusername/nodejs-docker-cicd:latest
</code></pre>
<p><strong>Railway</strong> — Connect your Docker Hub image in the Railway dashboard and it deploys on the next push.</p>
<p><strong>Fly.io</strong> — Run <code>fly launch</code> pointing at your Dockerfile and Fly handles the rest.</p>
<p><strong>Render</strong> — Paste your Docker Hub image URL into the Render service settings.</p>
<p>Each push to <code>main</code> runs the workflow. New image goes to Docker Hub, platform picks it up — that's your deployment handled.</p>
<h2 id="heading-wrapping-up">Wrapping Up</h2>
<p>What started as a local Node.js app now runs in a container. You get the same behavior on any machine, real PostgreSQL in development, and a pipeline that builds and ships to Docker Hub without you doing anything after the push.</p>
<p>The multi-stage build keeps the image lean — dev tools stay out, non-root user, health check baked in. Compose gets the full stack up with one command for anyone who clones the repo. The SHA tag on every GitHub Actions build means rolling back is just a matter of pulling an older tag.</p>
<p>These same patterns (multi-stage builds, Compose for local development, automated image publishing) are used across the industry for production Node.js deployments. Pick up these patterns once and they follow you to every project.</p>
<p>From here, you can extend the pipeline: drop a test step in before the build, or add multi-platform support if you're targeting ARM. Once Docker Compose starts feeling limiting in production, that's usually when Kubernetes enters the picture.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Build Your Own Healthcare AI Assistant with MedGemma, Ollama, and Open WebUI ]]>
                </title>
                <description>
                    <![CDATA[ Healthcare data is among the most sensitive data there is. Sending it to a cloud AI service is often not an option because of privacy requirements, regulatory compliance, or both. In this tutorial, yo ]]>
                </description>
                <link>https://www.freecodecamp.org/news/build-your-own-healthcare-ai-assistant-with-medgemma-ollama-and-open-webui/</link>
                <guid isPermaLink="false">6a4edb71b23ba37e305b1825</guid>
                
                    <category>
                        <![CDATA[ AI ]]>
                    </category>
                
                    <category>
                        <![CDATA[ healthcare ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Machine Learning ]]>
                    </category>
                
                    <category>
                        <![CDATA[ ollama ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Tutorial ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Medical Imaging ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Lakshmi Mahabaleshwara ]]>
                </dc:creator>
                <pubDate>Wed, 08 Jul 2026 23:21:21 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/uploads/covers/5e1e335a7a1d3fcc59028c64/c6e53c46-ca40-4f4a-87e9-a925c85963d6.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Healthcare data is among the most sensitive data there is. Sending it to a cloud AI service is often not an option because of privacy requirements, regulatory compliance, or both.</p>
<p>In this tutorial, you’ll build a healthcare AI assistant that runs entirely on your own machine using three open-source tools:</p>
<ul>
<li><p>MedGemma, Google’s open medical AI model for understanding medical text and images</p>
</li>
<li><p>Ollama, the easiest way to download and run AI models locally</p>
</li>
<li><p>Open WebUI, a ChatGPT-style web interface for interacting with local models</p>
</li>
</ul>
<p>By the end, you’ll be able to chat with a medically tuned AI model, upload medical images such as chest X-rays for analysis, and do it all locally, without sending your data to the cloud.</p>
<p><strong>Important disclaimer</strong> before we start: MedGemma is a developer model, not a medical device. Its outputs are not intended to directly inform clinical diagnosis, patient management, or treatment decisions.</p>
<p>Everything you build in this tutorial is for learning, prototyping, and research. Always consult qualified healthcare professionals for real medical questions.</p>
<h3 id="heading-what-well-cover">What We'll Cover:</h3>
<ul>
<li><p><a href="#heading-who-is-this-tutorial-for">Who is This Tutorial For?</a></p>
</li>
<li><p><a href="#heading-what-is-medgemma">What is MedGemma?</a></p>
</li>
<li><p><a href="#heading-why-run-models-locally">Why Run Models Locally?</a></p>
</li>
<li><p><a href="#heading-prerequisites">Prerequisites</a></p>
</li>
<li><p><a href="#heading-architecture-diagram">Architecture Diagram</a></p>
</li>
<li><p><a href="#heading-step-1-install-ollama">Step 1: Install Ollama</a></p>
</li>
<li><p><a href="#heading-step-2-pull-medgemma">Step 2: Pull MedGemma</a></p>
</li>
<li><p><a href="#heading-step-3-test-medgemma-from-the-terminal">Step 3: Test MedGemma from the Terminal</a></p>
</li>
<li><p><a href="#heading-step-4-install-open-webui">Step 4: Install Open WebUI</a></p>
<ul>
<li><p><a href="#heading-option-a-docker-recommended">Option A: Docker (recommended)</a></p>
</li>
<li><p><a href="#heading-option-b-pip-no-docker">Option B: pip (no Docker)</a></p>
</li>
</ul>
</li>
<li><p><a href="#heading-step-5-connect-open-webui-to-ollama">Step 5: Connect Open WebUI to Ollama</a></p>
</li>
<li><p><a href="#heading-step-6-start-chatting-with-medgemma">Step 6: Start Chatting with MedGemma</a></p>
</li>
<li><p><a href="#heading-step-7-upload-medical-images">Step 7: Upload Medical Images</a></p>
</li>
<li><p><a href="#heading-example-prompts-to-try">Example Prompts to Try</a></p>
</li>
<li><p><a href="#heading-running-larger-models">Running Larger Models</a></p>
</li>
<li><p><a href="#heading-troubleshooting-guide">Troubleshooting Guide</a></p>
<ul>
<li><p><a href="#heading-error-registryollamaailibrarymedgemmalatest-does-not-support-tools">Error: registry.ollama.ai/library/medgemma:latest does not support tools</a></p>
</li>
<li><p><a href="#heading-open-webui-shows-no-models-in-the-dropdown">Open WebUI shows no models in the dropdown</a></p>
</li>
<li><p><a href="#heading-ollama-pull-medgemma-says-model-not-found">ollama pull medgemma says model not found</a></p>
</li>
<li><p><a href="#heading-responses-are-extremely-slow">Responses are extremely slow</a></p>
</li>
<li><p><a href="#heading-image-upload-doesnt-work-or-the-model-ignores-the-image">Image upload doesn't work or the model ignores the image</a></p>
</li>
<li><p><a href="#heading-port-3000-is-already-in-use">Port 3000 is already in use</a></p>
</li>
<li><p><a href="#heading-out-of-memory-errors-when-loading-the-27b-model">"Out of memory" errors when loading the 27B model</a></p>
</li>
</ul>
</li>
<li><p><a href="#heading-conclusion">Conclusion</a></p>
</li>
</ul>
<h2 id="heading-who-is-this-tutorial-for"><strong>Who is This Tutorial For?</strong></h2>
<p>This tutorial is ideal if you’re:</p>
<ul>
<li><p>learning healthcare AI</p>
</li>
<li><p>building medical RAG systems</p>
</li>
<li><p>experimenting with radiology assistants</p>
</li>
<li><p>developing medical education tools</p>
</li>
<li><p>researching multimodal models</p>
</li>
</ul>
<h2 id="heading-what-is-medgemma">What is MedGemma?</h2>
<p><strong>MedGemma</strong> is a collection of open models from Google, built on the Gemma 3 architecture and specifically trained for medical text and image comprehension. Think of it as Gemma after four years of medical school and a radiology residency.</p>
<img src="https://cdn.hashnode.com/uploads/covers/69fd77e89f93a850a46d376f/0ea6b1a3-c9dd-4990-8fd4-404ab4069458.png" alt="Diagram showing MedGemma’s multimodal architecture, where medical images are processed by a SigLIP vision encoder and combined with a language model to understand medical text and images and generate responses." style="display:block;margin:0 auto" width="1245" height="1150" loading="lazy">

<h3 id="heading-why-medgemma">Why MedGemma?</h3>
<p>Unlike general-purpose models such as Llama or Mistral, MedGemma is designed specifically for healthcare applications.</p>
<ul>
<li><p><strong>Medical image understanding:</strong> Its multimodal models are trained on de-identified medical images, including chest X-rays, dermatology, ophthalmology, and pathology images.</p>
</li>
<li><p><strong>Medical language expertise:</strong> It has been trained on medical literature and clinical question-answer datasets, enabling it to better understand medical terminology and radiology reports.</p>
</li>
<li><p><strong>Multiple model sizes:</strong> MedGemma is available in 4B and 27B variants, both supporting text and image inputs with a 128K context window.</p>
</li>
<li><p><strong>Open weights:</strong> You can download, run, fine-tune, and build applications with the model locally under the Health AI Developer Foundation's terms of use.</p>
</li>
</ul>
<p>MedGemma is intended as a foundation model for developers building healthcare applications, medical education tools, research assistants, report summarizers, and other AI-powered medical workflows.</p>
<h2 id="heading-why-run-models-locally">Why Run Models Locally?</h2>
<p>You could call a hosted medical model through an API. So why go local? In healthcare, the case is stronger than almost anywhere else.</p>
<p>First, there's the principle of privacy by architecture. When the model runs on your machine, medical text and images never leave your device. There's no API log, no third-party data processor, no data processing agreement to negotiate.</p>
<p>For anyone working near PHI (Protected Health Information), "the data never left the laptop" is the simplest compliance story that exists.</p>
<p>Next, you have zero per-token cost. Experimentation is free once the model is downloaded. You can iterate on prompts hundreds of times without watching a billing dashboard.</p>
<p>You also get offline access. Hospitals, labs, and field clinics often have restricted or air-gapped networks. A local model works without internet after the initial download.</p>
<p>And you have full control over the setup: you choose the model version, you pin it, and it never changes underneath you. No deprecation notices, no silent behavior changes.</p>
<p>Finally, it's a great way to learn. Running models locally demystifies them. You'll develop intuition for context windows, quantization, and memory constraints that you simply don't get from calling an API.</p>
<h2 id="heading-prerequisites">Prerequisites</h2>
<p>Here's what you need before starting:</p>
<p><strong>Hardware:</strong></p>
<ul>
<li><p><strong>8 GB RAM minimum</strong> (16 GB recommended) for the MedGemma 4B model. The download is about 3.3 GB.</p>
</li>
<li><p><strong>32 GB RAM or a 24 GB+ GPU</strong> if you want to run the 27B model (a roughly 17 GB download).</p>
</li>
<li><p>Around <strong>15 GB of free disk space</strong> to be comfortable (model + Docker images + working room).</p>
</li>
<li><p>Apple Silicon Macs (M1 through M4) are excellent for this. Ollama uses Metal acceleration automatically. On Windows and Linux, an NVIDIA GPU helps a lot but isn't required. A CPU-only inference works, just slower.</p>
</li>
</ul>
<p><strong>Software:</strong></p>
<ul>
<li><p>macOS, Linux, or Windows 10/11</p>
</li>
<li><p><strong>Docker Desktop</strong> (for the recommended Open WebUI installation), or Python 3.11 if you prefer installing Open WebUI with pip</p>
</li>
<li><p>Basic comfort with the terminal</p>
</li>
</ul>
<p>That's it. No API keys, no accounts, and no GPU cloud credits.</p>
<h2 id="heading-architecture-diagram"><strong>Architecture Diagram</strong></h2>
<img src="https://cdn.hashnode.com/uploads/covers/69fd77e89f93a850a46d376f/fa07c471-322a-4a39-bbd3-cfc885b9feec.png" alt="Architecture diagram showing Open WebUI connected to Ollama, which runs the MedGemma model locally on the user’s computer. All medical text and image processing happens on the local machine without using cloud services." style="display:block;margin:0 auto" width="2720" height="1808" loading="lazy">

<h2 id="heading-step-1-install-ollama">Step 1: Install Ollama</h2>
<p>Ollama is a lightweight runtime that handles downloading, quantizing, and serving open models through a simple CLI and a local REST API.</p>
<p><strong>On macOS:</strong></p>
<p>Download the app from <a href="https://ollama.com/download">ollama.com/download</a> and drag it to Applications, or install via Homebrew:</p>
<pre><code class="language-shell">brew install ollama
</code></pre>
<p><strong>On Linux:</strong></p>
<pre><code class="language-shell">curl -fsSL https://ollama.com/install.sh | sh
</code></pre>
<p><strong>On Windows:</strong></p>
<p>Download the native Windows installer from <a href="https://ollama.com/download">ollama.com/download</a> and run it. (Ollama now supports Windows natively, no WSL required.)</p>
<p>Once installed, verify it works:</p>
<pre><code class="language-shell">ollama --version
</code></pre>
<p>You should see a version number printed. Ollama also starts a background service that listens on <code>http://localhost:11434</code>. This is the API that Open WebUI will talk to later. You can confirm the server is up with:</p>
<pre><code class="language-shell">curl http://localhost:11434
</code></pre>
<p>which should return <code>Ollama is running</code>.</p>
<h2 id="heading-step-2-pull-medgemma">Step 2: Pull MedGemma</h2>
<p>MedGemma is available directly in the official Ollama model library, so downloading it is one command:</p>
<pre><code class="language-shell">ollama pull medgemma
</code></pre>
<p>This pulls the default 4B multimodal variant, about a 3.3 GB download.</p>
<p>If you want to be explicit about the size (useful when you later experiment with the 27B model):</p>
<pre><code class="language-shell">ollama pull medgemma:4b     # 3.3 GB — multimodal, runs on most laptops
ollama pull medgemma:27b    # 17 GB — multimodal, needs serious hardware
</code></pre>
<p>When the download finishes, confirm the model is installed:</p>
<pre><code class="language-shell">ollama list
</code></pre>
<p>You should see <code>medgemma</code> in the output along with its size.</p>
<h2 id="heading-step-3-test-medgemma-from-the-terminal">Step 3: Test MedGemma from the Terminal</h2>
<p>Before adding a UI, let's make sure the model actually works. Start an interactive session:</p>
<pre><code class="language-shell">ollama run medgemma
</code></pre>
<p>You'll get a <code>&gt;&gt;&gt;</code> prompt. Try a medical question:</p>
<pre><code class="language-plaintext">&gt;&gt;&gt; What are the classic radiographic signs of pneumonia on a chest X-ray?
</code></pre>
<p>MedGemma should respond with a structured answer covering findings like consolidation, air bronchograms, and silhouette signs — the kind of answer that shows its radiology training.</p>
<p>Try one more to see the clinical reasoning:</p>
<pre><code class="language-plaintext">&gt;&gt;&gt; Explain the difference between Type 1 and Type 2 diabetes to a first-year medical student.
</code></pre>
<p>A few useful commands inside the session:</p>
<ul>
<li><p><code>/bye</code> — exit the session</p>
</li>
<li><p><code>/clear</code> — clear the conversation context</p>
</li>
<li><p><code>/show info</code> — display model details (parameters, quantization, context length)</p>
</li>
</ul>
<p>You can also test image input directly from the terminal by passing a file path directly in the prompt:</p>
<pre><code class="language-plaintext">&gt;&gt;&gt; Describe the key findings in this image. ./chest_xray_sample.png
</code></pre>
<p>While this works, uploading images through Open WebUI is much more convenient.</p>
<h2 id="heading-step-4-install-open-webui">Step 4: Install Open WebUI</h2>
<p>Open WebUI gives you a clean, ChatGPT-style interface on top of Ollama: conversation history, model switching, image uploads, and multi-user support, all self-hosted.</p>
<h3 id="heading-option-a-docker-recommended">Option A: Docker (recommended)</h3>
<p>Start by installing <a href="https://www.docker.com/get-started">Docker</a>.</p>
<p>Make sure Docker Desktop is running, then launch Open WebUI with:</p>
<pre><code class="language-shell">docker run -d -p 3000:8080 \
  --add-host=host.docker.internal:host-gateway \
  -v open-webui:/app/backend/data \
  --name open-webui \
  --restart always \
  ghcr.io/open-webui/open-webui:main
</code></pre>
<p>Let's break down what this command does:</p>
<ul>
<li><p><code>-d</code> runs the container in the background</p>
</li>
<li><p><code>-p 3000:8080</code> maps port 3000 on your machine to the WebUI's internal port 8080</p>
</li>
<li><p><code>--add-host=host.docker.internal:host-gateway</code> lets the container reach the Ollama server running on your host machine</p>
</li>
<li><p><code>-v open-webui:/app/backend/data</code> creates a Docker volume so your chats and settings survive container restarts</p>
</li>
<li><p><code>--restart always</code> brings the UI back up automatically after reboots</p>
</li>
</ul>
<h3 id="heading-option-b-pip-no-docker">Option B: pip (no Docker)</h3>
<p>If you'd rather skip Docker, you can instead install Open WebUI as a Python package (Python 3.11 is the supported version):</p>
<pre><code class="language-shell">pip install open-webui
open-webui serve
</code></pre>
<p>This starts the interface at <code>http://localhost:8080</code> instead of port 3000.</p>
<h2 id="heading-step-5-connect-open-webui-to-ollama">Step 5: Connect Open WebUI to Ollama</h2>
<p>Open your browser and go to <code>http://localhost:3000</code> (or <code>:8080</code> if you used pip).</p>
<p>On first launch, you'll be asked to create an admin account. This account is stored <strong>locally on your machine</strong> (it's not a cloud signup).</p>
<p>In most setups, Open WebUI auto-detects Ollama at <a href="http://localhost:11434"><code>http://localhost:11434</code></a> and you're done.</p>
<p>If your models don't appear, wire up the connection manually:</p>
<ol>
<li><p>Click your profile icon and go to <strong>Admin Panel</strong> then <strong>Settings</strong> then <strong>Connections</strong>.</p>
</li>
<li><p>Under <strong>Ollama API</strong>, set the URL:</p>
<ul>
<li><p>Docker install: <code>http://host.docker.internal:11434</code></p>
</li>
<li><p>pip install: <code>http://localhost:11434</code></p>
</li>
</ul>
</li>
<li><p>Click the refresh icon to verify the connection, then save.</p>
</li>
</ol>
<p>Head back to the main chat screen, and <code>medgemma</code> should now appear in the model dropdown at the top.</p>
<p>You can check the troubleshooting section below if you face any errors.</p>
<h2 id="heading-step-6-start-chatting-with-medgemma">Step 6: Start Chatting with MedGemma</h2>
<p>Select <strong>medgemma</strong> from the model selector and start a conversation. A good first test might look like this:</p>
<pre><code class="language-plaintext">Summarize this radiology report in plain language a patient could understand:

"Impression: Mild cardiomegaly. Small right pleural effusion.
No focal consolidation. Degenerative changes of the thoracic spine."
</code></pre>
<p>You should get a clear, patient-friendly explanation of each finding. This "clinical language to plain language" translation is one of MedGemma's genuine strengths.</p>
<p>There are a few Open WebUI features worth knowing about:</p>
<ul>
<li><p><strong>System prompts:</strong> Click the model name and set a system prompt like <em>"You are a medical education assistant. Always explain your reasoning and cite the relevant physiology."</em> This shapes every response in the conversation.</p>
</li>
<li><p><strong>Conversation history:</strong> Every chat is saved locally and searchable from the sidebar.</p>
</li>
<li><p><strong>Multiple models:</strong> You can add <code>llama3.2</code>, <code>gemma3</code>, or any other Ollama model and compare their answers to the same medical question side by side. This is a great way to <em>see</em> the difference domain training makes.</p>
</li>
</ul>
<h2 id="heading-step-7-upload-medical-images">Step 7: Upload Medical Images</h2>
<p>This is where MedGemma really separates itself from general-purpose models. Because its vision encoder was pre-trained on medical imaging, it can meaningfully describe radiographs, skin lesions, fundus photos, and histopathology patches.</p>
<p>To try it:</p>
<ol>
<li><p>Start a new chat with <code>medgemma</code> selected.</p>
</li>
<li><p>Click the <strong>+</strong> (or image) icon in the message box, or simply drag and drop an image file.</p>
</li>
<li><p>Add a prompt alongside the image and hit send.</p>
</li>
</ol>
<p>For sample images you can test with (without touching any real patient data), try public teaching datasets like the NIH ChestX-ray14 dataset, MedPix, or Radiopaedia's teaching cases.</p>
<p>Example workflow with a chest X-ray:</p>
<pre><code class="language-plaintext">[Upload: chest_xray.png]

You are an expert radiology assistant. Describe this chest X-ray
systematically: technical quality, lungs, heart, mediastinum, bones,
and soft tissues. Then summarize the key findings.
</code></pre>
<p>MedGemma will typically walk through the image in the systematic order you asked for, which mirrors how radiologists are trained to read films.</p>
<p><strong>Two important caveats:</strong></p>
<ul>
<li><p>Ollama and Open WebUI work with standard image formats (PNG, JPEG). Clinical DICOM files need to be converted to PNG/JPEG first — a one-liner with Python libraries like <code>pydicom</code> + <code>Pillow</code>.</p>
</li>
<li><p>Never upload images containing patient-identifying information (names, MRNs, dates burned into the image) unless the data has been properly de-identified. Even on a local machine, good data hygiene is a habit worth building.</p>
</li>
</ul>
<h2 id="heading-example-prompts-to-try">Example Prompts to Try</h2>
<p>Here are prompts that showcase different capabilities. Use them as starting points:</p>
<p>Medical education:</p>
<pre><code class="language-plaintext">Create a comparison table of ACE inhibitors vs ARBs: mechanism, common examples, key side effects, and contraindications.
</code></pre>
<p>Clinical documentation:</p>
<pre><code class="language-plaintext">Convert these shorthand clinic notes into a structured SOAP note:"45F, 3d cough + fever 101F, no SOB, lungs clear, likely viral URI, supportive care, return if worse"
</code></pre>
<p>Report translation for patients:</p>
<pre><code class="language-plaintext">Explain this MRI impression to a worried patient in a reassuring but honest tone: "Small disc protrusion at L4-L5 without significant canal stenosis or nerve root compression."
</code></pre>
<p>Image analysis (with an uploaded dermatology photo):</p>
<pre><code class="language-plaintext">Describe this skin lesion using the ABCDE criteria
(Asymmetry, Border, Color, Diameter, Evolution cannot be assessed from a single image — note that explicitly).
</code></pre>
<p>Differential reasoning:</p>
<pre><code class="language-plaintext">A 60-year-old presents with sudden painless vision loss in one eye. List the top 5 differential diagnoses and the key distinguishing feature of each.
</code></pre>
<p>Notice a pattern: the best results come from prompts that give MedGemma a <strong>role</strong>, a <strong>structure</strong> to follow, and <strong>explicit constraints</strong>. That's true of all LLMs, but it matters even more in a domain where precision counts.</p>
<h2 id="heading-running-larger-models">Running Larger Models</h2>
<p>The 4B model is impressive for its size, but the 27B variant is noticeably stronger at complex clinical reasoning, longer differential diagnoses, and nuanced report interpretation.</p>
<p>The trade-off is hardware:</p>
<table>
<thead>
<tr>
<th>Model</th>
<th>Download</th>
<th>Realistic RAM/VRAM needed</th>
<th>Best for</th>
</tr>
</thead>
<tbody><tr>
<td><code>medgemma:4b</code></td>
<td>3.3 GB</td>
<td>8 GB+ RAM</td>
<td>Laptops, quick iteration, image Q&amp;A</td>
</tr>
<tr>
<td><code>medgemma:27b</code></td>
<td>17 GB</td>
<td>32 GB RAM or 24 GB VRAM</td>
<td>Deep reasoning, complex cases</td>
</tr>
</tbody></table>
<p>To try the 27B model:</p>
<pre><code class="language-shell">ollama pull medgemma:27b
ollama run medgemma:27b
</code></pre>
<p>Practical tips for larger models:</p>
<ul>
<li><p><strong>Watch your memory:</strong> Run <code>ollama ps</code> to see how much RAM/VRAM a loaded model is using and whether it's running on GPU, CPU, or split across both. A model that spills from GPU to CPU gets dramatically slower.</p>
</li>
<li><p><strong>On Apple Silicon</strong>, a 32 GB M-series Mac runs the 27B model comfortably.</p>
</li>
<li><p><strong>Free memory between models:</strong> Ollama keeps models loaded for a few minutes after use. Unload immediately with <code>ollama stop medgemma:27b</code> if you need the RAM back.</p>
</li>
<li><p><strong>Sanity-check the speed trade-off:</strong> If the 27B model generates at 2–3 tokens per second on your machine, the 4B model at 30+ tokens/second may be the better.</p>
</li>
</ul>
<p>You can keep both installed and switch between them in the Open WebUI dropdown — 4B for fast iteration, 27B when you need the deeper reasoning.</p>
<h2 id="heading-troubleshooting-guide">Troubleshooting Guide</h2>
<h3 id="heading-error-registryollamaailibrarymedgemmalatest-does-not-support-tools">Error: <code>registry.ollama.ai/library/medgemma:latest does not support tools</code></h3>
<p>This is the most common MedGemma-specific error, and it means Open WebUI is sending native tool/function definitions with your request. MedGemma (like base Gemma 3) doesn't support Ollama's tools API, so the request is rejected before the model even sees your message.</p>
<p>Hunt down whatever is attaching tools, in this order:</p>
<ol>
<li><p><strong>Model capabilities (most likely culprit):</strong> Go to the Admin Panel, then Settings, then Models, then medgemma, then uncheck <code>Builtin Tools</code>, <code>Web Search</code>, <code>Code Interpreter</code>, and <code>Terminal</code> under Capabilities, and make sure every item in the Builtin Tools checklist is unticked. Keep <code>Vision</code>, <code>File Upload</code>, and <code>File Context</code> checked. Newer Open WebUI versions enable builtin tools by default, so a fresh install will hit this immediately.</p>
</li>
<li><p><strong>Task model:</strong> Go to Admin Panel, then Settings, then Interface, and make sure neither the local nor external Task Model is set to medgemma. Background jobs like title and follow-up generation use tool calls — route them to <code>llama3.2</code> or similar.</p>
</li>
<li><p><strong>Function Calling mode:</strong> Set to <strong>Default</strong> (not Native) in the model's Advanced Params <em>and</em> in your user Settings, General, Advanced Parameters.</p>
</li>
<li><p><strong>Global functions/filters:</strong> Go to Admin Panel, then Functions, and disable the Global toggle on any active function, since global functions attach to every model.</p>
</li>
<li><p><strong>Per-chat toggles:</strong> In the message box, make sure web search and code interpreter toggles are off, and no Tools are attached via the + menu.</p>
</li>
</ol>
<p>Then start a <strong>new chat</strong> (old chats can carry stale settings) and test. To confirm the model itself is fine, run <code>ollama run medgemma "hello"</code> in your terminal. If that works, the issue is purely Open WebUI configuration.</p>
<h3 id="heading-open-webui-shows-no-models-in-the-dropdown">Open WebUI shows no models in the dropdown</h3>
<p>The container can't reach Ollama. Check that:</p>
<ul>
<li><p>Ollama is actually running: <code>curl</code> <code>http://localhost:11434</code> should return <code>Ollama is running</code>.</p>
</li>
<li><p>The connection URL in Admin Panel, Settings, Connections is <code>http://host.docker.internal:11434</code> (Docker) — <code>localhost</code> won't work from inside a container because it refers to the container itself.</p>
</li>
<li><p>On Linux, if <code>host.docker.internal</code> doesn't resolve, add <code>--network=host</code> to your <code>docker run</code> command instead and use <code>http://localhost:11434</code>.</p>
</li>
</ul>
<h3 id="heading-ollama-pull-medgemma-says-model-not-found"><code>ollama pull medgemma</code> says model not found</h3>
<p>Update Ollama, as MedGemma requires a recent version. Re-run the installer or, on macOS, click the menu bar icon and then Update. Then retry the pull.</p>
<h3 id="heading-responses-are-extremely-slow">Responses are extremely slow</h3>
<ul>
<li><p>Check <code>ollama ps</code> — if the model shows a large CPU percentage, it doesn't fit in your GPU/unified memory. Switch to the 4B model.</p>
</li>
<li><p>Close memory-hungry apps (browsers with 40 tabs are the usual suspect).</p>
</li>
<li><p>On first message, models take several seconds to load into memory, subsequent messages are much faster.</p>
</li>
</ul>
<h3 id="heading-image-upload-doesnt-work-or-the-model-ignores-the-image">Image upload doesn't work or the model ignores the image</h3>
<ul>
<li><p>Make sure you selected <code>medgemma</code> (multimodal) and not a text-only model in the dropdown.</p>
</li>
<li><p>Use PNG or JPEG. DICOM files must be converted first.</p>
</li>
<li><p>Very high-resolution images can cause issues — resize to something reasonable (e.g., 1024px on the long edge) before uploading.</p>
</li>
</ul>
<h3 id="heading-port-3000-is-already-in-use">Port 3000 is already in use</h3>
<p>Map a different host port: change <code>-p 3000:8080</code> to <code>-p 3001:8080</code> and access the UI at <code>http://localhost:3001</code>.</p>
<h3 id="heading-out-of-memory-errors-when-loading-the-27b-model">"Out of memory" errors when loading the 27B model</h3>
<p>Your machine doesn't have enough free RAM/VRAM. Stick with <code>medgemma:4b</code>, or free memory and try again. There is no shame in the 4B model — it punches well above its weight.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In this tutorial, you built a complete, private healthcare AI assistant from scratch — and it took three tools and a handful of terminal commands.</p>
<p>Let's recap what you accomplished:</p>
<ul>
<li><p>Installed Ollama and pulled MedGemma, a medically-tuned multimodal model, onto your own machine</p>
</li>
<li><p>Verified the model from the terminal, then put a full chat interface on top of it with Open WebUI</p>
</li>
<li><p>Configured the model's capabilities correctly so tool-calling features don't break a model that doesn't support them</p>
</li>
<li><p>Chatted with a model that understands radiology reports, clinical terminology, and medical images — and uploaded images for analysis</p>
</li>
<li><p>Learned how to scale up to the 27B model and how to diagnose the most common errors along the way.</p>
</li>
</ul>
<p>You now have a fully private AI assistant running entirely on your own machine. From here, you can extend it with retrieval-augmented generation (RAG), integrate it with medical imaging pipelines, or connect it to de-identified clinical datasets to build more advanced healthcare AI applications.</p>
<p>Happy building!</p>
<p><strong>Further reading:</strong></p>
<ul>
<li><p><a href="https://ollama.com/library/medgemma">MedGemma on the Ollama library</a></p>
</li>
<li><p><a href="https://developers.google.com/health-ai-developer-foundations/medgemma">MedGemma model documentation (Google Health AI Developer Foundations)</a></p>
</li>
<li><p><a href="https://github.com/ollama/ollama">Ollama documentation</a></p>
</li>
<li><p><a href="https://docs.openwebui.com/">Open WebUI documentation</a></p>
</li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Build a Browser-Based PDF Analyzer Using JavaScript ]]>
                </title>
                <description>
                    <![CDATA[ PDF files are one of the most widely used document formats for sharing reports, invoices, contracts, books, research papers, manuals, forms, and business documents. Although viewing a PDF is simple, u ]]>
                </description>
                <link>https://www.freecodecamp.org/news/build-pdf-analyzer-javascript/</link>
                <guid isPermaLink="false">6a47d7568dc454430aaca51a</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ pdf ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Tutorial ]]>
                    </category>
                
                    <category>
                        <![CDATA[ freeCodeCamp.org ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Bhavin Sheth ]]>
                </dc:creator>
                <pubDate>Fri, 03 Jul 2026 15:37:58 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/uploads/covers/5e1e335a7a1d3fcc59028c64/a009f906-4ae4-4808-99fa-a31b419f66d5.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>PDF files are one of the most widely used document formats for sharing reports, invoices, contracts, books, research papers, manuals, forms, and business documents. Although viewing a PDF is simple, understanding what's inside the document is often much more difficult.</p>
<p>For example, you may need to know how many pages a PDF contains, whether it's password protected, who created it, what metadata it includes, how much text it contains, which fonts are used, or whether the document contains embedded images.</p>
<p>Manually inspecting all of this information can be time-consuming, especially when working with large collections of PDF files.</p>
<p>A PDF Analyzer solves this problem by automatically extracting detailed information from a document. Instead of opening the file in multiple applications, users can upload a PDF once and instantly view metadata, security settings, text statistics, image information, page details, fonts, and much more.</p>
<p>In this tutorial, you'll build a browser-based PDF Analyzer using JavaScript. The application allows users to upload a PDF, preview its pages, configure analysis options, perform different levels of document analysis, inspect the extracted information, and export a complete analysis report in multiple formats.</p>
<p>Everything runs directly inside the browser without requiring a backend server, making document analysis fast, private, and secure.</p>
<p>By the end of this tutorial, you'll have a fully functional PDF Analyzer capable of examining both simple and complex PDF documents.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/ba3b5025-7320-422d-a0ca-c96858c3ea73.png" alt="allinonetools pdf tools pdf analyzer tool" style="display:block;margin:0 auto" width="574" height="277" loading="lazy">

<h2 id="heading-table-of-contents">Table of Contents</h2>
<ul>
<li><p><a href="#heading-why-pdf-analysis-is-useful">Why PDF Analysis Is Useful</a></p>
</li>
<li><p><a href="#heading-how-pdf-analysis-works">How PDF Analysis Works</a></p>
</li>
<li><p><a href="#heading-project-setup">Project Setup</a></p>
</li>
<li><p><a href="#heading-what-library-are-we-using">What Library Are We Using?</a></p>
</li>
<li><p><a href="#heading-creating-the-upload-interface">Creating the Upload Interface</a></p>
</li>
<li><p><a href="#heading-previewing-uploaded-pdf-pages">Previewing Uploaded PDF Pages</a></p>
</li>
<li><p><a href="#heading-configuring-analysis-settings">Configuring Analysis Settings</a></p>
</li>
<li><p><a href="#heading-analyzing-the-pdf">Analyzing the PDF</a></p>
</li>
<li><p><a href="#heading-displaying-the-analysis-report">Displaying the Analysis Report</a></p>
</li>
<li><p><a href="#heading-exporting-the-analysis-report">Exporting the Analysis Report</a></p>
</li>
<li><p><a href="#heading-demo-how-the-pdf-analyzer-works">Demo: How the PDF Analyzer Works</a></p>
</li>
<li><p><a href="#heading-important-notes-from-real-world-use">Important Notes from Real-World Use</a></p>
</li>
<li><p><a href="#heading-common-mistakes-to-avoid">Common Mistakes to Avoid</a></p>
</li>
<li><p><a href="#heading-conclusion">Conclusion</a></p>
</li>
</ul>
<h2 id="heading-why-pdf-analysis-is-useful">Why PDF Analysis Is Useful</h2>
<p>Most people think of a PDF as simply a document that can be viewed or printed, but every PDF contains much more information than what appears on the screen.</p>
<p>Behind every document is a collection of properties such as metadata, security settings, page information, fonts, embedded images, and document statistics. Accessing this information can help users better understand the document before editing, sharing, printing, or archiving it.</p>
<p>Businesses often receive hundreds of PDF files every day from clients, suppliers, government departments, and employees. Before these files are stored or distributed, they frequently need to be inspected to verify their contents. A PDF Analyzer makes this process much faster by automatically extracting important document information.</p>
<p>Legal professionals regularly review contracts and agreements where document properties such as creation dates, authorship, and security restrictions may be important. Instead of manually checking each document, an analyzer provides these details in seconds.</p>
<p>Educational institutions use PDF analysis when reviewing assignments, research papers, and digital course materials. Teachers and administrators can quickly inspect page counts, metadata, extracted text, and document properties before storing or distributing files.</p>
<p>Publishing companies analyze PDF files before printing books, manuals, catalogs, and magazines. Reviewing page sizes, fonts, metadata, and embedded resources helps identify formatting problems before production begins.</p>
<p>Government agencies and healthcare organizations also benefit from document analysis when processing applications, medical records, permits, forms, and official reports. Verifying document integrity before long-term storage helps reduce errors and maintain consistent records.</p>
<p>A PDF Analyzer is equally useful for developers. Before building editing tools such as watermarking, page rotation, cropping, metadata editing, or page extraction, developers often need to inspect the document structure to determine how it should be processed.</p>
<p>Because this application performs all analysis directly inside the browser, users can inspect sensitive documents without uploading them to external servers. This provides an additional layer of privacy while delivering instant results.</p>
<h2 id="heading-how-pdf-analysis-works">How PDF Analysis Works</h2>
<p>A PDF Analyzer reads the uploaded document and extracts useful information from its internal structure.</p>
<p>Once the user selects a PDF file, the browser loads the document into memory. Instead of modifying the PDF, the application examines its contents and collects various types of information that can later be displayed in a structured report.</p>
<p>The analysis begins by reading the document itself. Basic properties such as the filename, total number of pages, and file size are identified immediately.</p>
<p>Next, the application extracts metadata including the document title, author, subject, keywords, creator, producer, creation date, modification date, and PDF version.</p>
<p>The analyzer can also inspect security-related properties to determine whether the document is password protected or contains restrictions on printing, copying, or editing.</p>
<p>After processing the document structure, the application examines each page individually. It can count words, characters, images, fonts, estimate reading time, calculate speaking time, and even perform sentiment analysis on extracted text when OCR is enabled.</p>
<p>If the uploaded document consists of scanned pages instead of selectable text, OCR can be used to recognize text before analysis begins.</p>
<p>Once all information has been collected, the application generates a complete report that can be viewed inside the browser or exported as a PDF, JSON, CSV, or text file.</p>
<p>Since the entire workflow runs locally, the original document remains on the user's device throughout the process.</p>
<h2 id="heading-project-setup">Project Setup</h2>
<p>We'll build this project using standard web technologies.</p>
<p>Create the following files:</p>
<pre><code class="language-text">pdf-analyzer/

│── index.html

│── style.css

│── script.js
</code></pre>
<p>Next, include the required libraries inside <strong>index.html</strong>.</p>
<pre><code class="language-html">&lt;script src="https://unpkg.com/pdf-lib"&gt;&lt;/script&gt;

&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/4.4.168/pdf.min.js"&gt;&lt;/script&gt;

&lt;script src="https://cdn.jsdelivr.net/npm/tesseract.js@5/dist/tesseract.min.js"&gt;&lt;/script&gt;

&lt;script src="https://cdn.jsdelivr.net/npm/chart.js"&gt;&lt;/script&gt;
</code></pre>
<p>These libraries provide everything needed for PDF loading, rendering, OCR processing, and report visualization.</p>
<h2 id="heading-what-library-are-we-using">What Library Are We Using?</h2>
<p>This project combines several JavaScript libraries because no single library can perform every type of PDF analysis.</p>
<p>The primary library is <strong>PDF-lib</strong>, which allows the application to load PDF documents and access important document properties such as metadata and page information. It's lightweight, fast, and runs entirely inside modern browsers.</p>
<p>The project also uses <strong>PDF.js</strong> to render document pages for previews. This enables users to visually inspect uploaded PDFs before running the analysis.</p>
<p>For scanned documents that don't contain selectable text, <strong>Tesseract.js</strong> provides Optical Character Recognition (OCR). It recognizes text directly inside the browser, making it possible to analyze scanned PDFs without requiring any server-side processing.</p>
<p>To visualize analysis results, we'll use <strong>Chart.js</strong> for generating simple graphs and statistics such as word counts, sentiment distribution, and other document metrics.</p>
<p>Together, these libraries create a powerful browser-based PDF Analyzer capable of extracting metadata, rendering previews, recognizing scanned text, generating statistics, and exporting detailed analysis reports while keeping every document completely private.</p>
<h2 id="heading-creating-the-upload-interface">Creating the Upload Interface</h2>
<p>Every PDF workflow begins with selecting a document. Before any analysis can take place, users need a simple and reliable way to upload one or more PDF files into the browser.</p>
<p>A good upload interface should clearly indicate that only PDF documents are accepted while supporting both drag-and-drop uploads and the traditional file picker. This makes the tool easy to use regardless of whether users are working on a desktop or a mobile device.</p>
<p>In this project, the upload area acts as the entry point for the entire analysis process. When a user selects a PDF, the browser validates the file type, loads the document into memory, and prepares it for previewing and analysis. Since everything happens locally, the original PDF never leaves the user's device.</p>
<p>Our upload component displays a drag-and-drop area, a browse button, and helpful instructions that guide users through the first step of the workflow.</p>
<p>Here's the HTML for the upload area:</p>
<pre><code class="language-html">&lt;div class="upload-container"&gt;

    &lt;div id="dropZone" class="drop-zone"&gt;

        &lt;div class="upload-icon"&gt;
            ☁
        &lt;/div&gt;

        &lt;h2&gt;Drag &amp; Drop PDF Here&lt;/h2&gt;

        &lt;p&gt;Or click to browse file&lt;/p&gt;

        &lt;button id="selectPDF"&gt;
            Select PDF
        &lt;/button&gt;

        &lt;input
            type="file"
            id="pdfInput"
            accept="application/pdf"
            hidden&gt;

    &lt;/div&gt;

&lt;/div&gt;
</code></pre>
<p>Next, register the file input and handle PDF selection.</p>
<pre><code class="language-javascript">const pdfInput = document.getElementById("pdfInput");

pdfInput.addEventListener("change", async (event) =&gt; {

    const file = event.target.files[0];

    if (!file) return;

    if (file.type !== "application/pdf") {

        alert("Please select a valid PDF file.");

        return;

    }

    loadPDF(file);

});
</code></pre>
<p>This validation prevents unsupported file types from being processed while ensuring the application only loads valid PDF documents.</p>
<p>After the upload interface is complete, users can immediately select a document and move to the preview stage.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/5a12d42f-494d-434f-8717-66b7563c6c52.png" alt="PDF upload interface allowing users to drag and drop or browse for a PDF document before analysis." style="display:block;margin:0 auto" width="740" height="548" loading="lazy">

<h2 id="heading-previewing-uploaded-pdf-pages">Previewing Uploaded PDF Pages</h2>
<p>Once a PDF has been uploaded, it's helpful to display a visual preview before starting the analysis. This allows users to verify that they selected the correct document and quickly inspect its pages.</p>
<p>Instead of showing only the file name, our application renders thumbnail previews of every page in the PDF. Users can scroll through the thumbnails to inspect the document and confirm that all pages loaded successfully.</p>
<p>Displaying previews also improves the user experience because it gives immediate visual feedback while the document is being prepared for analysis.</p>
<p>The browser uses PDF.js to render each page as a canvas before converting it into an image that can be displayed inside the page preview grid.</p>
<p>The following code loads the PDF document:</p>
<pre><code class="language-javascript">const pdf = await pdfjsLib.getDocument({

    data: await file.arrayBuffer()

}).promise;
</code></pre>
<p>Next, render each page:</p>
<pre><code class="language-javascript">for (let pageNumber = 1; pageNumber &lt;= pdf.numPages; pageNumber++) {

    const page = await pdf.getPage(pageNumber);

    const viewport = page.getViewport({

        scale: 0.35

    });

    const canvas = document.createElement("canvas");

    const context = canvas.getContext("2d");

    canvas.width = viewport.width;

    canvas.height = viewport.height;

    await page.render({

        canvasContext: context,

        viewport

    }).promise;

    previewContainer.appendChild(canvas);

}
</code></pre>
<p>Each page is rendered independently, making it possible to preview documents containing dozens or even hundreds of pages.</p>
<p>The preview shown in this project displays all page thumbnails together, making it easy to verify page order before continuing.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/f74f0c85-6910-445f-b005-710c312a6281.png" alt="Uploaded PDF preview displaying page thumbnails before document analysis begins." style="display:block;margin:0 auto" width="745" height="705" loading="lazy">

<h2 id="heading-configuring-analysis-settings">Configuring Analysis Settings</h2>
<p>Before analyzing the document, users can customize how the application should examine the PDF.</p>
<p>Different documents require different levels of analysis. Some users may only need basic information such as the page count and metadata, while others may want detailed statistics about extracted text, embedded images, fonts, security permissions, and OCR results.</p>
<p>To support these different scenarios, the PDF Analyzer provides several configurable options before processing begins.</p>
<p>The first option allows users to choose which pages should be analyzed. They can analyze every page in the document or specify a custom page range when only certain pages are relevant.</p>
<p>For scanned PDFs, OCR can be enabled to recognize text that's stored as images rather than selectable characters. Users can also select the OCR language before processing starts.</p>
<p>Finally, the application offers multiple analysis levels. Basic mode extracts essential document information such as metadata and security properties. Standard mode additionally collects text and image statistics. Advanced mode performs the most detailed inspection available, including fonts, page-level statistics, OCR processing, and sentiment analysis.</p>
<p>The analysis settings panel gives users complete control over how the document should be processed while keeping the interface simple and easy to understand.</p>
<p>Here's the HTML used for the settings panel:</p>
<pre><code class="language-html">&lt;select id="analysisLevel"&gt;

    &lt;option value="basic"&gt;
        Basic (Info, Metadata, Security)
    &lt;/option&gt;

    &lt;option value="standard"&gt;
        Standard (Basic + Text &amp; Image Stats)
    &lt;/option&gt;

    &lt;option value="advanced"&gt;
        Advanced (All Features)
    &lt;/option&gt;

&lt;/select&gt;
</code></pre>
<p>Users can also enable OCR when analyzing scanned PDF documents:</p>
<pre><code class="language-javascript">const enableOCR = document.getElementById("enableOCR").checked;

const language = document.getElementById("ocrLanguage").value;

if (enableOCR) {

    console.log("OCR Enabled");

    console.log(language);

}
</code></pre>
<p>Finally, capture the selected analysis level:</p>
<pre><code class="language-javascript">const level = document.getElementById("analysisLevel").value;

switch (level) {

    case "basic":

        runBasicAnalysis();

        break;

    case "standard":

        runStandardAnalysis();

        break;

    case "advanced":

        runAdvancedAnalysis();

        break;

}
</code></pre>
<p>These settings allow the application to adapt to many different types of PDF documents, from simple text files to complex scanned reports containing images, metadata, and security restrictions.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/62bf34b8-e706-44b3-a2b9-7f6481ed98ff.png" alt="PDF analysis settings showing page selection, OCR configuration, language selection, and available analysis levels." style="display:block;margin:0 auto" width="741" height="703" loading="lazy">

<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/886c865b-fa4a-42aa-95d2-9b507cfbe43b.png" alt="OCR language selection dropdown with multiple supported languages." style="display:block;margin:0 auto" width="731" height="254" loading="lazy">

<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/39370da7-2caf-4536-9603-03562b6206ce.png" alt="Analysis level selector showing Basic, Standard, and Advanced PDF analysis modes." style="display:block;margin:0 auto" width="670" height="174" loading="lazy">

<h2 id="heading-analyzing-the-pdf">Analyzing the PDF</h2>
<p>Once the PDF has been uploaded, previewed, and the analysis options have been configured, the application is ready to examine the document.</p>
<p>Unlike editing tools that modify pages, a PDF Analyzer inspects the document and extracts useful information without changing the original file. The analyzer reads the PDF structure, examines each page, and collects information that can later be displayed in a detailed report.</p>
<p>The analysis begins by loading the uploaded document into memory. From there, the application extracts basic information such as the filename, file size, total number of pages, and document validity. It then reads metadata including the title, author, subject, creator, producer, creation date, modification date, and PDF version.</p>
<p>Depending on the selected analysis level, the application can also inspect security permissions, count words and characters, estimate reading time, identify embedded images, list fonts used throughout the document, and perform OCR on scanned PDFs. When OCR is enabled, the analyzer converts scanned images into searchable text before calculating document statistics.</p>
<p>Because the application processes everything inside the browser, users receive instant results while maintaining complete privacy.</p>
<p>The first step is loading the uploaded PDF:</p>
<pre><code class="language-javascript">async function analyzePDF(file){

    const bytes = await file.arrayBuffer();

    const pdf = await PDFLib.PDFDocument.load(bytes);

    return pdf;

}
</code></pre>
<p>Next, extract the document metadata:</p>
<pre><code class="language-javascript">const metadata = {

    title: pdf.getTitle(),

    author: pdf.getAuthor(),

    subject: pdf.getSubject(),

    creator: pdf.getCreator(),

    producer: pdf.getProducer(),

    keywords: pdf.getKeywords(),

    creationDate: pdf.getCreationDate(),

    modificationDate: pdf.getModificationDate()

};
</code></pre>
<p>Basic document information is also collected:</p>
<pre><code class="language-javascript">const fileInfo = {

    fileName: file.name,

    fileSize: file.size,

    totalPages: pdf.getPageCount(),

    valid: true

};
</code></pre>
<p>If the user selects Advanced Analysis, additional routines extract page statistics, fonts, images, OCR results, and text analysis:</p>
<pre><code class="language-javascript">if(selectedLevel === "advanced"){

    analyzeFonts();

    analyzeImages();

    analyzeText();

    performOCR();

}
</code></pre>
<p>Once every analysis step has finished, the application combines the collected information into a single report object that will be displayed in the next stage.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/f5fbb355-dabb-4536-ae09-d2c3e79c2c4c.png" alt="Analyze PDF button used to generate a complete PDF analysis report." style="display:block;margin:0 auto" width="399" height="70" loading="lazy">

<h2 id="heading-displaying-the-analysis-report">Displaying the Analysis Report</h2>
<p>After processing is complete, the application presents the collected information inside a structured report.</p>
<p>Instead of showing raw JSON or technical output, the report organizes related information into separate cards. This layout makes it much easier for users to understand large amounts of document information.</p>
<p>The first section displays basic document information, including the filename, file size, total number of pages, and validation status.</p>
<p>The metadata section contains properties such as the document title, author, subject, keywords, creator, producer, PDF version, creation date, and modification date.</p>
<p>Security information indicates whether the document is password protected and whether printing, copying, or modification restrictions are present.</p>
<p>When text analysis is enabled, the report includes the total word count, character count, average words per page, estimated reading time, and estimated speaking time. If OCR has been performed, the extracted text is also analyzed to calculate sentiment statistics.</p>
<p>Additional cards display image information, embedded fonts, and page-by-page extracted text for users who need a deeper inspection of the document.</p>
<p>The following example creates a simple report section:</p>
<pre><code class="language-javascript">function renderBasicInfo(info){

    document.getElementById("fileName").textContent = info.fileName;

    document.getElementById("pageCount").textContent = info.totalPages;

    document.getElementById("fileSize").textContent = info.fileSize;

}
</code></pre>
<p>Rendering the metadata is straightforward:</p>
<pre><code class="language-javascript">function renderMetadata(metadata){

    title.innerText = metadata.title;

    author.innerText = metadata.author;

    creator.innerText = metadata.creator;

    producer.innerText = metadata.producer;

}
</code></pre>
<p>Page-wise extracted content can also be displayed:</p>
<pre><code class="language-javascript">pages.forEach((page,index)=&gt;{

    createPageCard(

        index + 1,

        page.text

    );

});
</code></pre>
<p>Organizing the results into individual sections allows users to quickly locate the information they need without scrolling through large blocks of text.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/08b89c29-48ee-4fd2-a5c1-059c2b61e732.png" alt="PDF analysis report displaying metadata, security information, text statistics, image information, fonts, and document insights." style="display:block;margin:0 auto" width="674" height="715" loading="lazy">

<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/b84c18f4-5cfc-4ea5-82c6-c4bf1b45495d.png" alt="Page-wise extracted text generated during PDF document analysis." style="display:block;margin:0 auto" width="660" height="880" loading="lazy">

<h2 id="heading-exporting-the-analysis-report">Exporting the Analysis Report</h2>
<p>After reviewing the analysis results, users often need to save the report for future reference or share it with colleagues.</p>
<p>To support different workflows, the PDF Analyzer allows the report to be exported in several formats. Depending on the user's needs, the report can be downloaded as a PDF document, JSON file, CSV spreadsheet, or plain text file.</p>
<p>PDF reports are useful for documentation and sharing with clients or team members. JSON exports are ideal for developers who want to process the analysis programmatically. CSV files can be opened in spreadsheet applications for further analysis, while text files provide a simple human-readable version of the report.</p>
<p>Providing multiple export formats makes the analyzer suitable for business users, developers, researchers, and system administrators alike.</p>
<p>The following example creates a JSON export:</p>
<pre><code class="language-javascript">const report = JSON.stringify(

    analysisResult,

    null,

    2

);
</code></pre>
<p>Create a downloadable file:</p>
<pre><code class="language-javascript">const blob = new Blob(

    [report],

    {

        type:"application/json"

    }

);
</code></pre>
<p>Generate the download link:</p>
<pre><code class="language-javascript">const url = URL.createObjectURL(blob);

const link = document.createElement("a");

link.href = url;

link.download = "analysis-report.json";

link.click();
</code></pre>
<p>The export menu allows users to choose the most appropriate output format before downloading the completed report.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/bed0c18f-4e25-492c-88c8-24423ce0f6b1.png" alt="bed0c18f-4e25-492c-88c8-24423ce0f6b1" style="display:block;margin:0 auto" width="901" height="373" loading="lazy">

<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/39ce70f7-d2dd-4c5a-9fe1-95eeb0c0b0ba.png" alt="Export format dropdown allowing users to select PDF, JSON, CSV, or text output before downloading." style="display:block;margin:0 auto" width="631" height="201" loading="lazy">

<h2 id="heading-demo-how-the-pdf-analyzer-works">Demo: How the PDF Analyzer Works</h2>
<h3 id="heading-step-1-upload-your-pdf-file">Step 1: Upload Your PDF File</h3>
<p>The process begins by uploading a PDF document using either the drag-and-drop area or the file selection button.</p>
<p>Once a file is selected, the browser validates that it's a PDF before loading it into memory. Because the application runs entirely inside the browser, the uploaded document never leaves the user's device, making the tool suitable for confidential business reports, contracts, invoices, research papers, legal documents, and other sensitive files.</p>
<p>After the PDF is loaded successfully, the application prepares it for page preview generation and document analysis.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/cce8e552-7d7d-4ec0-ac98-0312ae9b2395.png" alt="PDF upload interface allowing users to drag and drop or browse for a PDF document before analysis." style="display:block;margin:0 auto" width="740" height="548" loading="lazy">

<h3 id="heading-step-2-preview-uploaded-pdf-pages">Step 2: Preview Uploaded PDF Pages</h3>
<p>After the document has been loaded, the application generates page previews for the uploaded PDF.</p>
<p>Displaying page thumbnails allows users to confirm that the correct file has been selected before analysis begins. Users can quickly browse through the document, inspect page order, and verify that every page has loaded successfully.</p>
<p>This visual preview also helps identify scanned pages, blank pages, or unexpected formatting issues before processing.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/80d61017-4222-44a4-8c0b-71a17e9fa3aa.png" alt="Uploaded PDF page thumbnails displayed before document analysis." style="display:block;margin:0 auto" width="745" height="705" loading="lazy">

<h3 id="heading-step-3-configure-analysis-settings">Step 3: Configure Analysis Settings</h3>
<p>Next, users configure how the PDF should be analyzed.</p>
<p>The tool allows users to choose whether every page or only a specific page range should be processed. For scanned PDFs, OCR can be enabled to recognize text stored as images, and users can select the appropriate recognition language.</p>
<p>The application also offers multiple analysis levels. Basic mode extracts essential document properties, Standard mode adds text and image statistics, and Advanced mode performs a more detailed inspection that includes fonts, OCR, page-level information, sentiment analysis, and additional document insights.</p>
<p>These settings allow users to customize the analysis based on the type of PDF they are working with.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/c6b089c7-006e-4661-8890-f093bea294e8.png" alt="PDF analysis settings showing page selection, OCR configuration, language selection, and analysis level options." style="display:block;margin:0 auto" width="741" height="703" loading="lazy">

<h3 id="heading-step-4-analyze-the-pdf">Step 4: Analyze the PDF</h3>
<p>Once the settings have been reviewed, users simply click the <strong>Analyze PDF</strong> button.</p>
<p>The browser reads the uploaded document and extracts the selected information. Depending on the chosen analysis level, the application examines metadata, security settings, page information, extracted text, fonts, embedded images, and OCR results.</p>
<p>Although large documents may require a few additional seconds, the entire analysis is completed locally without uploading the PDF to a remote server.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/8eda0352-95ba-4735-914d-3b95ff975f30.png" alt="Analyze PDF button used to generate the document analysis report." style="display:block;margin:0 auto" width="399" height="70" loading="lazy">

<h3 id="heading-step-5-review-the-analysis-report">Step 5: Review the Analysis Report</h3>
<p>After processing is complete, the application displays a comprehensive analysis report.</p>
<p>The report is divided into multiple sections that make it easy to inspect different aspects of the document. Users can review basic document information, metadata, security settings, extracted text statistics, page information, fonts, embedded images, OCR results, estimated reading time, speaking time, and sentiment analysis.</p>
<p>Each section is organized into individual cards so that important information can be located quickly.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/850b54a3-74a5-4617-bdf3-faac236a0eee.png" alt="PDF analysis report displaying metadata, security settings, text statistics, fonts, images, and document insights." style="display:block;margin:0 auto" width="674" height="715" loading="lazy">

<h3 id="heading-step-6-review-page-level-analysis">Step 6: Review Page-Level Analysis</h3>
<p>For users who need more detailed information, the application also displays page-by-page analysis.</p>
<p>Each page can include extracted text, OCR output, word count, image statistics, page dimensions, and additional information collected during processing.</p>
<p>This level of detail is especially useful when analyzing large reports, scanned books, research papers, contracts, technical documentation, and multi-page business documents.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/dea81704-27d8-44a5-a0bd-8756659fcef2.png" alt="Page-by-page PDF analysis showing extracted content and document statistics." style="display:block;margin:0 auto" width="660" height="880" loading="lazy">

<h3 id="heading-step-7-export-the-analysis-report">Step 7: Export the Analysis Report</h3>
<p>After reviewing the analysis, users can export the report for future reference.</p>
<p>The tool supports multiple export formats, including PDF, JSON, CSV, and plain text. This allows developers, researchers, businesses, and system administrators to choose the format that best fits their workflow.</p>
<p>Exported reports can be archived, shared with team members, imported into other systems, or used for additional processing.</p>
<p>Once the desired format is selected, the browser generates the report and downloads it instantly.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/3b799aaa-8c47-4670-8d5e-77bac599c550.png" alt="Export analysis report section showing download options for PDF, JSON, CSV, and text files." style="display:block;margin:0 auto" width="901" height="373" loading="lazy">

<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/2f33c51b-d52b-43e6-979b-00eaeafe3162.png" alt="Export format selector allowing users to choose PDF, JSON, CSV, or text output before downloading." style="display:block;margin:0 auto" width="631" height="201" loading="lazy">

<h2 id="heading-important-notes-from-real-world-use">Important Notes from Real-World Use</h2>
<p>A PDF Analyzer can process everything from a single-page document to large reports containing hundreds of pages. While modern browsers handle most documents efficiently, larger files containing high-resolution images or scanned pages may require additional processing time, especially when OCR is enabled.</p>
<p>Before starting the analysis, it's good practice to validate the uploaded file.</p>
<pre><code class="language-javascript">if (file.type !== "application/pdf") {

    alert("Please upload a valid PDF document.");

    return;

}
</code></pre>
<p>If OCR is enabled, remember that recognizing text from scanned pages takes longer than extracting text from a standard searchable PDF. Users should only enable OCR when it's actually needed.</p>
<pre><code class="language-javascript">if(enableOCR){

    console.log("Running OCR Analysis...");

}
</code></pre>
<p>When analyzing very large documents, processing pages individually helps reduce memory usage and keeps the browser responsive.</p>
<pre><code class="language-javascript">for(let page = 1; page &lt;= pdf.numPages; page++){

    analyzePage(page);

}
</code></pre>
<p>Before exporting the report, review the extracted information to ensure metadata, text statistics, page information, and OCR results are accurate.</p>
<h2 id="heading-common-mistakes-to-avoid">Common Mistakes to Avoid</h2>
<p>One common mistake is running OCR on documents that already contain selectable text.</p>
<p>OCR is designed for scanned PDFs where text exists only as images. Running OCR on searchable PDFs increases processing time without improving the analysis.</p>
<pre><code class="language-javascript">if(pdfContainsText){

    enableOCR = false;

}
</code></pre>
<p>Another mistake is selecting the wrong analysis level.</p>
<p>For example, users who only need metadata and document properties can choose <strong>Basic Analysis</strong> instead of <strong>Advanced Analysis</strong>, which performs additional processing such as OCR, font inspection, sentiment analysis, and image detection.</p>
<pre><code class="language-javascript">const analysisLevel = "basic";

console.log(analysisLevel);
</code></pre>
<p>Some users also forget to verify the page selection before starting the analysis.</p>
<p>When working with large reports, analyzing only the required pages can significantly reduce processing time.</p>
<pre><code class="language-javascript">const pageRange = "1-20";

console.log(pageRange);
</code></pre>
<p>Finally, always review the generated report before exporting it.</p>
<p>A quick inspection helps verify that metadata, page statistics, OCR output, document properties, and extracted text are accurate before downloading the final report.</p>
<p>Taking a few extra moments to validate the results can save considerable time when working with large document collections.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In this tutorial, you built a browser-based PDF Analyzer using JavaScript.</p>
<p>You learned how to upload PDF files, preview document pages, configure analysis options, inspect metadata, analyze document structure, extract text, perform OCR, generate detailed reports, and export the analysis in multiple formats directly from the browser.</p>
<p>More importantly, you saw how modern browsers can inspect complex PDF documents without requiring a backend server or uploading files to third-party services.</p>
<p>This approach keeps document analysis fast, private, and secure while giving users valuable insights into the contents and structure of their PDF files.</p>
<p>You can try the complete implementation here:</p>
<p><strong>PDF Analyzer:</strong> <a href="https://allinonetools.net/pdf-analyzer/">https://allinonetools.net/pdf-analyzer/</a></p>
<p>Once you understand this workflow, you can extend the project further by adding AI-powered document summarization, keyword extraction, duplicate document detection, document comparison, accessibility analysis, compliance checking, digital signature validation, or advanced reporting dashboards.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Build a Browser-Based PDF Margin Tool Using JavaScript ]]>
                </title>
                <description>
                    <![CDATA[ Adding margins to a PDF is a common task when preparing documents for printing, binding, archiving, or sharing professionally. While many PDF editors include this feature, they often require installin ]]>
                </description>
                <link>https://www.freecodecamp.org/news/build-pdf-margin-tool-javascript/</link>
                <guid isPermaLink="false">6a4540361d2a0d9b0b18d5ff</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ pdf ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Tutorial ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Online PDF Tools ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Bhavin Sheth ]]>
                </dc:creator>
                <pubDate>Wed, 01 Jul 2026 16:28:38 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/uploads/covers/5e1e335a7a1d3fcc59028c64/617f84e9-a33b-494f-b036-7583a3c22585.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Adding margins to a PDF is a common task when preparing documents for printing, binding, archiving, or sharing professionally. While many PDF editors include this feature, they often require installing desktop software or uploading files to an online service.</p>
<p>In this tutorial, you'll learn how to build a browser-based PDF Add Margins Tool using JavaScript. The application allows users to upload a PDF, preview its pages, configure custom margin values, choose measurement units, apply preset margin sizes, select specific pages, and generate an updated PDF directly inside the browser.</p>
<p>Everything runs locally on the user's device using JavaScript, which means documents remain private and no backend server is required. This approach provides fast processing while giving users complete control over how margins are applied.</p>
<p>By the end of this guide, you'll understand how to work with PDF pages, create new page dimensions, reposition existing content, and export a new PDF with the desired margins.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/35dfed35-7a7a-4f65-8e1b-837b9dd842f4.png" alt="allinonetools ppdf toolkit add margin to pdf file or any pages" style="display:block;margin:0 auto" width="571" height="219" loading="lazy">

<h2 id="heading-table-of-contents">Table of Contents</h2>
<ul>
<li><p><a href="#heading-why-pdf-margins-are-useful">Why PDF Margins Are Useful</a></p>
</li>
<li><p><a href="#heading-how-pdf-margin-editing-works">How PDF Margin Editing Works</a></p>
</li>
<li><p><a href="#heading-project-setup">Project Setup</a></p>
</li>
<li><p><a href="#heading-what-library-are-we-using">What Library Are We Using?</a></p>
</li>
<li><p><a href="#heading-creating-the-upload-interface">Creating the Upload Interface</a></p>
</li>
<li><p><a href="#heading-previewing-uploaded-pdf-pages">Previewing Uploaded PDF Pages</a></p>
</li>
<li><p><a href="#heading-configuring-margin-settings">Configuring Margin Settings</a></p>
</li>
<li><p><a href="#heading-applying-the-margins">Applying the Margins</a></p>
</li>
<li><p><a href="#heading-generating-the-updated-pdf">Generating the Updated PDF</a></p>
</li>
<li><p><a href="#heading-demo-how-the-add-margins-tool-works">Demo: How the Add Margins Tool Works</a></p>
</li>
<li><p><a href="#heading-important-notes-from-real-world-use">Important Notes from Real-World Use</a></p>
</li>
<li><p><a href="#heading-common-mistakes-to-avoid">Common Mistakes to Avoid</a></p>
</li>
<li><p><a href="#heading-conclusion">Conclusion</a></p>
</li>
</ul>
<h2 id="heading-why-pdf-margins-are-useful">Why PDF Margins Are Useful</h2>
<p>PDF documents are designed to preserve their appearance across different devices and printers, but that doesn't always mean they're ready for every use case. Many PDFs are created with very little white space around the content, making them difficult to print, bind, annotate, or archive.</p>
<p>Adding margins creates extra space around the page without changing the document's content. This additional white space improves readability, prevents content from being clipped during printing, and provides room for notes, signatures, stamps, or hole punching.</p>
<p>One of the most common uses for PDF margins is printing. Most home and office printers can't print all the way to the edge of the paper, so documents with little or no margin may lose important text or images. Adding margins ensures the entire page fits safely within the printer's printable area.</p>
<p>Margins are also essential when preparing books, manuals, reports, and training materials for binding. Without enough inner spacing, text can disappear into the binding, making the document difficult to read. Publishers often use larger inner margins or mirror margins to create professional-looking printed books.</p>
<p>Businesses regularly add margins before printing invoices, quotations, purchase orders, financial reports, contracts, and presentations. The extra space makes documents easier to file and leaves room for handwritten notes, approval stamps, signatures, or comments.</p>
<p>Students, teachers, and researchers also benefit from margin editing. Universities and educational institutions often require assignments, dissertations, and research papers to follow specific formatting guidelines, including minimum page margins. Instead of recreating the document, users can simply add the required spacing before submission.</p>
<p>Government offices, legal firms, and healthcare organizations frequently work with PDFs that must meet strict printing or filing standards. Adding consistent margins helps ensure forms, applications, agreements, medical records, and official documents are easier to print, review, and archive.</p>
<p>Another practical example comes from e-commerce businesses. Sellers who process hundreds of orders from platforms such as Amazon, Flipkart, or Meesho often print invoices, packing slips, shipping labels, and courier documents in bulk. If the content is positioned too close to the paper edges, some printers may crop important information. Adding consistent margins before printing helps prevent this issue and ensures every document prints correctly.</p>
<p>Because this tool works entirely inside the browser, users can add margins to sensitive PDF documents without uploading them to external servers. This keeps document processing fast, private, and secure while producing professional-looking PDFs that are ready for printing, sharing, binding, or long-term storage.</p>
<h2 id="heading-how-pdf-margin-editing-works">How PDF Margin Editing Works</h2>
<p>Unlike editing text inside a PDF, adding margins doesn't modify the original content. Instead, the application creates a larger page and places the existing page content inside it with the specified spacing around each edge.</p>
<p>When a user uploads a document, the browser first reads every page in the PDF. The application calculates the current page dimensions and determines how much additional space should be added to the top, bottom, left, and right sides.</p>
<p>Depending on the selected settings, the tool can either expand the overall page size while preserving the original content dimensions or keep the existing page size and reposition the content within the available area.</p>
<p>Users can also choose whether the margin changes should be applied to every page or only selected page ranges. Mirror margins are available for printed books where the inner margin alternates between left and right pages to leave room for binding.</p>
<p>After processing every selected page, the browser generates a brand-new PDF containing the updated page dimensions and revised content positioning. Because everything happens locally, no files leave the user's computer during the process.</p>
<h2 id="heading-project-setup">Project Setup</h2>
<p>Before writing any JavaScript, create a simple project structure for the application.</p>
<p>Create a new project folder and add the following files:</p>
<pre><code class="language-text">pdf-add-margins/
│
├── index.html
├── style.css
├── script.js
└── assets/
</code></pre>
<p>The HTML file contains the upload area, preview section, margin settings, and download interface.</p>
<p>The CSS file styles the application and creates the responsive layout used throughout the project.</p>
<p>The JavaScript file handles file uploads, PDF processing, page rendering, margin calculations, and generation of the updated document.</p>
<p>Because everything runs inside the browser, there's no need to configure a backend server or install server-side frameworks.</p>
<h2 id="heading-what-library-are-we-using">What Library Are We Using?</h2>
<p>This project uses <strong>PDF-lib</strong>, one of the most popular JavaScript libraries for creating and editing PDF files directly in the browser.</p>
<p>PDF-lib allows developers to load existing PDF documents, create new pages, copy pages between documents, edit document metadata, rotate pages, resize pages, crop pages, add page numbers, insert images, draw text, and export completely new PDF files without relying on external software.</p>
<p>Install PDF-lib using npm:</p>
<pre><code class="language-bash">npm install pdf-lib
</code></pre>
<p>Or include it directly from a CDN inside your HTML page:</p>
<pre><code class="language-html">&lt;script src="https://unpkg.com/pdf-lib/dist/pdf-lib.min.js"&gt;&lt;/script&gt;
</code></pre>
<p>Once the library is loaded, you can import the required objects:</p>
<pre><code class="language-javascript">const {
  PDFDocument
} = PDFLib;
</code></pre>
<p>Throughout this tutorial, PDF-lib will be responsible for loading uploaded documents, creating new page dimensions, repositioning page content according to the selected margins, and exporting the finished PDF.</p>
<h2 id="heading-creating-the-upload-interface">Creating the Upload Interface</h2>
<p>The first feature users interact with is the upload interface. A simple and intuitive upload area makes it easy to select a PDF file using either drag-and-drop or the traditional file picker.</p>
<p>In this project, the upload section accepts only PDF documents. Once a valid file is selected, the browser immediately begins loading the document and prepares it for previewing and margin editing.</p>
<p>The upload component also acts as the starting point for the entire workflow. Every action that follows (page preview, margin configuration, page selection, and PDF generation) depends on the uploaded file.</p>
<p>Because the application runs entirely inside the browser, the uploaded PDF never leaves the user's computer. This improves privacy while reducing processing time.</p>
<p>Here's a simple upload field:</p>
<pre><code class="language-html">&lt;div class="upload-box"&gt;
    &lt;input
        type="file"
        id="pdfFile"
        accept=".pdf,application/pdf"
        hidden
    &gt;

    &lt;button id="selectPDF"&gt;
        Select PDF
    &lt;/button&gt;
&lt;/div&gt;
</code></pre>
<p>Connect the upload button with JavaScript:</p>
<pre><code class="language-javascript">const input = document.getElementById("pdfFile");
const button = document.getElementById("selectPDF");

button.addEventListener("click", () =&gt; {
    input.click();
});

input.addEventListener("change", async (event) =&gt; {

    const file = event.target.files[0];

    if (!file) return;

    const bytes = await file.arrayBuffer();

    console.log("PDF Loaded", bytes);

});
</code></pre>
<p>The uploaded PDF is now available for rendering page previews and applying margin settings.</p>
<h3 id="heading-upload-interface-demo">Upload Interface Demo</h3>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/1f11396b-ba30-48c5-a0e9-b2109af7f81a.png" alt="PDF upload interface allowing users to drag and drop or select a PDF file for adding margins." style="display:block;margin:0 auto" width="628" height="665" loading="lazy">

<h2 id="heading-previewing-uploaded-pdf-pages">Previewing Uploaded PDF Pages</h2>
<p>Once the PDF has been uploaded successfully, the next step is displaying its pages.</p>
<p>Showing page previews gives users confidence that the correct document has been selected before any changes are made. It also allows them to inspect each page and decide whether margins should be applied to the entire document or only to specific pages.</p>
<p>In this project, every page is rendered as a thumbnail. Users can quickly scroll through the document and verify page order before adjusting any settings.</p>
<p>For large documents, thumbnail previews make navigation much easier than displaying one full-size page at a time.</p>
<p>The browser renders each page directly from the uploaded PDF without sending the document to a server.</p>
<p>After loading the document, each page can be rendered individually.</p>
<pre><code class="language-javascript">const pdfDoc = await PDFDocument.load(pdfBytes);

const pages = pdfDoc.getPages();

console.log("Total Pages:", pages.length);
</code></pre>
<p>Each page is then displayed inside the preview gallery.</p>
<pre><code class="language-javascript">pages.forEach((page, index) =&gt; {

    console.log(`Rendering page ${index + 1}`);

});
</code></pre>
<p>After the previews are generated, users can move on to configuring the margin settings.</p>
<h3 id="heading-preview-demo">Preview Demo</h3>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/609ff23a-a621-4c03-babd-cdea1d330bb2.png" alt="Uploaded PDF showing page thumbnail previews before margin settings are applied." style="display:block;margin:0 auto" width="750" height="567" loading="lazy">

<h2 id="heading-configuring-margin-settings">Configuring Margin Settings</h2>
<p>After verifying the uploaded document, users can configure exactly how the margins should be added.</p>
<p>Rather than applying one fixed margin to every document, the tool provides several options that make it suitable for different printing, publishing, and business workflows.</p>
<p>Users can enter custom values for the top, bottom, left, and right margins. These values can be measured in millimeters, pixels, or inches depending on the intended use.</p>
<p>For users who don't want to calculate measurements manually, the application also includes preset margin sizes such as None, Narrow, Normal, and Wide.</p>
<p>The tool supports applying margins to every page or only to a specific page range. This is especially useful when only certain pages require additional spacing.</p>
<p>For printed books and manuals, mirror margins can be enabled so that left and right pages automatically receive opposite inner margins for binding.</p>
<p>Users can also decide how the margin should be applied. Expanding the page size preserves the original content dimensions while increasing the overall page size. Alternatively, the existing page size can be maintained and the content repositioned within the available space.</p>
<p>All of these settings are configured before any processing begins, allowing users to preview and adjust everything in advance.</p>
<p>Example margin configuration:</p>
<pre><code class="language-javascript">const marginSettings = {

    top: 25.4,

    bottom: 25.4,

    left: 25.4,

    right: 25.4,

    unit: "mm",

    applyTo: "all",

    mirrorMargins: false,

    preset: "Normal",

    resizeMode: "Expand Page Size"

};
</code></pre>
<p>The selected values are then used while generating the updated PDF pages.</p>
<h3 id="heading-margin-settings-demo">Margin Settings Demo</h3>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/2435c48a-d66f-4976-a368-44f1ee3a85f6.png" alt="Margin settings panel showing custom margin values, units, presets, mirror margins, page selection mode, and page resize options." style="display:block;margin:0 auto" width="690" height="771" loading="lazy">

<h2 id="heading-applying-the-margins">Applying the Margins</h2>
<p>Once the margin settings have been configured, the application can begin processing the PDF.</p>
<p>Instead of modifying the original document directly, the tool creates a new page layout based on the selected margin values. The existing page content is then repositioned inside the newly calculated page dimensions.</p>
<p>This approach preserves the original document while generating a new PDF with additional white space around the content.</p>
<p>Depending on the selected resize mode, the application can either expand the page size to accommodate the new margins or keep the existing page size and reposition the content within the available printable area.</p>
<p>For documents containing multiple pages, the same settings can be applied to every page or only to a selected page range.</p>
<p>A simplified example looks like this:</p>
<pre><code class="language-javascript">const pages = pdfDoc.getPages();

pages.forEach((page) =&gt; {

    const { width, height } = page.getSize();

    const newWidth = width + leftMargin + rightMargin;

    const newHeight = height + topMargin + bottomMargin;

    page.setSize(newWidth, newHeight);

});
</code></pre>
<p>The application then adjusts the page content so it remains correctly positioned inside the new page dimensions.</p>
<pre><code class="language-javascript">page.translateContent(
    leftMargin,
    bottomMargin
);
</code></pre>
<p>This ensures the document content shifts into the correct position while leaving the requested space around the page edges.</p>
<h3 id="heading-applying-margins-demo">Applying Margins Demo</h3>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/a22dac1d-55e3-4cc5-aa00-1ae844b6a4de.png" alt="Add Margins button used to process the PDF and generate the updated document." style="display:block;margin:0 auto" width="694" height="144" loading="lazy">

<h2 id="heading-generating-the-updated-pdf">Generating the Updated PDF</h2>
<p>After every selected page has been processed, the browser creates a brand-new PDF containing the updated page sizes and margin layout.</p>
<p>The original PDF remains unchanged while the modified document is prepared for download.</p>
<p>Because everything happens locally, the generation process is usually very fast, even for multi-page documents.</p>
<p>Once processing is complete, the updated PDF is converted into downloadable bytes.</p>
<pre><code class="language-javascript">const pdfBytes = await pdfDoc.save();
</code></pre>
<p>A Blob object can then be created for downloading.</p>
<pre><code class="language-javascript">const blob = new Blob(
    [pdfBytes],
    {
        type: "application/pdf"
    }
);

const url = URL.createObjectURL(blob);
</code></pre>
<p>Finally, the browser starts the download.</p>
<pre><code class="language-javascript">const link = document.createElement("a");

link.href = url;

link.download = "updated-document.pdf";

link.click();
</code></pre>
<p>The generated PDF can also be previewed before downloading.</p>
<p>Users are able to review the updated document, rename the output file, view the total number of pages, check the final file size, and download the completed PDF when they are satisfied with the results.</p>
<h3 id="heading-updated-pdf-preview">Updated PDF Preview</h3>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/91c81790-f17b-41e9-b7df-bb52677daf46.png" alt="Updated PDF preview showing added margins, filename, page count, file size, page navigation controls, and download button." style="display:block;margin:0 auto" width="681" height="781" loading="lazy">

<h2 id="heading-demo-how-the-add-margins-tool-works">Demo: How the Add Margins Tool Works</h2>
<h3 id="heading-step-1-upload-your-pdf-file">Step 1: Upload Your PDF File</h3>
<p>The process begins by uploading a PDF document using either the drag-and-drop area or the file picker.</p>
<p>Once a file is selected, the browser validates that it's a PDF and loads it locally. Since all processing happens inside the browser, the document never leaves the user's device, making the tool suitable for confidential reports, contracts, invoices, and other sensitive documents.</p>
<p>After the file is loaded successfully, the application prepares the document for preview generation and margin editing.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/e7c41384-a5e3-4ccb-abec-c8e17056177d.png" alt=" PDF upload interface allowing users to select a PDF before adding margins." style="display:block;margin:0 auto" width="628" height="665" loading="lazy">

<h3 id="heading-step-2-preview-uploaded-pdf-pages">Step 2: Preview Uploaded PDF Pages</h3>
<p>After uploading the document, the application generates page previews directly inside the browser.</p>
<p>Displaying page thumbnails allows users to verify that the correct document has been selected before making any changes. For larger PDFs, the preview section also makes it easier to navigate through the document and inspect individual pages.</p>
<p>This step helps prevent mistakes before the margin settings are applied.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/7a1c681f-8b4f-4ae2-8d2e-e87ea96a07a8.png" alt="Uploaded PDF page previews displayed before margin editing." style="display:block;margin:0 auto" width="750" height="567" loading="lazy">

<h3 id="heading-step-3-configure-margin-settings">Step 3: Configure Margin Settings</h3>
<p>Next, users configure how margins should be added to the document.</p>
<p>The tool allows custom values for the top, bottom, left, and right margins while also supporting predefined presets such as None, Narrow, Normal, and Wide.</p>
<p>Users can choose whether the margins should be applied to every page or only to selected page ranges. Mirror margins are available for printed books and documents that require binding.</p>
<p>Another useful option lets users decide whether the application should expand the overall page size or reposition the existing content while keeping the original page dimensions.</p>
<p>These settings provide complete control over how the final document will appear.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/ef4ca256-537e-4523-9213-8ebf98cfd923.png" alt="Margin settings panel showing custom margins, presets, mirror margins, measurement units, page selection, and resize options." style="display:block;margin:0 auto" width="690" height="771" loading="lazy">

<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/1a21a94d-6e9b-4a59-8df4-4022ece9dcc5.png" alt="marign units setiins " style="display:block;margin:0 auto" width="306" height="151" loading="lazy">

<h3 id="heading-step-4-apply-the-margins">Step 4: Apply the Margins</h3>
<p>After reviewing the selected settings, users simply click the <strong>Add Margins</strong> button.</p>
<p>The browser processes every selected page, calculates the new page dimensions, repositions the original content, and generates an updated PDF with the requested spacing around each page.</p>
<p>If users want to work with another document, the <strong>Start Over</strong> button clears the current session without requiring a page refresh.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/f85e1e17-2466-43fc-9462-c14a356d47bb.png" alt="Add Margins button used to generate the updated PDF." style="display:block;margin:0 auto" width="694" height="144" loading="lazy">

<h3 id="heading-step-5-preview-the-updated-pdf">Step 5: Preview the Updated PDF</h3>
<p>Once processing has finished, the updated document is displayed inside the browser.</p>
<p>Users can review every page before downloading to ensure the margins have been applied correctly.</p>
<p>The preview section also includes page navigation controls, making it easy to browse through multi-page documents and confirm that every selected page has been processed successfully.</p>
<p>Reviewing the document before downloading helps catch formatting issues early and reduces the need for additional edits later.</p>
<h3 id="heading-step-6-download-the-final-pdf">Step 6: Download the Final PDF</h3>
<p>After verifying the updated document, users can download the finished PDF.</p>
<p>The final output section displays useful information including the output filename, total number of pages, and file size. Users can rename the generated document before downloading it, making file organization much easier.</p>
<p>Once everything looks correct, the updated PDF can be downloaded and immediately used for printing, sharing, binding, archiving, or submitting to organizations that require specific page margins.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/11e369ac-480d-47b6-96d1-589ead6b0365.png" alt=" Final PDF ready for download showing filename, page count, file size, download button, and Start Over option." style="display:block;margin:0 auto" width="681" height="781" loading="lazy">

<h2 id="heading-important-notes-from-real-world-use">Important Notes from Real-World Use</h2>
<p>Adding margins is generally a lightweight operation, but large PDF files containing hundreds of pages or high-resolution images may require additional processing time.</p>
<p>Before processing begins, it's a good idea to validate the uploaded file.</p>
<pre><code class="language-javascript">if (file.type !== "application/pdf") {
    alert("Please upload a valid PDF file.");
    return;
}
</code></pre>
<p>When working with large documents, verify the selected margin values before generating the final PDF.</p>
<pre><code class="language-javascript">console.log(`Top: ${topMargin}`);
console.log(`Bottom: ${bottomMargin}`);
console.log(`Left: ${leftMargin}`);
console.log(`Right: ${rightMargin}`);
</code></pre>
<p>If the document is intended for printing, preview the generated PDF to ensure that text, tables, images, and page numbers remain correctly positioned.</p>
<p>Because all processing happens locally, documents remain on the user's device throughout the entire workflow, making browser-based margin editing suitable for contracts, invoices, financial reports, legal documents, educational records, healthcare forms, and other confidential PDFs.</p>
<h2 id="heading-common-mistakes-to-avoid">Common Mistakes to Avoid</h2>
<p>One common mistake is using excessively large margin values that reduce the printable area more than necessary.</p>
<p>Always verify the selected measurements before generating the updated document.</p>
<pre><code class="language-javascript">if (leftMargin &lt; 0 || rightMargin &lt; 0) {
    alert("Margin values cannot be negative.");
}
</code></pre>
<p>Another mistake is forgetting to choose the correct page selection mode.</p>
<p>Sometimes only the first page or a specific page range requires additional margins, while the rest of the document should remain unchanged.</p>
<pre><code class="language-javascript">const applyTo = "all";

console.log(`Apply margins to: ${applyTo}`);
</code></pre>
<p>Users should also verify whether <strong>Expand Page Size</strong> or <strong>Keep Original Page Size</strong> is the correct option for their workflow. Choosing the wrong mode can affect the final layout when printing or sharing the document.</p>
<p>Finally, always review the generated PDF before downloading it.</p>
<p>Taking a few moments to inspect the updated pages helps confirm that the spacing is correct, page content remains properly aligned, and the document is ready for printing, binding, archiving, or distribution.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In this tutorial, you built a browser-based PDF Add Margins Tool using JavaScript.</p>
<p>You learned how to upload PDF files, preview document pages, configure custom margin settings, apply margins to selected pages, and generate updated PDF files directly inside the browser.</p>
<p>More importantly, you saw how modern browsers can perform PDF page layout modifications without requiring a backend server.</p>
<p>This approach keeps document processing fast, private, and easy to use while giving users complete control over page spacing.</p>
<p>You can try the live implementation here: <a href="https://allinonetools.net/add-margins-to-pdf/">AllInOneTools - Add Margin to PDF</a>.</p>
<p>Once you understand this workflow, you can extend it further by adding features such as page cropping, resizing, page numbering, watermarking, document organization, metadata editing, and other advanced PDF editing capabilities.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Build a Text Compare Tool with HTML, CSS, and JavaScript ]]>
                </title>
                <description>
                    <![CDATA[ Have you ever tried to spot the differences between two long paragraphs of text? Reading line-by-line to find a missing word or a new sentence is a massive headache. In this tutorial, you'll build you ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-build-text-compare-tool-html-css-javascript/</link>
                <guid isPermaLink="false">6a4533f11247307c0491a76f</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ HTML5 ]]>
                    </category>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Tutorial ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Bansidhar Kadiya ]]>
                </dc:creator>
                <pubDate>Wed, 01 Jul 2026 15:36:17 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/uploads/covers/5e1e335a7a1d3fcc59028c64/632b58c6-8930-421d-b17d-847b24bb0e9e.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Have you ever tried to spot the differences between two long paragraphs of text? Reading line-by-line to find a missing word or a new sentence is a massive headache.</p>
<p>In this tutorial, you'll build your very own browser-based Text Compare Tool. It will take an original piece of text, compare it against a changed version, and instantly highlight exactly what was added or removed.</p>
<p>Building this project will help you level up your JavaScript skills. You'll also create a tool that's highly secure, because everything happens locally in the user's browser. No sensitive data is ever sent to a server.</p>
<p>Let’s get started.</p>
<h2 id="heading-prerequisites">Prerequisites</h2>
<p>To follow along easily, you should know:</p>
<ul>
<li><p><strong>Basic HTML and CSS knowledge:</strong> How to structure a page and use Flexbox to put items side-by-side.</p>
</li>
<li><p><strong>Basic JavaScript knowledge:</strong> How to write functions, use arrays, and listen for button clicks.</p>
</li>
<li><p><strong>Your Setup:</strong> A code editor (like VS Code) and a web browser to view your work.</p>
</li>
</ul>
<h2 id="heading-table-of-contents"><strong>Table of Contents</strong></h2>
<ul>
<li><p><a href="#heading-step-1-set-up-your-project-files">Step 1: Set Up Your Project Files</a></p>
</li>
<li><p><a href="#heading-step-2-build-the-html-structure">Step 2: Build the HTML Structure</a></p>
</li>
<li><p><a href="#heading-step-3-style-the-tool-with-css">Step 3: Style the Tool with CSS</a></p>
</li>
<li><p><a href="#heading-step-4-write-the-javascript-engine">Step 4: Write the JavaScript Engine</a></p>
</li>
<li><p><a href="#heading-step-5-test-your-application">Step 5: Test Your Application</a></p>
</li>
<li><p><a href="#heading-conclusion">Conclusion</a></p>
</li>
</ul>
<h2 id="heading-step-1-set-up-your-project-files">Step 1: Set Up Your Project Files</h2>
<p>First, you need a place to store your code. Create a new folder on your computer and name it <code>text-compare-tool</code>.</p>
<p>Inside that folder, create three empty files:</p>
<ul>
<li><p><code>index.html</code> (This holds the structure of your app)</p>
</li>
<li><p><code>style.css</code> (This makes your app look good)</p>
</li>
<li><p><code>script.js</code> (This makes your app actually work)</p>
</li>
</ul>
<h2 id="heading-step-2-build-the-html-structure">Step 2: Build the HTML Structure</h2>
<p>Open your <code>index.html</code> file. You need to create a simple layout with two large text boxes: one for the original text, and one for the updated text.</p>
<p>Copy and paste this code into your HTML file:</p>
<pre><code class="language-html">&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
    &lt;title&gt;Text Compare Tool&lt;/title&gt;
    &lt;link rel="stylesheet" href="style.css"&gt;
&lt;/head&gt;
&lt;body&gt;

    &lt;h1&gt;Text Compare Tool&lt;/h1&gt;
    &lt;p class="description"&gt;
        Quickly find every addition and deletion between two versions of your text. Just paste them into our tool, and we’ll show you exactly what’s been changed.
    &lt;/p&gt;

    &lt;div class="container"&gt;
        
        &lt;div class="panels-wrapper"&gt;
            &lt;!-- Left Side: Original Text --&gt;
            &lt;div class="panel"&gt;
                &lt;textarea id="text1" placeholder="Paste your original text here..."&gt;&lt;/textarea&gt;
                &lt;div id="result1" class="result-box"&gt;&lt;/div&gt;
            &lt;/div&gt;
            
            &lt;!-- Right Side: Changed Text --&gt;
            &lt;div class="panel"&gt;
                &lt;textarea id="text2" placeholder="Paste your changed text here..."&gt;&lt;/textarea&gt;
                &lt;div id="result2" class="result-box"&gt;&lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;

        &lt;!-- Action Buttons --&gt;
        &lt;div class="controls"&gt;
            &lt;button class="btn-compare" onclick="compareText()"&gt;Compare&lt;/button&gt;
            &lt;button class="btn-clear" onclick="clearText()"&gt;Clear&lt;/button&gt;
        &lt;/div&gt;

    &lt;/div&gt;

    &lt;script src="script.js"&gt;&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p>Understanding the HTML:</p>
<ul>
<li><p><strong>The two panels:</strong> Inside the <code>.panels-wrapper</code>, you have a left side and a right side.</p>
</li>
<li><p><strong>Textareas vs results:</strong> Each side has a <code>&lt;textarea&gt;</code> where the user can type. Right below the text area is a <code>&lt;div&gt;</code> with the class <code>.result-box</code>. Right now, those result boxes are invisible. Later, JavaScript will hide the text areas and show the result boxes instead.</p>
</li>
<li><p><strong>The buttons:</strong> The "Compare" and "Clear" buttons are hooked up to JavaScript functions using <code>onclick</code>.</p>
</li>
</ul>
<h2 id="heading-step-3-style-the-tool-with-css">Step 3: Style the Tool with CSS</h2>
<p>A good utility tool should be easy on the eyes. You'll use a clean white and blue design, and apply soft red and green colors to highlight the text changes.</p>
<p>Open your <code>style.css</code> file and add this code:</p>
<pre><code class="language-css">:root {
    --primary-blue: #007bff;
    --background-color: #f8f9fa;
    --text-color: #202124;
    --border-color: #dadce0;
    
    /* Highlight Colors */
    --red-bg: #fce8e6;
    --red-text: #c5221f;
    --green-bg: #e6f4ea;
    --green-text: #137333;
}

body {
    font-family: Arial, sans-serif;
    background-color: var(--background-color);
    color: var(--text-color);
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 40px 20px;
    margin: 0;
}

h1 {
    margin-bottom: 10px;
}

.description {
    text-align: center;
    max-width: 600px;
    color: #5f6368;
    margin-bottom: 30px;
    line-height: 1.5;
}

.container {
    background: white;
    padding: 20px;
    border-radius: 8px;
    border: 1px solid var(--border-color);
    width: 100%;
    max-width: 1000px;
    box-shadow: 0 4px 10px rgba(0,0,0,0.05);
}

.panels-wrapper {
    display: flex;
    gap: 20px;
    margin-bottom: 20px;
}

.panel {
    flex: 1;
    display: flex;
    flex-direction: column;
}

textarea, .result-box {
    width: 100%;
    height: 300px;
    padding: 15px;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    font-size: 16px;
    line-height: 1.5;
    box-sizing: border-box;
    resize: vertical;
}

textarea:focus {
    outline: none;
    border-color: var(--primary-blue);
}

/* Hidden by default */
.result-box {
    display: none; 
    background-color: #fafafa;
    overflow-y: auto;
    white-space: pre-wrap; 
}

.controls {
    display: flex;
    justify-content: center;
    gap: 15px;
}

button {
    padding: 10px 25px;
    font-size: 16px;
    font-weight: bold;
    border: none;
    border-radius: 5px;
    cursor: pointer;
}

.btn-compare {
    background-color: var(--primary-blue);
    color: white;
}

.btn-clear {
    background-color: white;
    color: var(--primary-blue);
    border: 1px solid var(--border-color);
}

/* How the differences will look */
.deleted {
    background-color: var(--red-bg);
    color: var(--red-text);
    padding: 2px 4px;
    border-radius: 3px;
}

.added {
    background-color: var(--green-bg);
    color: var(--green-text);
    padding: 2px 4px;
    border-radius: 3px;
}
</code></pre>
<p>Understanding the CSS:</p>
<ul>
<li><p><strong>Flexbox layout:</strong> <code>display: flex;</code> inside <code>.panels-wrapper</code> is what places your two text boxes neatly side-by-side.</p>
</li>
<li><p><strong>The highlighters:</strong> The <code>.deleted</code> and <code>.added</code> classes are the most important part of the visual design. When a user deletes a word, we give it a soft red background. When they add a word, it gets a soft green background.</p>
</li>
</ul>
<p>This is what your tool will look like once it's finished:</p>
<img src="https://cdn.hashnode.com/uploads/covers/699c7b22cf5def0f6aaf982b/6676b86c-c0ea-4dec-b5b7-489e8e06f58b.png" alt="Text Compare Tool Preview" style="display:block;margin:0 auto" width="1874" height="872" loading="lazy">

<h2 id="heading-step-4-write-the-javascript-engine">Step 4: Write the JavaScript Engine</h2>
<p>Now you need to make the tool actually work. How does your computer know if a word has changed?</p>
<p>We have to write logic that breaks paragraphs down into individual words. The code will look at the original list of words and compare it to the new list. If a word from the original text is missing, it gets marked as "deleted." If a brand new word appears, it gets marked as "added."</p>
<p>Open your <code>script.js</code> file and paste in this complete, working code:</p>
<pre><code class="language-javascript">function compareText() {
    // 1. Grab the text from the text boxes
    const text1 = document.getElementById('text1').value;
    const text2 = document.getElementById('text2').value;

    // 2. Chop the text up into an array of words (and keep the spaces)
    const words1 = text1.split(/(\s+)/);
    const words2 = text2.split(/(\s+)/);

    // 3. Find the differences
    const { diff1, diff2 } = calculateDifferences(words1, words2);

    const resultBox1 = document.getElementById('result1');
    const resultBox2 = document.getElementById('result2');
    
    // 4. Turn those differences into HTML with colors
    resultBox1.innerHTML = createColoredHTML(diff1, 'deleted');
    resultBox2.innerHTML = createColoredHTML(diff2, 'added');

    // 5. Hide the text boxes and show the final results
    document.getElementById('text1').style.display = 'none';
    document.getElementById('text2').style.display = 'none';
    resultBox1.style.display = 'block';
    resultBox2.style.display = 'block';
}

// The engine that compares the two lists of words
function calculateDifferences(arr1, arr2) {
    const n = arr1.length;
    const m = arr2.length;
    
    // Create a grid to keep track of matching words
    const grid = Array.from({ length: n + 1 }, () =&gt; Array(m + 1).fill(0));

    for (let i = 1; i &lt;= n; i++) {
        for (let j = 1; j &lt;= m; j++) {
            if (arr1[i - 1] === arr2[j - 1]) {
                grid[i][j] = grid[i - 1][j - 1] + 1;
            } else {
                grid[i][j] = Math.max(grid[i - 1][j], grid[i][j - 1]);
            }
        }
    }

    let i = n, j = m;
    const diff1 = [];
    const diff2 = [];

    // Walk backwards through the grid to mark what changed
    while (i &gt; 0 || j &gt; 0) {
        if (i &gt; 0 &amp;&amp; j &gt; 0 &amp;&amp; arr1[i - 1] === arr2[j - 1]) {
            diff1.unshift({ value: arr1[i - 1], type: 'equal' });
            diff2.unshift({ value: arr2[j - 1], type: 'equal' });
            i--;
            j--;
        } else if (j &gt; 0 &amp;&amp; (i === 0 || grid[i][j - 1] &gt;= grid[i - 1][j])) {
            diff2.unshift({ value: arr2[j - 1], type: 'changed' });
            j--;
        } else if (i &gt; 0 &amp;&amp; (j === 0 || grid[i][j - 1] &lt; grid[i - 1][j])) {
            diff1.unshift({ value: arr1[i - 1], type: 'changed' });
            i--;
        }
    }

    return { diff1, diff2 };
}

// Packages the text safely into HTML span elements
function createColoredHTML(diffArray, colorClass) {
    return diffArray.map(wordItem =&gt; {
        // Replace dangerous characters so the browser doesn't crash
        const safeText = wordItem.value.replace(/&lt;/g, "&amp;lt;").replace(/&gt;/g, "&amp;gt;");
        
        // If the word was changed (and isn't just a blank space), wrap it in color
        if (wordItem.type === 'changed' &amp;&amp; !/^\s+$/.test(wordItem.value)) {
            return `&lt;span class="${colorClass}"&gt;${safeText}&lt;/span&gt;`;
        }
        return safeText;
    }).join('');
}

// Puts the tool back to its default state
function clearText() {
    document.getElementById('text1').value = '';
    document.getElementById('text2').value = '';
    
    document.getElementById('text1').style.display = 'block';
    document.getElementById('text2').style.display = 'block';
    
    document.getElementById('result1').style.display = 'none';
    document.getElementById('result2').style.display = 'none';
}
</code></pre>
<p>Understanding the JavaScript:</p>
<ol>
<li><p><strong>Keeping the formatting:</strong> In the first function, you see <code>.split(/(\s+)/)</code>. This splits the text up by spaces, but <em>keeps</em> the spaces and line-breaks. If you don't do this, all of the user's paragraphs will mash into one giant block of text!</p>
</li>
<li><p><strong>The grid system:</strong> The <code>calculateDifferences</code> function creates an invisible grid. It compares every word in the first box with every word in the second box. If it sees the same word in the same order, it leaves it alone. If it hits a snag, it marks the word as a change.</p>
</li>
<li><p><strong>Safety first:</strong> The <code>createColoredHTML</code> function wraps our changed words in <code>&lt;span class="added"&gt;</code> or <code>&lt;span class="deleted"&gt;</code> so CSS can color them. But before it does that, it removes any <code>&lt;</code> or <code>&gt;</code> symbols using <code>.replace()</code>. This stops hackers from pasting malicious code into your app.</p>
</li>
</ol>
<h2 id="heading-step-5-test-your-application">Step 5: Test Your Application</h2>
<p>You're completely done coding! Now it’s time to see it in action.</p>
<ol>
<li><p>Open your <code>text-compare-tool</code> folder.</p>
</li>
<li><p>Double-click the <code>index.html</code> file. It will open in your default web browser.</p>
</li>
<li><p>Type a sentence into the left box: <em>"The quick brown fox jumps over the lazy dog."</em></p>
</li>
<li><p>Type a slightly different sentence into the right box: <em>"The fast brown fox jumps over the sleepy dog."</em></p>
</li>
<li><p>Click <strong>Compare</strong>.</p>
</li>
</ol>
<p>You will instantly see the word "quick" highlight in red on the left, and the word "fast" highlight in green on the right. If you want to start over, just click <strong>Clear</strong>.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Great job! You just built a highly practical, browser-based text comparison utility using nothing but pure HTML, CSS, and JavaScript.</p>
<p>You learned how to break text into arrays, compare them using a grid-based algorithm, and manipulate the DOM to show those differences to the user safely. Because this tool relies on local browser processing, it's incredibly fast and 100% private.</p>
<p>If you want to see this exact logic running in a live production environment, or if you need to bookmark a fast tool for your own writing tasks, check out the live <a href="https://99tools.net/text-compare-tool/">Text Compare Tool</a>. Keep experimenting with the code, and happy building!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Build a Browser-Based PDF Resizer Using JavaScript ]]>
                </title>
                <description>
                    <![CDATA[ PDF documents come in many different page sizes. Some are designed for A4 paper, while others use Letter, Legal, Tabloid, or custom dimensions. This can create problems when printing, sharing, archivi ]]>
                </description>
                <link>https://www.freecodecamp.org/news/build-pdf-resizer-javascript/</link>
                <guid isPermaLink="false">6a4308a0b89cf8453d414d2e</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ webdevelopment ]]>
                    </category>
                
                    <category>
                        <![CDATA[ pdf ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Online PDF Tools ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Tutorial ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Bhavin Sheth ]]>
                </dc:creator>
                <pubDate>Tue, 30 Jun 2026 00:06:56 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/uploads/covers/5e1e335a7a1d3fcc59028c64/1b1d6bca-dec7-4b4c-8983-0b2a94ba8f8e.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>PDF documents come in many different page sizes. Some are designed for A4 paper, while others use Letter, Legal, Tabloid, or custom dimensions. This can create problems when printing, sharing, archiving, or submitting documents to organizations that require a specific page format.</p>
<p>A PDF resizing tool solves this problem by allowing users to change the page dimensions of an existing PDF without recreating the document from scratch.</p>
<p>Whether you're preparing business reports, invoices, scanned documents, eBooks, presentations, or government forms, resizing pages helps ensure documents fit the required paper size while maintaining a professional appearance.</p>
<p>In this tutorial, you'll build a browser-based PDF Resizer using JavaScript. Users will be able to upload PDF files, preview document pages, choose preset or custom page sizes, configure scaling behavior, add page margins, and generate a resized PDF directly inside the browser.</p>
<p>Everything happens locally using JavaScript, so uploaded documents never leave the user's device. This improves privacy, eliminates server-side processing, and provides a faster experience.</p>
<p>By the end of this guide, you'll understand how browser-based PDF resizing works and how to build a practical tool that can be extended with additional document editing features.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/b895666f-44f2-461f-8f10-7ad1443b35a1.png" alt="allinonetools pdf tools kit pdf resize tool" style="display:block;margin:0 auto" width="619" height="308" loading="lazy">

<h2 id="heading-table-of-contents">Table of Contents</h2>
<ul>
<li><p><a href="#heading-why-pdf-resizing-is-useful">Why PDF Resizing Is Useful</a></p>
</li>
<li><p><a href="#heading-how-pdf-resizing-works">How PDF Resizing Works</a></p>
</li>
<li><p><a href="#heading-project-setup">Project Setup</a></p>
</li>
<li><p><a href="#heading-what-library-are-we-using">What Library Are We Using?</a></p>
</li>
<li><p><a href="#heading-creating-the-upload-interface">Creating the Upload Interface</a></p>
</li>
<li><p><a href="#heading-previewing-uploaded-pdf-pages">Previewing Uploaded PDF Pages</a></p>
</li>
<li><p><a href="#heading-configuring-resize-settings">Configuring Resize Settings</a></p>
</li>
<li><p><a href="#heading-applying-the-resize">Applying the Resize</a></p>
</li>
<li><p><a href="#heading-generating-the-resized-pdf">Generating the Resized PDF</a></p>
</li>
<li><p><a href="#heading-demo-how-the-pdf-resize-tool-works">Demo: How the PDF Resize Tool Works</a></p>
</li>
<li><p><a href="#heading-important-notes-from-real-world-use">Important Notes from Real-World Use</a></p>
</li>
<li><p><a href="#heading-common-mistakes-to-avoid">Common Mistakes to Avoid</a></p>
</li>
<li><p><a href="#heading-conclusion">Conclusion</a></p>
</li>
</ul>
<h2 id="heading-why-pdf-resizing-is-useful">Why PDF Resizing Is Useful</h2>
<p>PDF files are created for many different purposes, and not every document uses the same page dimensions. A presentation might be designed for widescreen viewing, an invoice could use Letter size, a government application may require A4 paper, and engineering drawings often use Legal or Tabloid formats.</p>
<p>When documents don't match the required page size, they can be difficult to print correctly. Content may appear cut off, excessive white space may be introduced, or the document may scale incorrectly during printing.</p>
<p>A PDF resizing tool helps solve these problems by allowing users to convert pages into standardized sizes without rebuilding the document.</p>
<p>This is especially useful in everyday situations. Businesses often receive invoices and contracts from different organizations that use different paper standards. Before storing or printing these documents, employees frequently resize them to a consistent format.</p>
<p>Students regularly download lecture notes, assignments, research papers, and ebooks from different sources. Converting these files to a common page size makes printing and reading much easier.</p>
<p>Graphic designers and publishers also resize PDFs before sending artwork for commercial printing, ensuring that documents match the required dimensions of the printing service.</p>
<p>One practical example comes from e-commerce businesses. Imagine a seller who receives hundreds of shipping labels every day from platforms like Amazon, Flipkart, or Meesho. Different marketplaces may generate labels using different paper sizes. Before printing them in bulk, the seller can resize every PDF to A4 so all labels print consistently without manual adjustments.</p>
<p>Government offices, banks, educational institutions, and legal firms also work with PDFs from multiple sources. Standardizing page sizes simplifies document management, digital archiving, scanning, and future editing.</p>
<p>Instead of recreating an entire document, resizing allows users to adapt existing PDFs quickly while preserving their original content.</p>
<h2 id="heading-how-pdf-resizing-works">How PDF Resizing Works</h2>
<p>A PDF resizing tool changes the dimensions of one or more pages inside an existing PDF document while preserving the page content.</p>
<p>When a user uploads a PDF, the browser first reads the document and extracts information such as page count, page dimensions, and page objects. Instead of editing the original file directly, a new PDF is created with the selected page size, and each page is copied into the new document using the chosen scaling method.</p>
<p>Depending on the resize settings, the page content can be scaled to fill the page, stretched to match the new dimensions, centered without scaling, or cropped to fit the selected paper size.</p>
<p>Users can also choose whether the resize operation should apply to every page or only a specific page range. This is useful when only part of a document needs to be converted to another paper size.</p>
<p>Because all processing happens inside the browser, uploaded files remain on the user's device throughout the entire workflow. No documents are sent to external servers, making the tool suitable for confidential reports, contracts, invoices, educational documents, business records, and other sensitive files.</p>
<h2 id="heading-project-setup">Project Setup</h2>
<p>Create a new project folder for the PDF resizer.</p>
<p>A simple project structure looks like this:</p>
<pre><code class="language-text">pdf-resizer/
│── index.html
│── style.css
│── script.js
│── assets/
</code></pre>
<p>The HTML file contains the upload area, page preview, resize settings, and download section.</p>
<p>The CSS file handles the overall layout and responsive design.</p>
<p>The JavaScript file manages PDF loading, page previews, resize operations, and exporting the updated document.</p>
<p>Keeping the project organized makes it easier to add new features later, such as cropping, page rotation, watermarking, metadata editing, or PDF merging.</p>
<h2 id="heading-what-library-are-we-using">What Library Are We Using?</h2>
<p>This project uses <strong>pdf-lib</strong>, one of the most popular JavaScript libraries for creating and editing PDF documents directly in the browser.</p>
<p>It allows developers to:</p>
<ul>
<li><p>Read existing PDF files.</p>
</li>
<li><p>Create new PDF documents.</p>
</li>
<li><p>Copy pages between PDFs.</p>
</li>
<li><p>Resize pages.</p>
</li>
<li><p>Rotate pages.</p>
</li>
<li><p>Add text, images, and shapes.</p>
</li>
<li><p>Modify document metadata.</p>
</li>
<li><p>Export updated PDFs.</p>
</li>
</ul>
<p>Unlike many online PDF services, <strong>pdf-lib</strong> works entirely in the browser and doesn't require a backend server.</p>
<p>Install the library using npm:</p>
<pre><code class="language-bash">npm install pdf-lib
</code></pre>
<p>Or include it directly from a CDN:</p>
<pre><code class="language-html">&lt;script src="https://unpkg.com/pdf-lib/dist/pdf-lib.min.js"&gt;&lt;/script&gt;
</code></pre>
<p>Once loaded, the library exposes the <code>PDFDocument</code> object that is used throughout the application.</p>
<p>Example:</p>
<pre><code class="language-javascript">const { PDFDocument } = PDFLib;
</code></pre>
<h2 id="heading-creating-the-upload-interface">Creating the Upload Interface</h2>
<p>The first step is allowing users to upload a PDF document.</p>
<p>The interface includes a drag-and-drop area together with a traditional file picker so users can choose whichever method they prefer.</p>
<p>When a file is selected, JavaScript validates that it is a PDF before loading it into memory.</p>
<p>Here's a simple upload input:</p>
<pre><code class="language-html">&lt;input
    type="file"
    id="pdfUpload"
    accept="application/pdf"
/&gt;
</code></pre>
<p>Next, listen for file selection:</p>
<pre><code class="language-javascript">const upload = document.getElementById("pdfUpload");

upload.addEventListener("change", async (event) =&gt; {
    const file = event.target.files[0];

    if (!file) return;

    console.log(file.name);
});
</code></pre>
<p>Read the uploaded file:</p>
<pre><code class="language-javascript">const bytes = await file.arrayBuffer();

const pdfDoc = await PDFLib.PDFDocument.load(bytes);
</code></pre>
<p>At this point, the uploaded PDF is loaded into memory and is ready for preview generation and resizing.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/a8630362-557c-4cd5-9f9a-3805134884e5.png" alt="PDF upload interface with drag-and-drop area for selecting a PDF file to resize." style="display:block;margin:0 auto" width="624" height="629" loading="lazy">

<h2 id="heading-previewing-uploaded-pdf-pages">Previewing Uploaded PDF Pages</h2>
<p>After the document has been uploaded, the next step is generating page previews.</p>
<p>Displaying page thumbnails helps users confirm that the correct file has been selected before changing its size.</p>
<p>For multi-page PDFs, previewing every page also makes it easier to understand how the resize operation will affect the document.</p>
<p>Your tool displays thumbnails for every page, allowing users to visually inspect the PDF before making any modifications.</p>
<p>Developers can retrieve the total number of pages using <strong>pdf-lib</strong>:</p>
<pre><code class="language-javascript">const totalPages = pdfDoc.getPageCount();

console.log(totalPages);
</code></pre>
<p>Retrieve individual pages:</p>
<pre><code class="language-javascript">const pages = pdfDoc.getPages();

pages.forEach((page, index) =&gt; {
    console.log(`Page ${index + 1}`);
});
</code></pre>
<p>Once the pages are available, the application can generate preview thumbnails and display them inside the browser.</p>
<p>Users can then decide whether to resize the entire document or only selected pages.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/76fbd44e-c359-4239-9add-56c44356adf9.png" alt="Uploaded PDF page thumbnails displayed before resizing the document" style="display:block;margin:0 auto" width="594" height="641" loading="lazy">

<h2 id="heading-configuring-resize-settings">Configuring Resize Settings</h2>
<p>Once the PDF pages have been loaded, users need to configure how the document should be resized.</p>
<p>A flexible PDF resizing tool should support both standard paper sizes and custom dimensions while allowing users to control how the existing content fits inside the new page.</p>
<p>In this project, users can choose whether the resize operation should be applied to every page or only a specific page range.</p>
<p>For page dimensions, the tool supports popular paper formats such as A4, A5, Letter, Legal, Tabloid, and Square. If none of these meet the user's requirements, custom width and height values can also be entered.</p>
<p>Another important setting is content scaling. Depending on the document, users may want to fit the existing content inside the page, stretch it to fill the available space, keep the original size centered on the page, or crop the content to match the selected paper size.</p>
<p>The resize panel also includes margin controls so users can add additional spacing around the document if required.</p>
<p>Together, these settings provide complete control over the final page layout before generating the resized PDF.</p>
<h3 id="heading-choosing-preset-paper-sizes">Choosing Preset Paper Sizes</h3>
<p>Many users simply want to convert documents into common paper formats.</p>
<p>Instead of entering page dimensions manually, the tool provides several predefined page sizes that can be selected with a single click.</p>
<p>These presets are especially useful when preparing PDFs for printing, business reports, contracts, government forms, books, or educational documents.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/c1548fd3-d401-4714-87cc-eceffc9a3c6f.png" alt=" Preset paper size options including A4, A5, Letter, Legal, Tabloid, and Square." style="display:block;margin:0 auto" width="604" height="207" loading="lazy">

<h3 id="heading-using-custom-page-sizes">Using Custom Page Sizes</h3>
<p>If a standard paper size isn't suitable, users can enter custom page dimensions.</p>
<p>The width and height can be specified using supported measurement units, allowing documents to match exact printing or publishing requirements.</p>
<p>The <strong>Lock Aspect Ratio</strong> option ensures that the document maintains its original proportions while resizing.</p>
<h3 id="heading-selecting-content-scaling">Selecting Content Scaling</h3>
<p>Different documents require different scaling behavior.</p>
<p>The tool offers several scaling modes so users can choose the most appropriate layout for their document.</p>
<p>For example, <strong>Fit to Page</strong> scales content while preserving proportions, <strong>Stretch to Fit</strong> expands the content to fill the page completely, <strong>Keep Original Size (Center)</strong> places the original page in the center without scaling, and <strong>Crop to Fit</strong> trims overflowing content to match the selected page dimensions.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/c66fde6a-6a3e-4e36-af4a-b9c508358f34.png" alt="Content scaling options including Fit to Page, Stretch to Fit, Keep Original Size, and Crop to Fit." style="display:block;margin:0 auto" width="603" height="201" loading="lazy">

<h3 id="heading-example-resize-settings">Example Resize Settings</h3>
<p>Here's a simplified configuration object that stores the selected resize options.</p>
<pre><code class="language-javascript">const resizeOptions = {
    width: 210,
    height: 297,
    unit: "mm",
    scaleMode: "fit",
    applyTo: "all"
};
</code></pre>
<p>The selected values are then used while generating the resized PDF.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/bc1f16f4-4f38-4fd5-b553-51da4fc03ee3.png" alt="Resize settings panel showing page selection, custom dimensions, content scaling, and margin controls." style="display:block;margin:0 auto" width="628" height="687" loading="lazy">

<h2 id="heading-applying-the-resize">Applying the Resize</h2>
<p>After the resize settings have been configured, the application creates a new PDF document and copies each page into it using the selected dimensions.</p>
<p>Each page is resized according to the chosen paper size and scaling mode before being added to the new document.</p>
<p>A simplified example looks like this:</p>
<pre><code class="language-javascript">const newPdf = await PDFLib.PDFDocument.create();

const copiedPages = await newPdf.copyPages(
    pdfDoc,
    pdfDoc.getPageIndices()
);

copiedPages.forEach(page =&gt; {
    page.setSize(595, 842);

    newPdf.addPage(page);
});
</code></pre>
<p>The dimensions shown above represent an A4 page measured in PDF points.</p>
<p>Developers can replace these values with custom dimensions depending on the selected paper size.</p>
<p>After every page has been resized, the new document is ready for export.</p>
<h2 id="heading-generating-the-resized-pdf">Generating the Resized PDF</h2>
<p>Once all pages have been processed, the updated document is converted into a downloadable PDF.</p>
<p>The browser creates a binary PDF file and generates a temporary download link without sending the document to a server.</p>
<p>Saving the resized document is straightforward.</p>
<pre><code class="language-javascript">const pdfBytes = await newPdf.save();

const blob = new Blob([pdfBytes], {
    type: "application/pdf"
});

const url = URL.createObjectURL(blob);
</code></pre>
<p>The generated URL can then be attached to a download button.</p>
<pre><code class="language-javascript">const link = document.createElement("a");

link.href = url;
link.download = "resized-document.pdf";

link.click();
</code></pre>
<p>The user immediately receives the updated document with the newly selected page dimensions.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/6f390adc-e404-4381-a6de-b0b5b424f2fe.png" alt="Resize PDF button used to generate the resized document." style="display:block;margin:0 auto" width="295" height="81" loading="lazy">

<h2 id="heading-demo-how-the-pdf-resize-tool-works">Demo: How the PDF Resize Tool Works</h2>
<h3 id="heading-step-1-upload-your-pdf-file">Step 1: Upload Your PDF File</h3>
<p>The process begins by uploading a PDF document into the browser.</p>
<p>Users can either drag and drop a file into the upload area or choose a document using the file picker.</p>
<p>Once selected, the browser validates the file type and loads the document locally without uploading it to a server. This keeps the entire resizing process private and ensures sensitive documents remain on the user's device.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/6711451f-2b38-4215-b0f2-f2f9d53c6914.png" alt=" PDF upload interface with drag-and-drop area for resizing PDF pages" style="display:block;margin:0 auto" width="624" height="629" loading="lazy">

<h3 id="heading-step-2-preview-uploaded-pdf-pages">Step 2: Preview Uploaded PDF Pages</h3>
<p>After the PDF has been loaded, the tool generates page previews for the entire document.</p>
<p>Displaying thumbnails allows users to verify that the correct document has been selected before making any modifications.</p>
<p>Users can also select individual pages if they only want to resize certain parts of the document.</p>
<p>This preview step helps avoid mistakes before generating the final PDF.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/b50a41d0-cea3-44b9-bcc0-934a64b367ee.png" alt="Uploaded PDF page thumbnails displayed before resizing." style="display:block;margin:0 auto" width="622" height="727" loading="lazy">

<h3 id="heading-step-3-configure-resize-settings">Step 3: Configure Resize Settings</h3>
<p>Next, users configure how the document should be resized.</p>
<p>The tool supports standard paper sizes such as A4, A5, Letter, Legal, Tabloid, and Square, while also allowing completely custom dimensions.</p>
<p>Users can specify whether the resize operation should apply to all pages or only selected page ranges.</p>
<p>Additional options include locking the aspect ratio, choosing page orientation, selecting a content scaling mode, and adding page margins when necessary.</p>
<p>These settings provide complete control over the final document layout before processing begins.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/d01f0c31-135e-4997-8b22-23e866d17e63.png" alt="PDF resize settings showing paper size selection, page range, scaling options, and custom dimensions. " style="display:block;margin:0 auto" width="628" height="687" loading="lazy">

<h3 id="heading-step-4-generate-the-resized-pdf">Step 4: Generate the Resized PDF</h3>
<p>After reviewing the selected options, users simply click the <strong>Resize PDF</strong> button.</p>
<p>The browser processes every selected page according to the configured paper size and scaling method.</p>
<p>Because the entire operation runs locally, even large documents can usually be processed within a few seconds depending on the number of pages.</p>
<p>If users want to process another document, the <strong>Start Over</strong> button clears the current session and returns the tool to its initial state.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/a3da0b1d-6655-433c-802f-81af05787a1f.png" alt="Resize PDF button with Start Over option." style="display:block;margin:0 auto" width="295" height="81" loading="lazy">

<h3 id="heading-step-5-preview-the-resized-pdf">Step 5: Preview the Resized PDF</h3>
<p>Once processing is complete, the newly generated PDF is displayed inside the browser.</p>
<p>Users can review the resized pages before downloading the document.</p>
<p>The preview section includes page navigation controls, making it easy to move through multi-page PDFs and confirm that every page has been resized correctly.</p>
<p>Reviewing the output before download helps catch formatting issues and ensures the selected page size and scaling options produced the expected results.</p>
<h3 id="heading-step-6-download-the-final-pdf">Step 6: Download the Final PDF</h3>
<p>After confirming the resized document, users can download the updated PDF.</p>
<p>The final output section displays useful information such as the output filename, total number of pages, and file size.</p>
<p>Users may also rename the document before downloading it, making it easier to organize files after processing.</p>
<p>The <strong>Download PDF</strong> button saves the resized document, while the <strong>Start Over</strong> button allows users to upload another PDF without refreshing the page.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/12d5074e-d534-4b91-b3af-79d846e35041.png" alt="Alt text: Resized PDF ready for download showing filename, page count, file size, and download button." style="display:block;margin:0 auto" width="624" height="397" loading="lazy">

<h2 id="heading-important-notes-from-real-world-use">Important Notes from Real-World Use</h2>
<p>Resizing PDF pages can significantly improve document compatibility, but it's important to validate uploaded files before processing.</p>
<p>For example:</p>
<pre><code class="language-javascript">if (file.type !== "application/pdf") {
    alert("Please upload a valid PDF file.");
    return;
}
</code></pre>
<p>Very large PDF files may require additional processing time depending on the number of pages and embedded images.</p>
<p>When resizing documents containing hundreds of pages, processing them page by page can help reduce memory usage.</p>
<p>Before generating the final PDF, it's also a good idea to verify the selected page size.</p>
<p>For example:</p>
<pre><code class="language-javascript">console.log(`Selected Size: ${pageSize}`);
console.log(`Scale Mode: ${scaleMode}`);
</code></pre>
<p>Previewing the resized document before downloading helps ensure that text, images, and page layouts appear correctly.</p>
<p>Because everything happens inside the browser, uploaded documents remain on the user's device throughout the entire resizing process.</p>
<h2 id="heading-common-mistakes-to-avoid">Common Mistakes to Avoid</h2>
<p>One common mistake is selecting a page size that doesn't match the intended output.</p>
<p>For example, resizing a wide presentation directly to A4 portrait may cause content to become too small or appear compressed.</p>
<p>Always verify the selected paper size before processing.</p>
<pre><code class="language-javascript">if (pageWidth &lt;= 0 || pageHeight &lt;= 0) {
    alert("Invalid page dimensions.");
}
</code></pre>
<p>Another common mistake is choosing the wrong content scaling mode.</p>
<p>Stretching content may distort images and text, while cropping may remove important information from the page.</p>
<p>Users should preview different scaling modes before generating the final PDF.</p>
<p>It's also important to check whether the resize operation should apply to every page or only selected pages.</p>
<pre><code class="language-javascript">const applyMode = "all";

console.log(`Resize Mode: ${applyMode}`);
</code></pre>
<p>Finally, always review the generated PDF before downloading it.</p>
<p>Taking a few seconds to inspect page layouts, margins, and scaling can prevent unnecessary reprocessing later.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In this tutorial, you built a browser-based PDF Resizer using JavaScript.</p>
<p>You learned how to upload PDF files, preview document pages, configure page sizes, adjust scaling behavior, resize pages, and generate downloadable PDF files directly inside the browser.</p>
<p>More importantly, you saw how modern browsers can perform PDF page resizing locally without requiring a backend server.</p>
<p>This approach keeps document processing fast, private, and easy to use while giving users complete control over the final document layout.</p>
<p>If you'd like to see a working example, try the <a href="https://allinonetools.net/resize-pdf/"><strong>PDF Resize Tool</strong></a> and explore how PDF pages can be resized directly in your browser.</p>
<p>Once you understand this workflow, you can extend it further with features like page cropping, rotation, watermarking, metadata editing, page numbering, document organization, and other advanced PDF editing capabilities.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Build a Browser-Based PDF Crop Tool Using JavaScript ]]>
                </title>
                <description>
                    <![CDATA[ PDF files often contain unwanted margins, blank spaces, scanner borders, page headers, page footers, or unnecessary content around the main document area. Cropping allows users to remove these unwante ]]>
                </description>
                <link>https://www.freecodecamp.org/news/build-pdf-crop-tool-javascript/</link>
                <guid isPermaLink="false">6a2cfab7f3a6ae5b0409749b</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Online PDF Tools ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Programming Blogs ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Tutorial ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Bhavin Sheth ]]>
                </dc:creator>
                <pubDate>Sat, 13 Jun 2026 06:37:43 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/uploads/covers/5e1e335a7a1d3fcc59028c64/4849599a-a0bc-4cb7-9d14-86bb990d000d.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>PDF files often contain unwanted margins, blank spaces, scanner borders, page headers, page footers, or unnecessary content around the main document area.</p>
<p>Cropping allows users to remove these unwanted areas and focus only on the important content.</p>
<p>In this tutorial, you'll build a browser-based PDF Crop Tool using JavaScript.</p>
<p>Users will be able to upload a PDF, preview pages, select a crop area visually, apply crop settings to specific pages, generate a cropped PDF, preview the final result, and download the updated document directly from the browser.</p>
<p>Everything runs locally without requiring a backend server.</p>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ul>
<li><p><a href="#heading-why-pdf-cropping-is-useful">Why PDF Cropping Is Useful</a></p>
</li>
<li><p><a href="#heading-how-pdf-cropping-works">How PDF Cropping Works</a></p>
</li>
<li><p><a href="#heading-project-setup">Project Setup</a></p>
</li>
<li><p><a href="#heading-what-library-are-we-using">What Library Are We Using?</a></p>
</li>
<li><p><a href="#heading-creating-the-upload-interface">Creating the Upload Interface</a></p>
</li>
<li><p><a href="#heading-previewing-uploaded-pdf-pages">Previewing Uploaded PDF Pages</a></p>
</li>
<li><p><a href="#heading-configuring-crop-settings">Configuring Crop Settings</a></p>
</li>
<li><p><a href="#heading-applying-the-crop">Applying the Crop</a></p>
</li>
<li><p><a href="#heading-generating-the-cropped-pdf">Generating the Cropped PDF</a></p>
</li>
<li><p><a href="#heading-why-pdf-cropping-is-useful-in-real-world-documents">Why PDF Cropping Is Useful in Real-World Documents</a></p>
</li>
<li><p><a href="#heading-demo-cropping-pdf-files-in-the-browser">Demo: Cropping PDF Files in the Browser</a></p>
</li>
<li><p><a href="#heading-important-notes-from-real-world-use">Important Notes from Real-World Use</a></p>
</li>
<li><p><a href="#heading-common-mistakes-to-avoid">Common Mistakes to Avoid</a></p>
</li>
<li><p><a href="#heading-conclusion">Conclusion</a></p>
</li>
</ul>
<h2 id="heading-why-pdf-cropping-is-useful">Why PDF Cropping Is Useful</h2>
<p>PDF cropping is commonly used when working with scanned documents, invoices, reports, contracts, forms, ebooks, manuals, presentations, and academic documents.</p>
<p>Many PDFs contain unnecessary whitespace or scanning artifacts around the edges of the page. Cropping helps remove distractions and makes documents easier to read.</p>
<p>Businesses often crop invoices and reports before sharing them with clients. Students crop lecture notes and scanned study materials to focus on the important content. And designers frequently crop exported PDFs to remove unwanted margins before printing or publishing.</p>
<p>Cropping also reduces visual clutter and creates a cleaner, more professional document.</p>
<h2 id="heading-how-pdf-cropping-works">How PDF Cropping Works</h2>
<p>A PDF crop tool loads document pages inside the browser and allows users to define a rectangular crop area.</p>
<p>Once selected, the crop coordinates are applied to the chosen pages. The browser then generates a new PDF using only the selected content area.</p>
<p>Everything happens locally inside the browser. This means uploaded documents never leave the user's device, improving privacy and security.</p>
<h2 id="heading-project-setup">Project Setup</h2>
<p>This project is intentionally simple: you'll only need an HTML file, a JavaScript file, and a PDF processing library.</p>
<p>No backend server or database is required, as everything runs directly inside the browser.</p>
<h2 id="heading-what-library-are-we-using">What Library Are We Using?</h2>
<p>We'll use PDF-lib for PDF processing.</p>
<p>PDF-lib allows us to load PDF documents, modify page boundaries, and export updated PDF files directly in JavaScript.</p>
<p>Add the library using a CDN:</p>
<pre><code class="language-html">&lt;script src="https://unpkg.com/pdf-lib/dist/pdf-lib.min.js"&gt;&lt;/script&gt;
</code></pre>
<p>Once loaded, JavaScript can process PDF pages directly inside the browser.</p>
<h2 id="heading-creating-the-upload-interface">Creating the Upload Interface</h2>
<p>Users first upload a PDF document into the browser.</p>
<p>A simple file input works well:</p>
<pre><code class="language-html">&lt;input type="file" id="pdfInput" accept=".pdf"&gt;
</code></pre>
<p>JavaScript can detect when a file is selected:</p>
<pre><code class="language-javascript">document.getElementById("pdfInput").addEventListener("change", (event) =&gt; {
  const file = event.target.files[0];
  console.log(file.name);
});
</code></pre>
<p>Here's what the upload section looks like:</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/5e4c19dd-0946-4d94-959e-ca537a5d31d4.png" alt="PDF upload interface for browser-based PDF crop tool" style="display:block;margin:0 auto" width="824" height="665" loading="lazy">

<h2 id="heading-previewing-uploaded-pdf-pages">Previewing Uploaded PDF Pages</h2>
<p>After uploading a document, users can preview PDF pages directly inside the browser.</p>
<p>The preview area includes page navigation controls that allow users to move between pages before cropping.</p>
<p>A default crop selection area is displayed on the preview page to help users begin selecting content immediately. This makes it easier to verify the document before applying crop settings.</p>
<p>Here's what the preview section looks like:</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/0b1255e5-5616-4633-87fb-b4e8edcf3901.png" alt="PDF page preview with page navigation and crop selection area" style="display:block;margin:0 auto" width="764" height="757" loading="lazy">

<h2 id="heading-configuring-crop-settings">Configuring Crop Settings</h2>
<p>After users select a crop area on the PDF preview, they often need more precise control over how the crop should be applied.</p>
<p>A practical PDF crop tool should allow users to manually adjust crop coordinates, choose predefined page ratios, and decide which pages should receive the crop operation.</p>
<p>This flexibility is especially useful when working with scanned documents, forms, reports, ebooks, presentations, and multi-page PDFs where different pages may require different crop settings.</p>
<p>In this project, users can adjust the crop position, control the crop dimensions, choose predefined crop ratios, and decide whether the crop should be applied to the current page, all pages, or a specific page range.</p>
<p>The crop settings panel provides complete control before generating the final PDF.</p>
<p>Here's what the crop settings section looks like:</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/e4754c1f-d82e-4404-853a-a585a9cdfc14.png" alt="PDF crop settings with crop coordinates predefined ratios and page selection options" style="display:block;margin:0 auto" width="668" height="742" loading="lazy">

<h3 id="heading-reading-crop-coordinates">Reading Crop Coordinates</h3>
<p>When users drag a selection area on the preview page, the application records the crop dimensions.</p>
<p>A crop object typically contains:</p>
<pre><code class="language-javascript">const cropArea = {
  x: 173,
  y: 141,
  width: 452,
  height: 309
};
</code></pre>
<p>These values determine which portion of the page will remain visible after cropping.</p>
<h3 id="heading-applying-custom-coordinates">Applying Custom Coordinates</h3>
<p>Users can manually modify crop coordinates for more accurate results.</p>
<p>For example:</p>
<pre><code class="language-javascript">const left = parseInt(document.getElementById("cropX").value);
const top = parseInt(document.getElementById("cropY").value);
const width = parseInt(document.getElementById("cropWidth").value);
const height = parseInt(document.getElementById("cropHeight").value);
</code></pre>
<p>These values are later used when applying the crop to the PDF page.</p>
<h3 id="heading-supporting-predefined-crop-ratios">Supporting Predefined Crop Ratios</h3>
<p>Many users don't want to manually enter crop coordinates every time they crop a document.</p>
<p>In real-world situations, documents often need to follow standard page dimensions. Instead of adjusting the crop area manually, users can quickly choose a predefined ratio and let the tool apply the appropriate crop settings automatically.</p>
<p>For example, a user preparing documents for printing may choose an A4 layout, while someone working with presentation slides may prefer a landscape format. Other users may simply want to remove margins while keeping the original page proportions intact.</p>
<p>A simple example looks like this:</p>
<pre><code class="language-javascript">function applyA4Portrait() {
  cropArea = {
    x: 0,
    y: 0,
    width: 595,
    height: 842
  };
}
</code></pre>
<p>This allows users to instantly apply a standard page size.</p>
<h3 id="heading-selecting-pages-to-crop">Selecting Pages to Crop</h3>
<p>Not every page requires cropping. Some users may only want to crop a single page while leaving the rest of the document unchanged.</p>
<p>The tool supports three page selection modes:</p>
<p>Current page only:</p>
<pre><code class="language-javascript">const applyMode = "current";
</code></pre>
<p>All pages:</p>
<pre><code class="language-javascript">const applyMode = "all";
</code></pre>
<p>Specific page ranges:</p>
<pre><code class="language-javascript">const applyMode = "specific";
const pageRange = "1,3-5,10";
</code></pre>
<p>This gives users full control over where the crop should be applied.</p>
<h3 id="heading-applying-crop-settings-to-pdf-pages">Applying Crop Settings to PDF Pages</h3>
<p>Once the crop values are finalized, the selected pages can be updated using PDF-lib.</p>
<p>A simplified example looks like this:</p>
<pre><code class="language-javascript">const pages = pdfDoc.getPages();

pages.forEach((page) =&gt; {
  page.setCropBox(
    cropArea.x,
    cropArea.y,
    cropArea.width,
    cropArea.height
  );
});
</code></pre>
<p>The crop box defines the visible area that will remain in the generated PDF.</p>
<h3 id="heading-validating-crop-values">Validating Crop Values</h3>
<p>Before applying the crop, it's important to verify that users entered valid dimensions.</p>
<p>For example:</p>
<pre><code class="language-javascript">if (
  cropArea.width &lt;= 0 ||
  cropArea.height &lt;= 0
) {
  alert("Invalid crop size");
  return;
}
</code></pre>
<p>Validation helps prevent errors and ensures the final PDF is generated correctly.</p>
<p>After the crop settings are configured, users can proceed to generate the updated PDF and review the results before downloading the final document.</p>
<h2 id="heading-applying-the-crop">Applying the Crop</h2>
<p>Once the crop settings are configured, users can apply the crop operation.</p>
<p>For example:</p>
<pre><code class="language-javascript">page.setCropBox(x, y, width, height);
</code></pre>
<p>The selected crop area is applied to the chosen pages before generating the updated document.</p>
<p>Here's what the crop action section looks like:</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/0bcfb644-71b0-4f84-a08b-c0aed34ce420.png" alt="Crop PDF button and start over option" style="display:block;margin:0 auto" width="175" height="80" loading="lazy">

<h2 id="heading-generating-the-cropped-pdf">Generating the Cropped PDF</h2>
<p>After cropping is complete, the browser generates a new PDF document containing only the selected page areas.</p>
<p>For example:</p>
<pre><code class="language-javascript">const pdfBytes = await pdfDoc.save();
</code></pre>
<p>The updated file can then be previewed and downloaded.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/ac18fcf4-d632-4b32-9767-70e69667c98a.png" alt="GENERATED PDF CROP FILE SHOWING WITH ITS PREVIEW" style="display:block;margin:0 auto" width="1200" height="645" loading="lazy">

<h2 id="heading-why-pdf-cropping-is-useful-in-real-world-documents">Why PDF Cropping Is Useful in Real-World Documents</h2>
<p>PDF cropping is one of those features that seems simple at first, but it becomes incredibly useful once you start working with real-world documents.</p>
<p>Many PDFs contain content that users don't actually need. Scanned documents often include large white borders created by scanners. Screenshots converted into PDFs may contain unnecessary background areas. Reports and presentations frequently have oversized margins that waste space and make documents harder to read.</p>
<p>By cropping unwanted areas, users can focus attention on the content that actually matters. The final document becomes cleaner, easier to read, more professional, and often more suitable for printing.</p>
<p>PDF cropping is especially valuable in business environments where documents are processed in large quantities.</p>
<p>For example, many e-commerce sellers on platforms such as Flipkart, Amazon, Meesho, and other marketplaces regularly download shipping labels, invoices, and packing slips in PDF format.</p>
<p>Imagine receiving a PDF containing 100 shipping labels for customer orders. The downloaded file may include unnecessary margins, extra whitespace, instructions, or content outside the area that needs to be printed.</p>
<p>Instead of manually editing every page, users can define a crop area once and apply the same crop settings to all pages in the document. This automatically removes unwanted content from all 100 labels in a single operation.</p>
<p>The result is a cleaner PDF that contains only the information required for printing and packaging.</p>
<p>The same workflow is useful for:</p>
<ul>
<li><p>E-commerce packing slips</p>
</li>
<li><p>Warehouse barcode sheets</p>
</li>
<li><p>Courier documentation</p>
</li>
<li><p>Invoices and billing documents</p>
</li>
<li><p>Scanned contracts and agreements</p>
</li>
<li><p>Academic research papers</p>
</li>
<li><p>Government forms</p>
</li>
<li><p>Business reports and presentations</p>
</li>
<li><p>Construction drawings and engineering documents</p>
</li>
<li><p>Training manuals and internal company documentation</p>
</li>
</ul>
<p>Cropping can also significantly improve printing efficiency. When unnecessary margins are removed, the important content occupies more of the printable area, making labels, invoices, diagrams, and reports easier to read.</p>
<p>Another common use case involves scanned paperwork. Many scanner applications automatically capture extra background around a document. Cropping removes these unwanted edges and produces a cleaner digital copy without requiring image-editing software.</p>
<p>Because the crop area can be applied to the current page, all pages, or specific page ranges, users can process large PDF documents in seconds rather than manually editing pages one by one.</p>
<p>For businesses that handle hundreds of PDF files every week, this can save a significant amount of time while producing cleaner, more professional documents ready for sharing, printing, or archiving.</p>
<h2 id="heading-demo-how-the-pdf-crop-tool-works">Demo: How the PDF Crop Tool Works</h2>
<h3 id="heading-step-1-upload-a-pdf-file">Step 1: Upload a PDF File</h3>
<p>Users begin by uploading a PDF document into the browser.</p>
<p>The upload area supports drag-and-drop functionality and manual file selection.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/2d5c10b2-d151-4194-a573-7137d3097e2e.png" alt="Upload PDF file for cropping" style="display:block;margin:0 auto" width="824" height="665" loading="lazy">

<h3 id="heading-step-2-preview-the-uploaded-pdf">Step 2: Preview the Uploaded PDF</h3>
<p>After uploading the document, the browser displays a page preview.</p>
<p>Users can move between pages using the navigation controls.</p>
<p>A default crop selection area is displayed to simplify the cropping process.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/06b0efe9-161b-484f-bd8f-034deef47d79.png" alt="Uploaded PDF preview with crop selection and page navigation" style="display:block;margin:0 auto" width="764" height="757" loading="lazy">

<h3 id="heading-step-3-configure-crop-settings">Step 3: Configure Crop Settings</h3>
<p>Users can fine-tune crop coordinates, choose predefined ratios, and select which pages should receive the crop.</p>
<p>The crop can be applied to a single page, all pages, or specific page ranges.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/f5ed20db-b7de-4358-940f-b3d3ea2e7ac6.png" alt="Configure crop coordinates ratios and page settings" style="display:block;margin:0 auto" width="668" height="742" loading="lazy">

<h3 id="heading-step-4-apply-the-crop">Step 4: Apply the Crop</h3>
<p>Once everything is configured, users click the Crop PDF button.</p>
<p>The browser processes the selected pages and applies the crop settings.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/d0f1d324-1445-47bf-9809-b2a8653dbf62.png" alt="Apply crop operation to PDF pages" style="display:block;margin:0 auto" width="175" height="80" loading="lazy">

<h3 id="heading-step-5-preview-the-cropped-pdf">Step 5: Preview the Cropped PDF</h3>
<p>After processing is complete, users can preview the cropped document.</p>
<p>Page navigation controls allow users to review every cropped page before downloading.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/feee3a65-9896-4e27-8172-b0ff8bae5216.png" alt="Preview cropped PDF with page navigation controls" style="display:block;margin:0 auto" width="662" height="519" loading="lazy">

<h3 id="heading-step-6-download-the-cropped-pdf">Step 6: Download the Cropped PDF</h3>
<p>The final section displays the generated file.</p>
<p>Users can rename the document, review file details such as total pages and file size, and download the cropped PDF.</p>
<p>A Start Over button is also available for processing another file.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/f0005bca-0e60-4ef7-862f-28f61f38fea8.png" alt="Download cropped PDF with filename page count and file size details" style="display:block;margin:0 auto" width="661" height="383" loading="lazy">

<h2 id="heading-important-notes-from-real-world-use">Important Notes from Real-World Use</h2>
<p>When working with large PDF files, processing may take longer depending on the number of pages.</p>
<p>Always validate uploaded files before loading them.</p>
<p>For example:</p>
<pre><code class="language-javascript">if (!file.name.endsWith(".pdf")) {
  alert("Please upload a PDF file");
  return;
}
</code></pre>
<p>Previewing pages before downloading helps catch cropping mistakes early.</p>
<h2 id="heading-common-mistakes-to-avoid">Common Mistakes to Avoid</h2>
<p>One common mistake is selecting crop coordinates that remove important document content.</p>
<p>Another mistake is applying a crop to all pages when only specific pages should be modified.</p>
<p>For example:</p>
<pre><code class="language-javascript">if (!cropArea) {
  alert("Select a crop area first");
  return;
}
</code></pre>
<p>Always review the cropped preview before downloading the final document.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In this tutorial, you built a browser-based PDF Crop Tool using JavaScript.</p>
<p>You learned how to upload PDF files, preview pages, define crop areas, configure crop settings, apply cropping operations, generate updated PDFs, and download the final document directly from the browser.</p>
<p>More importantly, you saw how modern browsers can handle PDF editing tasks locally without requiring a backend server. This approach keeps document processing fast, private, and easy to use.</p>
<p>If you'd like to see a working example, try the <a href="https://allinonetools.net/crop-pdf/"><strong>AllinoneTools-</strong> <strong>PDF Crop Tool</strong></a> and explore how PDF pages can be cropped directly in the browser.</p>
<p>Once you understand this workflow, you can extend it further with features like PDF rotation, page organization, watermarking, metadata editing, annotations, and advanced PDF editing tools.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Build a Browser-Based PDF to Image Converter Using JavaScript ]]>
                </title>
                <description>
                    <![CDATA[ Whether it’s invoices, scanned documents, reports, certificates, or receipts, users often need to convert PDF pages into image files quickly. Modern browsers make this much easier than before. Instead ]]>
                </description>
                <link>https://www.freecodecamp.org/news/pdf-to-image-converter/</link>
                <guid isPermaLink="false">6a024b87fca21b0d4b6cbcd9</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ pdf ]]>
                    </category>
                
                    <category>
                        <![CDATA[ webdev ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Tutorial ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Bhavin Sheth ]]>
                </dc:creator>
                <pubDate>Mon, 11 May 2026 21:35:03 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/uploads/covers/5e1e335a7a1d3fcc59028c64/d412f56a-2860-4b61-a300-ab3511c34e78.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Whether it’s invoices, scanned documents, reports, certificates, or receipts, users often need to convert PDF pages into image files quickly.</p>
<p>Modern browsers make this much easier than before.</p>
<p>Instead of uploading documents to a server, we can process PDF files directly inside the browser using JavaScript. This keeps the tool fast, private, and easy to use.</p>
<p>In this tutorial, you’ll build a browser-based PDF to image converter using JavaScript.</p>
<p>The tool will support uploading PDF files, previewing pages, selecting image formats like JPG or PNG, adjusting image quality, and downloading converted images directly from the browser.</p>
<p>Everything runs entirely client-side without any backend.</p>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ol>
<li><p><a href="#heading-how-pdf-to-image-conversion-works">How PDF to Image Conversion Works</a></p>
</li>
<li><p><a href="#heading-project-setup">Project Setup</a></p>
</li>
<li><p><a href="#heading-what-library-are-we-using">What Library Are We Using?</a></p>
</li>
<li><p><a href="#heading-creating-the-upload-interface">Creating the Upload Interface</a></p>
</li>
<li><p><a href="#heading-reading-the-pdf-file">Reading the PDF File</a></p>
</li>
<li><p><a href="#heading-rendering-pdf-pages-as-images">Rendering PDF Pages as Images</a></p>
</li>
<li><p><a href="#heading-selecting-image-format-and-quality">Selecting Image Format and Quality</a></p>
</li>
<li><p><a href="#heading-generating-and-downloading-images">Generating and Downloading Images</a></p>
</li>
<li><p><a href="#heading-demo-how-the-pdf-to-image-tool-works">Demo: How the PDF to Image Tool Works</a></p>
</li>
<li><p><a href="#heading-important-notes-from-real-world-use">Important Notes from Real-World Use</a></p>
</li>
<li><p><a href="#heading-common-mistakes-to-avoid">Common Mistakes to Avoid</a></p>
</li>
<li><p><a href="#heading-conclusion">Conclusion</a></p>
</li>
</ol>
<h2 id="heading-how-pdf-to-image-conversion-works">How PDF to Image Conversion Works</h2>
<p>A browser can't directly convert PDF files into images on its own.</p>
<p>Instead, JavaScript libraries render PDF pages onto an HTML canvas, which can then be exported as image files like JPG or PNG.</p>
<p>The process starts when users upload a PDF document into the browser. JavaScript then reads the file, renders each PDF page visually onto a canvas, converts those rendered pages into image files, and finally makes them available for download.</p>
<p>Everything happens locally inside the browser.</p>
<p>This means users don't need to upload private documents to external servers, making the process faster and more privacy-friendly.</p>
<h2 id="heading-project-setup">Project Setup</h2>
<p>This project is intentionally simple. Everything runs directly inside the browser using JavaScript, so no backend or server setup is required.</p>
<p>You only need:</p>
<ul>
<li><p>an HTML file</p>
</li>
<li><p>a JavaScript file</p>
</li>
<li><p>the PDF.js library</p>
</li>
</ul>
<h2 id="heading-what-library-are-we-using">What Library Are We Using?</h2>
<p>We’ll use Mozilla’s PDF.js library to render PDF pages inside the browser.</p>
<p>Add it using a CDN:</p>
<pre><code class="language-html">&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.min.js"&gt;&lt;/script&gt;
</code></pre>
<p>Once loaded, the browser can read and render PDF pages directly using JavaScript.</p>
<h2 id="heading-creating-the-upload-interface">Creating the Upload Interface</h2>
<p>Start with a simple upload area:</p>
<pre><code class="language-html">&lt;input type="file" id="pdfUpload" accept="application/pdf"&gt;

&lt;select id="format"&gt;
  &lt;option&gt;JPG&lt;/option&gt;
  &lt;option&gt;PNG&lt;/option&gt;
  &lt;option&gt;WEBP&lt;/option&gt;
&lt;/select&gt;

&lt;input type="range" id="quality" min="10" max="100" value="90"&gt;

&lt;button onclick="convertPDF()"&gt;
  Convert to Images
&lt;/button&gt;
</code></pre>
<p>This allows users to upload PDF files directly into the browser.</p>
<p>Here’s what the upload section looks like inside the tool:</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/09e2683b-617c-4703-9e6b-78c7b25c6000.png" alt="PDF upload interface inside browser-based PDF to image converter" style="display:block;margin:0 auto" width="1398" height="681" loading="lazy">

<h2 id="heading-reading-the-pdf-file">Reading the PDF File</h2>
<p>After the file is uploaded, we need to read it using JavaScript.</p>
<p>For example:</p>
<pre><code class="language-javascript">const file = document.getElementById("pdfUpload").files[0];

const reader = new FileReader();

reader.onload = async function () {
  const typedArray = new Uint8Array(reader.result);

  const pdf = await pdfjsLib.getDocument(typedArray).promise;

  console.log(pdf.numPages);
};

reader.readAsArrayBuffer(file);
</code></pre>
<p>This loads the PDF document directly inside the browser.</p>
<p>You can then access each page individually.</p>
<h2 id="heading-rendering-pdf-pages-as-images">Rendering PDF Pages as Images</h2>
<p>Once the PDF is loaded, pages can be rendered onto a canvas.</p>
<p>For example:</p>
<pre><code class="language-javascript">const page = await pdf.getPage(1);

const viewport = page.getViewport({ scale: 2 });

const canvas = document.createElement("canvas");

const context = canvas.getContext("2d");

canvas.width = viewport.width;
canvas.height = viewport.height;

await page.render({
  canvasContext: context,
  viewport: viewport
}).promise;
</code></pre>
<p>This renders the selected PDF page visually inside the browser.</p>
<p>After rendering, the canvas can be converted into an image.</p>
<p>For example:</p>
<pre><code class="language-javascript">const imageData = canvas.toDataURL("image/jpeg", 0.9);
</code></pre>
<p>This creates a downloadable image version of the PDF page.</p>
<h2 id="heading-selecting-image-format-and-quality">Selecting Image Format and Quality</h2>
<p>Before generating the final images, users may want to customize output settings.</p>
<p>Different image formats work better for different situations.</p>
<p>For example:</p>
<ul>
<li><p>JPG works well for smaller file sizes</p>
</li>
<li><p>PNG preserves better quality</p>
</li>
<li><p>WEBP offers modern compression</p>
</li>
</ul>
<p>Users can also control image quality using a slider.</p>
<p>For example:</p>
<pre><code class="language-javascript">canvas.toDataURL("image/jpeg", 0.8);
</code></pre>
<p>The value <code>0.8</code> controls compression quality.</p>
<p>Here’s an example of image format and quality settings inside the tool:</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/e315c633-3cac-434b-9564-294bce940e99.png" alt=" Image format selection options and quality slider inside PDF to image converter" style="display:block;margin:0 auto" width="843" height="238" loading="lazy">

<h2 id="heading-generating-and-downloading-images">Generating and Downloading Images</h2>
<p>Once pages are rendered, images can be downloaded directly from the browser.</p>
<p>For example:</p>
<pre><code class="language-javascript">const link = document.createElement("a");

link.href = imageData;

link.download = `page-${pageNumber}.jpg`;

link.click();
</code></pre>
<p>This downloads the generated image instantly.</p>
<p>When working with multi-page PDFs, the same process can run for every page automatically.</p>
<p>This allows users to export complete PDF documents as separate image files.</p>
<h2 id="heading-demo-how-the-pdf-to-image-tool-works">Demo: How the PDF to Image Tool Works</h2>
<p>For this example, we’ll convert PDF pages into downloadable image files directly inside the browser.</p>
<h3 id="heading-step-1-upload-pdf-files">Step 1: Upload PDF Files</h3>
<p>Users upload one or more PDF files into the converter.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/e4722a3f-b390-46e4-bb71-a8e4a3ec7138.png" alt="Uploading PDF files into the PDF to image converter" style="display:block;margin:0 auto" width="1398" height="681" loading="lazy">

<h3 id="heading-step-2-preview-uploaded-pages">Step 2: Preview Uploaded Pages</h3>
<p>The tool generates page previews before conversion.</p>
<p>This helps users verify the uploaded document visually.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/3fcd6377-c5ac-4643-a363-7e6c9d4237c0.png" alt="Preview cards showing uploaded PDF pages before conversion" style="display:block;margin:0 auto" width="1310" height="444" loading="lazy">

<h3 id="heading-step-3-configure-output-settings">Step 3: Configure Output Settings</h3>
<p>Users can choose image format and quality settings before generating images.</p>
<p>This allows better control over output size and image clarity.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/e315c633-3cac-434b-9564-294bce940e99.png" alt="Configuring image format and quality settings before conversion" style="display:block;margin:0 auto" width="843" height="238" loading="lazy">

<h3 id="heading-step-4-convert-pdf-pages-into-images">Step 4: Convert PDF Pages into Images</h3>
<p>Once settings are configured, users click the convert button.</p>
<p>The browser processes the PDF locally and generates image files instantly.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/f5b7aaeb-3dfe-4aa3-808f-5a223dd850a1.png" alt="f5b7aaeb-3dfe-4aa3-808f-5a223dd850a1" style="display:block;margin:0 auto" width="358" height="112" loading="lazy">

<h3 id="heading-step-5-download-generated-images">Step 5: Download Generated Images</h3>
<p>After conversion, every PDF page becomes a downloadable image.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/255709e8-3c1d-4d93-9661-5774be70da5b.png" alt="Converted PDF pages exported as downloadable image files" style="display:block;margin:0 auto" width="1188" height="695" loading="lazy">

<h2 id="heading-important-notes-from-real-world-use">Important Notes from Real-World Use</h2>
<p>When working with large PDFs, performance and memory usage become important.</p>
<p>Documents with many pages can slow down rendering if everything is processed at once.</p>
<p>One practical optimization is processing pages step-by-step instead of rendering the entire document immediately.</p>
<p>For example:</p>
<pre><code class="language-javascript">for (let i = 1; i &lt;= pdf.numPages; i++) {
  const page = await pdf.getPage(i);

  // render page
}
</code></pre>
<p>This keeps browser memory usage more stable.</p>
<p>Another useful optimization is reducing render scale for large documents.</p>
<p>For example:</p>
<pre><code class="language-javascript">const viewport = page.getViewport({
  scale: 1.5
});
</code></pre>
<p>Lower scale values generate smaller image files and improve performance.</p>
<p>You can also resize generated images before export.</p>
<p>For example:</p>
<pre><code class="language-javascript">canvas.width = viewport.width;
canvas.height = viewport.height;
</code></pre>
<p>This helps reduce unnecessary file size growth.</p>
<p>Since everything runs locally inside the browser, uploaded PDF files never leave the user’s device, which improves privacy and security.</p>
<h2 id="heading-common-mistakes-to-avoid">Common Mistakes to Avoid</h2>
<p>One common mistake is not validating uploaded files before processing them.</p>
<p>For example:</p>
<pre><code class="language-javascript">if (!file || file.type !== "application/pdf") {
  alert("Please upload a valid PDF file.");
  return;
}
</code></pre>
<p>This prevents unsupported files from breaking the tool.</p>
<p>Another issue is rendering extremely large pages at very high scale values.</p>
<p>Large canvas rendering can consume a lot of memory and slow down conversion significantly.</p>
<p>Using smaller scale values usually improves performance.</p>
<p>Another common mistake is forgetting to wait for page rendering before exporting the image.</p>
<p>For example:</p>
<pre><code class="language-javascript">await page.render({
  canvasContext: context,
  viewport: viewport
}).promise;
</code></pre>
<p>Without <code>await</code>, the image may export before rendering finishes.</p>
<p>Incorrect file naming can also confuse users when multiple pages are generated.</p>
<p>Adding page numbers to filenames improves organization:</p>
<pre><code class="language-javascript">link.download = `page-${pageNumber}.jpg`;
</code></pre>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In this tutorial, you built a browser-based PDF to image converter using JavaScript.</p>
<p>You learned how to upload PDF files, render pages inside the browser, generate images, and download them directly without using a backend server.</p>
<p>More importantly, you saw how modern browsers can handle document processing tasks locally while keeping user files private.</p>
<p>This approach keeps the tool fast, lightweight, and easy to use.</p>
<p>Once you understand this workflow, you can extend it further with features like ZIP downloads, batch exports, page selection, watermarking, or image compression.</p>
<p>You can also try a real working version here:</p>
<p><a href="https://allinonetools.net/pdf-to-image-converter/">https://allinonetools.net/pdf-to-image-converter/</a></p>
<p>And that’s where things start getting really interesting.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Build a Barcode Generator Using JavaScript (Step-by-Step) ]]>
                </title>
                <description>
                    <![CDATA[ If you’ve ever worked on something like an inventory system, billing dashboard, or even a small internal tool, chances are you’ve needed to generate barcodes at some point. Most developers either rely ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-build-a-barcode-generator/</link>
                <guid isPermaLink="false">69cfdf9b21e7d63506a6957e</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ webdev ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Tutorial ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Frontend Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Bhavin Sheth ]]>
                </dc:creator>
                <pubDate>Fri, 03 Apr 2026 15:41:15 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/uploads/covers/5e1e335a7a1d3fcc59028c64/684644dd-4128-415f-94ec-cf45b2a80cad.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>If you’ve ever worked on something like an inventory system, billing dashboard, or even a small internal tool, chances are you’ve needed to generate barcodes at some point.</p>
<p>Most developers either rely on external tools or assume this requires backend processing. That’s usually where things get slower, more complex, and harder to maintain.</p>
<p>But modern browsers have quietly become powerful enough to handle this entirely on their own.</p>
<p>In this tutorial, you’ll build a barcode generator that runs completely in the browser. It won’t upload data anywhere, and it won’t require any server logic. Everything happens instantly on the client side.</p>
<p>Along the way, you’ll also learn how barcode formats work, how to validate inputs properly, and how to create a real-time preview experience that feels responsive and practical.</p>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ol>
<li><p><a href="#heading-how-barcode-generation-works">How Barcode Generation Works</a></p>
</li>
<li><p><a href="#heading-project-setup">Project Setup</a></p>
</li>
<li><p><a href="#heading-what-library-are-we-using">What Library Are We Using?</a></p>
</li>
<li><p><a href="#heading-creating-the-html-structure">Creating the HTML Structure</a></p>
</li>
<li><p><a href="#heading-adding-javascript-for-barcode-generation">Adding JavaScript for Barcode Generation</a></p>
</li>
<li><p><a href="#heading-how-the-barcode-is-generated">How the Barcode Is Generated</a></p>
</li>
<li><p><a href="#heading-types-of-barcodes-you-can-generate">Types of Barcodes You Can Generate</a></p>
</li>
<li><p><a href="#heading-adding-real-time-preview">Adding Real-Time Preview</a></p>
</li>
<li><p><a href="#heading-how-to-validate-input-properly">How to Validate Input Properly</a></p>
</li>
<li><p><a href="#heading-how-to-download-the-barcode">How to Download the Barcode</a></p>
</li>
<li><p><a href="#heading-important-notes-from-real-world-use">Important Notes from Real-World Use</a></p>
</li>
<li><p><a href="#heading-common-mistakes-to-avoid">Common Mistakes to Avoid</a></p>
</li>
<li><p><a href="#heading-demo-how-the-barcode-generator-works">Demo: How the Barcode Generator Works</a></p>
</li>
<li><p><a href="#heading-conclusion">Conclusion</a></p>
</li>
</ol>
<h2 id="heading-how-barcode-generation-works">How Barcode Generation Works</h2>
<p>A barcode is simply a visual encoding of data. Instead of displaying text directly, it represents that data using a pattern of lines and spaces.</p>
<p>Different barcode formats use different encoding rules. Some support only numbers, while others allow full text input. When you generate a barcode in the browser, you’re essentially converting user input into a structured visual pattern.</p>
<p>The key idea here is that we don’t draw these lines manually. A library takes care of encoding the data and rendering it as an SVG element, which the browser can display instantly.</p>
<h2 id="heading-project-setup">Project Setup</h2>
<p>We’ll keep this project intentionally simple so the focus stays on understanding how it works.</p>
<p>All you need is a basic HTML file, a small JavaScript file, and a barcode library. There’s no backend involved, and nothing gets stored or uploaded.</p>
<p>This makes the tool fast, private, and easy to integrate into other projects.</p>
<h2 id="heading-what-library-are-we-using">What Library Are We Using?</h2>
<p>In this project, we use the <strong>JsBarcode</strong> library.</p>
<p>It’s a lightweight JavaScript library that can generate barcodes directly inside the browser using SVG. It supports multiple formats and works without any external dependencies.</p>
<p>You can include it using a CDN:</p>
<pre><code class="language-html">&lt;script src="https://cdn.jsdelivr.net/npm/jsbarcode@3.11.5/dist/JsBarcode.all.min.js"&gt;&lt;/script&gt;
</code></pre>
<h2 id="heading-creating-the-html-structure">Creating the HTML Structure</h2>
<p>The interface is simple but practical. It includes an input field where users can enter data, a dropdown to choose the barcode format, and a preview area where the barcode is rendered.</p>
<pre><code class="language-html">&lt;input type="text" id="text" placeholder="Enter text or number"&gt;

&lt;select id="format"&gt;
  &lt;option value="CODE128"&gt;Code128&lt;/option&gt;
  &lt;option value="EAN13"&gt;EAN13&lt;/option&gt;
&lt;/select&gt;

&lt;button onclick="generateBarcode()"&gt;Generate&lt;/button&gt;

&lt;svg id="barcode"&gt;&lt;/svg&gt;
</code></pre>
<p>This structure is enough to handle input, display output, and connect everything through JavaScript.</p>
<h2 id="heading-adding-javascript-for-barcode-generation">Adding JavaScript for Barcode Generation</h2>
<p>Now we'll connect the user input to barcode generation.</p>
<pre><code class="language-javascript">function generateBarcode() {
  const text = document.getElementById("text").value;
  const format = document.getElementById("format").value;

  if (!text) {
    alert("Please enter a value");
    return;
  }

  JsBarcode("#barcode", text, {
    format: format,
    width: 2,
    height: 100,
    displayValue: true
  });
}
</code></pre>
<p>This function reads the input, checks if it exists, and then generates the barcode using the selected format.</p>
<h2 id="heading-how-the-barcode-is-generated">How the Barcode Is Generated</h2>
<p>When you call the JsBarcode function, the library handles everything behind the scenes.</p>
<p>It encodes the input into a barcode standard, converts that into a pattern of lines, and renders it as an SVG element. Because SVG is vector-based, the barcode remains sharp even when resized.</p>
<p>All of this happens instantly in the browser, which is why the experience feels fast.</p>
<h2 id="heading-types-of-barcodes-you-can-generate">Types of Barcodes You Can Generate</h2>
<p>Different barcode formats are used in different industries, and understanding them helps you build more practical tools.</p>
<ol>
<li><p><strong>Code128</strong> is the most flexible format. It supports letters, numbers, and special characters, which makes it ideal for general-purpose use.</p>
</li>
<li><p><strong>EAN-13</strong> is commonly used in retail products. It works only with 13-digit numbers, so it requires strict validation.</p>
</li>
<li><p><strong>UPC</strong> is similar to EAN and is widely used in billing systems, especially in the US. It also expects numeric input with a fixed length.</p>
</li>
<li><p><strong>Code39</strong> is simpler and supports uppercase letters and numbers, but it’s less compact compared to Code128.</p>
</li>
<li><p><strong>ITF-14</strong> is mostly used in logistics and packaging. It’s designed for numeric data and is common in shipping environments.</p>
</li>
</ol>
<p>In most cases, starting with Code128 is the safest option unless you have a specific requirement.</p>
<h2 id="heading-adding-real-time-preview">Adding Real-Time Preview</h2>
<p>One of the biggest improvements you can make to a tool like this is real-time feedback.</p>
<p>Instead of requiring users to click a button every time, you can generate the barcode as they type.</p>
<pre><code class="language-javascript">document.getElementById("text").addEventListener("input", generateBarcode);
document.getElementById("format").addEventListener("change", generateBarcode);
</code></pre>
<p>This small change makes the tool feel much more responsive.</p>
<p>As soon as the user types or changes the format, the barcode updates automatically. This is the same kind of interaction you see in polished production tools.</p>
<h2 id="heading-how-to-validate-input-properly">How to Validate Input Properly</h2>
<p>Validation is where many simple tools break.</p>
<p>Since different barcode formats have different rules, if you don’t validate input correctly, the barcode may fail silently or produce incorrect output.</p>
<p>Here’s a simple example:</p>
<pre><code class="language-javascript">function isValidInput(text, format) {
  if (format === "EAN13") {
    return /^\d{13}$/.test(text);
  }

  if (format === "UPC") {
    return /^\d{12}$/.test(text);
  }

  return text.length &gt; 0;
}
</code></pre>
<p>Then use it inside your generator:</p>
<pre><code class="language-javascript">if (!isValidInput(text, format)) {
  alert("Invalid input for selected format");
  return;
}
</code></pre>
<p>This ensures users get immediate feedback instead of confusion.</p>
<h2 id="heading-how-to-download-the-barcode">How to Download the Barcode</h2>
<p>Once the barcode is generated, you can allow users to download it.</p>
<pre><code class="language-javascript">function downloadBarcode() {
  const svg = document.getElementById("barcode");
  const serializer = new XMLSerializer();
  const source = serializer.serializeToString(svg);

  const blob = new Blob([source], { type: "image/svg+xml" });
  const url = URL.createObjectURL(blob);

  const link = document.createElement("a");
  link.href = url;
  link.download = "barcode.svg";
  link.click();
}
</code></pre>
<p>This converts the SVG into a file that can be downloaded directly from the browser.</p>
<h2 id="heading-important-notes-from-real-world-use">Important Notes from Real-World Use</h2>
<p>When building tools like this in production, small details matter.</p>
<p>Large input values can sometimes affect readability, so it’s important to test how dense the barcode becomes. Choosing the right format also makes a difference depending on whether you need flexibility or strict standards.</p>
<p>Another important detail is rendering quality. Using SVG instead of raster formats ensures that the barcode remains sharp even when printed.</p>
<h2 id="heading-common-mistakes-to-avoid">Common Mistakes to Avoid</h2>
<p>One common issue is skipping validation. This leads to broken or unreadable barcodes, especially with strict formats like EAN or UPC.</p>
<p>Another mistake is relying too much on button-based interactions. Real-time updates create a much better user experience.</p>
<p>Finally, developers sometimes forget to include the library correctly, which leads to silent failures. Always verify that your CDN is loaded.</p>
<h2 id="heading-demo-how-the-barcode-generator-works">Demo: How the Barcode Generator Works</h2>
<p>To better understand how everything comes together, here’s a quick walkthrough of how the tool works in the browser.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/3a90ba9d-0b4d-4cc6-8060-de238571e67a.png" alt="Barcode generator interface showing barcode type selection options like Code128, EAN-13, UPC and input field for entering barcode data" style="display:block;margin:0 auto" width="944" height="326" loading="lazy">

<h3 id="heading-step-1-select-a-barcode-type">Step 1: Select a Barcode Type</h3>
<p>Start by choosing the barcode format. In most cases, Code128 is a good default since it supports both text and numbers.</p>
<h3 id="heading-step-2-enter-your-data">Step 2: Enter Your Data</h3>
<p>Next, enter the value you want to encode. This could be a product ID, URL, or any text depending on the selected format.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/5e7d1655-92f8-4b38-ac92-52b8fd700aab.png" alt="Barcode customization panel with options to change bar color, background color, width, height, and display settings" style="display:block;margin:0 auto" width="916" height="327" loading="lazy">

<h3 id="heading-step-3-customize-the-design">Step 3: Customize the Design</h3>
<p>You can adjust things like bar width, height, and colors. These settings help control how the barcode looks and how readable it is in different use cases.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/8b46e3fb-bd4d-41c9-84dc-c91e36656680.png" alt="Generated barcode preview displayed in the browser based on user input" style="display:block;margin:0 auto" width="433" height="217" loading="lazy">

<h3 id="heading-step-4-generate-and-preview">Step 4: Generate and Preview</h3>
<p>As you type or change settings, the barcode updates instantly. This real-time preview makes it easier to experiment and see results immediately.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/24b16194-d38d-4e2c-be1a-cbcef646ef7f.png" alt="Download options for generated barcode in PNG, JPG, and SVG formats" style="display:block;margin:0 auto" width="440" height="145" loading="lazy">

<h3 id="heading-step-5-download-the-barcode">Step 5: Download the Barcode</h3>
<p>Once you're satisfied with the result, you can download the barcode in formats like PNG, JPG, or SVG.</p>
<p>This entire process happens in the browser, without uploading any data to a server.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In this tutorial, you built a browser-based barcode generator using JavaScript.</p>
<p>More importantly, you learned how to think about building tools that run entirely on the client side. This approach reduces complexity, improves performance, and gives users a faster experience.</p>
<p>Once you understand this pattern, you can apply it to many other tools like QR generators, image converters, and file processors.</p>
<p>And that’s where things start to get interesting.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Build a Browser-Based Image Converter with JavaScript ]]>
                </title>
                <description>
                    <![CDATA[ Image conversion is one of those small tasks developers run into occasionally. You might need to convert a PNG to JPEG to reduce size, or export an image to WebP for better performance. Most developer ]]>
                </description>
                <link>https://www.freecodecamp.org/news/build-a-browser-based-image-converter-using-javascript/</link>
                <guid isPermaLink="false">69c173e230a9b81e3a7df8f3</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ HTML5 ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Tutorial ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Bhavin Sheth ]]>
                </dc:creator>
                <pubDate>Mon, 23 Mar 2026 17:09:54 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/uploads/covers/5e1e335a7a1d3fcc59028c64/c042a600-ec0e-495b-b004-dd5a4dfb1434.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Image conversion is one of those small tasks developers run into occasionally. You might need to convert a PNG to JPEG to reduce size, or export an image to WebP for better performance.</p>
<p>Most developers use online tools for this. But there’s a problem: many of those tools upload your image to a server. That can be slow, and sometimes you don’t want to upload private files at all.</p>
<p>The good news is that modern browsers are powerful enough to handle image conversion locally using JavaScript.</p>
<p>In this tutorial, you’ll learn how to build a browser-based image converter that runs entirely in the browser. The tool converts images using JavaScript without uploading files to a server, and allows users to download the converted file instantly.</p>
<p>By the end, you’ll understand how browser-based file processing works and how to use it in your own projects.</p>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ol>
<li><p><a href="#heading-how-browser-based-image-conversion-works">How Browser-Based Image Conversion Works</a></p>
</li>
<li><p><a href="#heading-project-setup">Project Setup</a></p>
</li>
<li><p><a href="#heading-how-to-read-the-image-file-in-javascript">How to Read the Image File in JavaScript</a></p>
</li>
<li><p><a href="#heading-how-the-canvas-converts-the-image">How the Canvas Converts the Image</a></p>
</li>
<li><p><a href="#heading-how-the-download-works">How the Download Works</a></p>
</li>
<li><p><a href="#heading-why-this-approach-is-powerful">Why This Approach Is Powerful</a></p>
</li>
<li><p><a href="#heading-important-notes-from-real-world-use">Important Notes from Real-World Use</a></p>
</li>
<li><p><a href="#heading-common-mistakes-to-avoid">Common Mistakes to Avoid</a></p>
</li>
<li><p><a href="#heading-how-you-can-extend-this-project">How You Can Extend This Project</a></p>
</li>
<li><p><a href="#heading-why-browser-based-tools-are-becoming-more-popular">Why Browser-Based Tools Are Becoming More Popular</a></p>
</li>
<li><p><a href="#heading-demo-how-the-image-converter-works">Demo: How the Image Converter Works</a></p>
</li>
<li><p><a href="#heading-conclusion">Conclusion</a></p>
</li>
</ol>
<h2 id="heading-how-browser-based-image-conversion-works">How Browser-Based Image Conversion Works</h2>
<p>Before writing code, you should understand what’s happening behind the scenes.</p>
<p>Modern browsers provide several APIs that make this possible. JavaScript can read local files from a user’s device, draw images on a canvas element, and export the processed image in a different format.</p>
<p>The key pieces we’ll use are:</p>
<ul>
<li><p>File input – to select an image</p>
</li>
<li><p>FileReader – to read the file</p>
</li>
<li><p>Canvas API – to redraw and convert</p>
</li>
<li><p>toDataURL or toBlob – to export the converted image</p>
</li>
</ul>
<p>The important thing is that everything happens locally in the user’s browser. Nothing gets uploaded anywhere.</p>
<h2 id="heading-project-setup">Project Setup</h2>
<p>We’ll keep this simple with just HTML and JavaScript.</p>
<p>Create an <code>index.html</code> file:</p>
<pre><code class="language-html">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;Image Converter&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;h2&gt;Browser Image Converter&lt;/h2&gt;

&lt;input type="file" id="upload" accept="image/*"&gt;

&lt;select id="format"&gt;
  &lt;option value="image/png"&gt;PNG&lt;/option&gt;
  &lt;option value="image/jpeg"&gt;JPEG&lt;/option&gt;
  &lt;option value="image/webp"&gt;WebP&lt;/option&gt;
&lt;/select&gt;

&lt;button onclick="convertImage()"&gt;Convert&lt;/button&gt;

&lt;br&gt;&lt;br&gt;

&lt;a id="download" style="display:none;"&gt;Download Converted Image&lt;/a&gt;

&lt;script src="script.js"&gt;&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p>This simple interface includes a file upload input for selecting the image, a format selector for choosing the output format, a convert button to start the process, and a download link that appears once the image has been converted.</p>
<p>Now let’s add the logic.</p>
<h2 id="heading-how-to-read-the-image-file-in-javascript">How to Read the Image File in JavaScript</h2>
<p>Create a <code>script.js</code> file:</p>
<pre><code class="language-javascript">function convertImage() {

  const fileInput = document.getElementById("upload");
  const format = document.getElementById("format").value;

  if (!fileInput.files.length) {
    alert("Please select an image");
    return;
  }

  const file = fileInput.files[0];
  const reader = new FileReader();

  reader.onload = function(event) {

    const img = new Image();

    img.onload = function() {

      const canvas = document.createElement("canvas");
      const ctx = canvas.getContext("2d");

      canvas.width = img.width;
      canvas.height = img.height;

      ctx.drawImage(img, 0, 0);

      const converted = canvas.toDataURL(format);

      const link = document.getElementById("download");

      link.href = converted;
      link.download = "converted-image";
      link.style.display = "inline";
      link.innerText = "Download Converted Image";

    };

    img.src = event.target.result;

  };

  reader.readAsDataURL(file);

}
</code></pre>
<p>This is the core of the image converter. Let’s break down what’s happening.</p>
<h3 id="heading-how-the-canvas-converts-the-image">How the Canvas Converts the Image</h3>
<p>This line draws the image:</p>
<pre><code class="language-javascript">ctx.drawImage(img, 0, 0);
</code></pre>
<p>Now the image exists inside the canvas.</p>
<p>This line converts it:</p>
<pre><code class="language-javascript">canvas.toDataURL(format);
</code></pre>
<p>This exports the image in the selected format.</p>
<p>For example:</p>
<ul>
<li><p>PNG → image/png</p>
</li>
<li><p>JPEG → image/jpeg</p>
</li>
<li><p>WebP → image/webp</p>
</li>
</ul>
<p>This is where the conversion actually happens.</p>
<h3 id="heading-how-the-download-works"><strong>How the Download Works</strong></h3>
<p>This part creates the download:</p>
<pre><code class="language-javascript">link.href = converted;
link.download = "converted-image";
</code></pre>
<p>The browser treats it as a downloadable file. No server needed.</p>
<h3 id="heading-why-this-approach-is-powerful"><strong>Why This Approach Is Powerful</strong></h3>
<p>This technique has several advantages.</p>
<ul>
<li><p><strong>It’s fast</strong>: There is no upload time, and everything runs locally.</p>
</li>
<li><p><strong>It’s private</strong>: Files never leave the user’s device. This matters for sensitive images.</p>
</li>
<li><p><strong>It reduces server costs</strong>: You don’t need backend processing. No storage, and no processing servers.</p>
</li>
</ul>
<h2 id="heading-important-notes-from-real-world-use">Important Notes from Real-World Use</h2>
<p>If you plan to build tools like this, here are a few practical things I’ve learned.</p>
<h3 id="heading-large-images-use-more-memory">Large Images Use More Memory</h3>
<p>Very large images can slow down the browser. If needed, you can resize images using Canvas.</p>
<h3 id="heading-jpeg-supports-quality-settings">JPEG Supports Quality Settings</h3>
<p>You can control quality:</p>
<pre><code class="language-plaintext">canvas.toDataURL("image/jpeg", 0.8);
</code></pre>
<p>This reduces file size.</p>
<h3 id="heading-webp-usually-gives-the-best-compression">WebP Usually Gives the Best Compression</h3>
<p>WebP often produces smaller files than PNG or JPEG. It’s a good default option.</p>
<h3 id="heading-how-to-resize-an-image-using-canvas">How to Resize an Image Using Canvas</h3>
<p>If you need to reduce the size of large images, you can resize them before exporting.</p>
<p>After loading the image, you can set a smaller width and height on the canvas:</p>
<pre><code class="language-javascript">const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");

const maxWidth = 800;
const scale = maxWidth / img.width;

canvas.width = maxWidth;
canvas.height = img.height * scale;

ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
</code></pre>
<h2 id="heading-common-mistakes-to-avoid">Common Mistakes to Avoid</h2>
<h3 id="heading-trying-to-upload-files-unnecessarily">Trying to Upload Files Unnecessarily</h3>
<p>If processing can happen in the browser, do it there. It’s faster and simpler.</p>
<h3 id="heading-forgetting-browser-compatibility">Forgetting Browser Compatibility</h3>
<p>Most modern browsers support Canvas and FileReader. But always test.</p>
<h3 id="heading-not-validating-file-input-properly">Not Validating File Input Properly</h3>
<p>Before processing the image, it’s important to validate the input file.</p>
<p>For example, you can check if a file is selected and ensure it is an image:</p>
<pre><code class="language-javascript">const file = fileInput.files[0];

if (!file) {
  alert("Please select a file.");
  return;
}

if (!file.type.startsWith("image/")) {
  alert("Please upload a valid image file.");
  return;
}
</code></pre>
<h2 id="heading-how-you-can-extend-this-project">How You Can Extend This Project</h2>
<p>Once this basic converter works, you can expand it with additional features. For example, you could add image resizing so users can adjust dimensions before downloading the converted file. Another useful improvement is implementing drag-and-drop uploads, which makes the interface more user-friendly.</p>
<p>You might also support multiple file uploads so users can convert several images at once. Adding compression controls would allow users to balance image quality and file size. Finally, you could include an image preview before download so users can confirm the result before saving the file.</p>
<p>All of these improvements rely on the same browser APIs used in this tutorial, so once you understand the core logic, extending the project becomes much easier.</p>
<h2 id="heading-why-browser-based-tools-are-becoming-more-popular">Why Browser-Based Tools Are Becoming More Popular</h2>
<p>Browsers today are far more capable than they used to be. Modern browser APIs allow developers to handle tasks that previously required server-side processing.</p>
<p>For example, browsers can now perform image processing, generate PDFs, convert files into different formats, and even handle some types of video processing directly on the client side.</p>
<p>Because of these capabilities, developers can build tools that run entirely inside the browser without relying on a backend server. This approach improves performance since users don’t have to upload files and wait for a server to process them.</p>
<p>It also improves privacy because files stay on the user’s device instead of being sent to a remote server. At the same time, it simplifies system architecture and makes applications easier to scale since there is no server infrastructure needed for file processing.</p>
<h2 id="heading-demo-how-the-image-converter-works">Demo: How the Image Converter Works</h2>
<p>After building the project, here is what the tool looks like in the browser.</p>
<h3 id="heading-upload-an-image">Upload an Image</h3>
<p>First, the user uploads an image using the file upload area.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/a11a29c5-32ff-4a08-a2bb-78a672ccde41.png" alt="Image upload interface showing drag and drop area" style="display:block;margin:0 auto" width="600" height="400" loading="lazy">

<h3 id="heading-select-the-output-format">Select the Output Format</h3>
<p>After uploading the image, the tool displays a preview along with details such as the <strong>image name, format, and file size</strong>. This helps users confirm that they uploaded the correct file before converting it.</p>
<p>Next, the user can choose the desired output format from the dropdown menu. The tool supports formats such as <strong>PNG, JPEG, WebP, GIF, and BMP</strong>, allowing the image to be converted into the format that best fits the user's needs.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/9be730b3-f9d0-4cb4-b110-5cb5bddddbb2.png" alt="Dropdown menu for selecting output image format" style="display:block;margin:0 auto" width="600" height="400" loading="lazy">

<h3 id="heading-convert-the-image">Convert the Image</h3>
<p>Once the format is selected, clicking the <strong>Convert All Images</strong> button processes the image directly in the browser.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/bd54a90b-ddf0-4875-aec6-a414d5f9c421.png" alt="convert button used to process for the uplaoded imnage" style="display:block;margin:0 auto" width="600" height="400" loading="lazy">

<h3 id="heading-download-the-converted-image">Download the Converted Image</h3>
<p>After conversion is complete, the tool generates a downloadable file.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/5c4ef749-9e0e-45f5-9a78-275866f10dfc.png" alt="converted image with download option" style="display:block;margin:0 auto" width="600" height="400" loading="lazy">

<h3 id="heading-conversion-results">Conversion Results</h3>
<p>The tool can also display useful information such as original size, converted size, and space saved after compression.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6979d22f93bc273cc33971b1/9c58dfc2-5b0b-4f18-a782-aea4e0fc868c.png" alt="image conversion result showing file size reduction " style="display:block;margin:0 auto" width="600" height="400" loading="lazy">

<p>Because everything happens in the browser using JavaScript and the Canvas API, the image never leaves the user's device.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In this tutorial, you built a browser-based image converter using JavaScript.</p>
<p>In this tutorial, you learned how to read local image files using JavaScript, process images using the Canvas API, convert them into different formats, and allow users to download the result directly from the browser.</p>
<p>This pattern is useful far beyond image conversion.</p>
<p>You can use the same approach for many browser-based tools.</p>
<p>Understanding how to use browser APIs like this opens up a lot of possibilities for building fast, efficient web applications.</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
