webdevpuneet.com

jsPDF Certificate Generator with Custom Script Font — Free Snippet

jsPDF Certificate Generator with Custom Script Font · Tools · Plain HTML, CSS & JS · Live preview

Copy & paste

Source Code

<div class="jcg">
  <form class="jcg-form" id="jcgForm" autocomplete="off">
    <h2>Certificate of completion</h2>
    <label>Recipient name <input name="name" value="Amara Okafor" maxlength="40"></label>
    <label>Course <input name="course" value="Advanced Accessible Interfaces" maxlength="60"></label>
    <div class="jcg-row">
      <label>Date <input name="date" type="date" value="2026-09-25"></label>
      <label>Hours <input name="hours" type="number" min="1" max="500" value="24"></label>
    </div>
    <label>Signed by <input name="signer" value="Dr. Lena Park, Programme Lead" maxlength="50"></label>
    <fieldset class="jcg-theme">
      <legend>Theme</legend>
      <label><input type="radio" name="theme" value="navy" checked> Navy &amp; gold</label>
      <label><input type="radio" name="theme" value="forest"> Forest</label>
      <label><input type="radio" name="theme" value="plum"> Plum</label>
    </fieldset>
    <button type="button" id="jcgDownload">Download PDF</button>
    <p class="jcg-status" id="jcgStatus" role="status">Loading script font…</p>
  </form>
  <div class="jcg-preview"><canvas id="jcgCanvas" aria-label="Rendered certificate PDF"></canvas></div>
</div>

Extend with AI

Take This Snippet Further With AI

The AI prompt for this one is for Patreon supporters ($3 a month, 7 days free), along with a site with no ads. Supporters enter the passcode once and it is remembered in this browser.

· Become a supporter

What's included

Features

Custom TTF embedding
addFileToVFS plus addFont with a Google Font.
Graceful font fallback
Times Italic if the font can't load.
Chunked base64 conversion
Avoids call-stack limits on large fonts.
Auto-fitting names
getTextWidth shrinks long names.
Wrapped course titles
splitTextToSize with centre alignment.
Print-accurate layout
Millimetre coordinates on A4 landscape.
Colour themes
Ink and accent RGB sets.
Live PDF preview
The real file rendered with PDF.js.

About this UI Snippet

jsPDF Certificates — Custom Fonts, Fitted Text and Print-Ready Layout

Screenshot of the jsPDF Certificate Generator with Custom Script Font snippet rendered live

Course platforms, workshops and HR tools all eventually need to hand out certificates. Generating them in the browser keeps personal data on the user's device and needs no rendering server. This snippet builds a landscape A4 certificate with jsPDF and previews the actual PDF with PDF.js as you type.

Embedding a custom font

jsPDF ships with 14 standard fonts, none of them script faces. To use another font it must be TrueType (.ttf, not WOFF), converted to base64, added to jsPDF's virtual file system with addFileToVFS, and registered with addFont(file, name, style). After that setFont('GreatVibes', 'normal') works like any built-in font. The TTF is fetched once and reused for every re-render. If the fetch fails, the certificate falls back to Times Italic rather than breaking.

Base64 without blowing the stack

String.fromCharCode.apply with a 450 KB array exceeds the maximum argument count, so the conversion processes the bytes in 32 KB chunks before calling btoa.

Text that always fits

Names vary wildly in length. The generator measures the name with getTextWidth and steps the font size down until it fits the available width, with a minimum size. Long course titles wrap with splitTextToSize and centre with { align: 'center' }.

Layout in millimetres

Every element is positioned in millimetres on a 297 × 210 mm page, so the certificate prints at exactly the size you design. Borders are two rectangles with different line widths, and the seal is two concentric circles with small caps text. setCharSpace spaces out the heading letters — and because align: 'center' ignores that spacing, the heading is centred by hand from its measured width plus the added space.

Themes

Three colour themes change the ink and accent colours, applied through setDrawColor, setFillColor and setTextColor with RGB arrays.

Downloads in sandboxes

Sandboxed iframes block downloads, so the status line tells users to open the demo in its own tab if nothing is saved.

Step by step

How to Use

  1. 1
    Load jsPDF and PDF.jsBoth from the CDN; the script font is fetched at startup.
  2. 2
    Fill in the detailsName, course, date, hours and signer; the preview follows.
  3. 3
    Try a long nameThe font size shrinks to keep it inside the border.
  4. 4
    Pick a themeNavy and gold, forest or plum.
  5. 5
    DownloadSaves a print-ready landscape A4 PDF.

Real-world uses

Common Use Cases

Course completion certificates
Generate a certificate per learner the moment they finish, with the name set in an embedded script font. Long names shrink automatically using getTextWidth, so nothing overflows the line.
Workshop and event attendance
Hand out printable A4 certificates on the spot at a workshop or webinar. The live PDF.js preview shows the real PDF as you type, so typos are caught before anything is printed.
HR and compliance training records
Issue completion records for safety or compliance courses while keeping personal data on the user's device, since the PDF is built entirely in the browser with no rendering server.
Awards for schools and clubs
Create achievement and sports-day awards in three colour themes with a border and seal. If the Google font cannot load, the generator falls back to Times Italic instead of failing.
Learning custom fonts in jsPDF
See how addFileToVFS and addFont embed a TTF, and why large fonts are converted to base64 in chunks. It is a reusable recipe for any branded PDF.
Related: jsPDF Invoice Generator
Tables and totals with autoTable: jsPDF Invoice Generator with Live PDF Preview.
Related: Certificate Preview Card
An HTML certificate card: Certificate of Completion Preview.

Got questions?

Frequently Asked Questions

Load a TrueType (.ttf) file, convert it to base64, call doc.addFileToVFS('Font.ttf', base64) and doc.addFont('Font.ttf', 'FontName', 'normal'), then doc.setFont('FontName', 'normal').

No. jsPDF needs TrueType fonts. Download the .ttf version, which Google Fonts provides in its GitHub repository and download archives.

Measure it with doc.getTextWidth(text) at the current font size and reduce the size until it is narrower than the space available, with a sensible minimum.

Pass the horizontal centre as the x coordinate and { align: 'center' } as options: doc.text(text, pageWidth / 2, y, { align: 'center' }).

String.fromCharCode.apply on a very large array can exceed the engine's maximum number of function arguments. Processing the bytes in chunks keeps each call within the limit.