VibeFox JS PDF — Demo
Quick start
Plain HTML
The document of the quick start, made in the browser and shown at once in the viewer with page thumbnails; the line added after the viewer was created appears by itself. Save PDF downloads the file.
function addButtons(buttons) {
const bar = document.createElement('div');
for (const [text, onClick] of buttons) {
const button = document.createElement('button');
button.textContent = text;
button.onclick = onClick;
bar.append(button);
}
document.getElementById('app').before(bar);
return bar;
}
addButtons([['Save PDF', () => doc.save('report.pdf')]]);
document.getElementById('app').style.height = '520px';
const doc = new VibeFoxJSPdf.PdfDocument({
format: 'A4',
metadata: { title: 'Sales report' },
footer: '{page} / {pages}',
});
doc.text('Sales report', { size: 22, bold: true });
doc.text('The table lists the orders of the month.', { marginBottom: 12 });
doc.table({
columns: [
{ header: 'Item', field: 'item' },
{ header: 'Price', field: 'price', width: 80, align: 'right', format: (v) => v.toFixed(2) },
],
rows: [{ item: 'Widget', price: 990 }, { item: 'Gadget', price: 1490 }],
zebra: '#f4f6f8',
});
// the viewer shows the document before it is saved and follows every change
const viewer = VibeFoxJSPdf.createPdfViewer('#app', { document: doc, sidebar: 'thumbnails', zoom: 0.75 });
doc.text('Added later: the preview updates by itself.');Open a PDF file
Choose any PDF file of your computer: it is opened and drawn by the library itself, nothing is uploaded. Until then the viewer shows a file made in the code.
document.getElementById('app').style.height = '500px';
const input = document.createElement('input');
input.type = 'file';
input.accept = 'application/pdf';
document.getElementById('app').before(input);
// a first file to show: a document of the library saved to PDF bytes and opened again
const sample = new VibeFoxJSPdf.PdfDocument();
sample.text('Choose a PDF file above', { size: 20, bold: true });
sample.text('It is opened in this page only; nothing is sent anywhere.');
const viewer = VibeFoxJSPdf.createPdfViewer('#app', { document: await VibeFoxJSPdf.loadPdf(await sample.toBytes()), zoom: 0.75 });
input.onchange = async () => {
const pdf = await VibeFoxJSPdf.loadPdf(input.files[0]); // bytes, a Blob / File or a URL
viewer.setDocument(pdf); // drawn by the library itself
};Core concepts
Documents and pages
Flowing text from the top margin and a box placed at exact coordinates of the page (points, from the top-left corner). The line under the viewer shows the page count and the flow cursor.
document.getElementById('app').style.height = '440px';
const output = document.createElement('pre');
output.style.cssText = 'height: 40px; overflow: auto; margin: 6px 0 0; padding: 4px 8px; border: 1px solid #d0d7de; font: 12px ui-monospace, Consolas, monospace; white-space: pre-wrap';
document.getElementById('app').after(output);
const show = (...lines) => { output.textContent = lines.join('\n'); };
const report = new VibeFoxJSPdf.PdfDocument({ format: 'A4', orientation: 'portrait', margins: { top: 56, right: 50, bottom: 56, left: 50 } });
report.text('Flowing text starts at the top margin.');
const page = report.currentPage; // the page the flow cursor is on
page.drawRect(50, 120, 200, 60, { fill: '#e0f2fe', stroke: '#0369a1', radius: 6 });
page.text('Placed at x 60, y 140', { x: 60, y: 140, size: 10 });
show('pageCount: ' + report.pageCount + ', cursor: ' + JSON.stringify(report.cursor));
VibeFoxJSPdf.createPdfViewer('#app', { document: report, zoom: 0.75 });Events
Every event of the viewer and the document is written into the box. Turn pages, zoom, or press Add text: the document emits change and the viewer draws it again.
function addButtons(buttons) {
const bar = document.createElement('div');
for (const [text, onClick] of buttons) {
const button = document.createElement('button');
button.textContent = text;
button.onclick = onClick;
bar.append(button);
}
document.getElementById('app').before(bar);
return bar;
}
document.getElementById('app').style.height = '420px';
const output = document.createElement('pre');
output.style.cssText = 'height: 90px; overflow: auto; margin: 6px 0 0; padding: 4px 8px; border: 1px solid #d0d7de; font: 12px ui-monospace, Consolas, monospace; white-space: pre-wrap';
document.getElementById('app').after(output);
const show = (...lines) => { output.textContent = lines.join('\n'); };
const report = new VibeFoxJSPdf.PdfDocument({ metadata: { title: 'Annual report 2026' }, footer: { text: 'Page {page} of {pages}', align: 'right', size: 9 } });
const chapters = [
['Introduction', 'This report describes the year of a small company that makes garden furniture. Sales grew in every region, and the new online shop brought many first-time customers.'],
['Sales', 'Sales reached 4.2 million EUR, 12 % more than last year. Chairs and tables sold best; the invoice total of the largest order was 48 000 EUR.'],
['Customers', 'Most customers are families and small cafes. Write to [email protected] or call +47 22 33 44 55 for a catalog.'],
['Outlook', 'Next year the company plans a second workshop and a winter collection. The search below finds the word invoice on page 2.'],
];
chapters.forEach(([title, body], i) => {
if (i) report.addPage();
report.addBookmark(title, { page: i });
report.text((i + 1) + '. ' + title, { size: 22, bold: true, color: '#0f766e', marginBottom: 10 });
report.text(body + ' ' + body, { size: 12, align: 'justify', marginBottom: 14 });
const page = report.page(i);
[60, 95, 75, 120, 105].forEach((h, j) => page.drawRect(70 + j * 90, 420 - h, 60, h, { fill: ['#99f6e4', '#5eead4', '#2dd4bf', '#14b8a6', '#0d9488'][j] }));
page.drawLine(60, 420, 520, 420, { color: '#64748b', width: 1 });
});
const log = [];
const write = (line) => { log.unshift(line); show(...log.slice(0, 6)); };
const viewer = VibeFoxJSPdf.createPdfViewer('#app', { document: report, zoom: 0.6 });
viewer.on('pageChange', (e) => write('pageChange: page ' + e.page + ' of ' + e.pages));
viewer.on('zoomChange', (e) => write('zoomChange: ' + Math.round(e.zoom * 100) + ' %'));
report.on('change', (e) => write('document change: ' + e.type));
write('events are listed here, the newest first');
addButtons([
['Next page', () => viewer.nextPage()],
['Zoom in', () => viewer.zoomIn()],
['Add text', () => report.page(0).text('Added at ' + new Date().toLocaleTimeString(), { x: 60, y: 460, size: 14, color: '#b91c1c' })],
]);State snapshots
Change the page, zoom or rotation, press Save state, change them again, then Restore state: the viewer goes back to the saved view.
function addButtons(buttons) {
const bar = document.createElement('div');
for (const [text, onClick] of buttons) {
const button = document.createElement('button');
button.textContent = text;
button.onclick = onClick;
bar.append(button);
}
document.getElementById('app').before(bar);
return bar;
}
document.getElementById('app').style.height = '420px';
const output = document.createElement('pre');
output.style.cssText = 'height: 40px; overflow: auto; margin: 6px 0 0; padding: 4px 8px; border: 1px solid #d0d7de; font: 12px ui-monospace, Consolas, monospace; white-space: pre-wrap';
document.getElementById('app').after(output);
const show = (...lines) => { output.textContent = lines.join('\n'); };
const report = new VibeFoxJSPdf.PdfDocument({ metadata: { title: 'Annual report 2026' }, footer: { text: 'Page {page} of {pages}', align: 'right', size: 9 } });
const chapters = [
['Introduction', 'This report describes the year of a small company that makes garden furniture. Sales grew in every region, and the new online shop brought many first-time customers.'],
['Sales', 'Sales reached 4.2 million EUR, 12 % more than last year. Chairs and tables sold best; the invoice total of the largest order was 48 000 EUR.'],
['Customers', 'Most customers are families and small cafes. Write to [email protected] or call +47 22 33 44 55 for a catalog.'],
['Outlook', 'Next year the company plans a second workshop and a winter collection. The search below finds the word invoice on page 2.'],
];
chapters.forEach(([title, body], i) => {
if (i) report.addPage();
report.addBookmark(title, { page: i });
report.text((i + 1) + '. ' + title, { size: 22, bold: true, color: '#0f766e', marginBottom: 10 });
report.text(body + ' ' + body, { size: 12, align: 'justify', marginBottom: 14 });
const page = report.page(i);
[60, 95, 75, 120, 105].forEach((h, j) => page.drawRect(70 + j * 90, 420 - h, 60, h, { fill: ['#99f6e4', '#5eead4', '#2dd4bf', '#14b8a6', '#0d9488'][j] }));
page.drawLine(60, 420, 520, 420, { color: '#64748b', width: 1 });
});
const viewer = VibeFoxJSPdf.createPdfViewer('#app', { document: report, zoom: 0.6, sidebar: 'thumbnails' });
let saved = null;
show('no state saved yet');
addButtons([
['Save state', () => { saved = viewer.getState(); show('saved: ' + JSON.stringify(saved)); }],
['Change view', () => { viewer.goToPage(3); viewer.setZoom(0.9); viewer.rotate(90); viewer.hideSidebar(); }],
['Restore state', () => { if (saved) viewer.setState(saved); }],
]);Creating documents
Pages and the flow
An A5 book in Times: a justified paragraph that runs over the next pages by itself, then a wide A4 landscape page with the flow continuing at y 200.
document.getElementById('app').style.height = '480px';
const book = new VibeFoxJSPdf.PdfDocument({ format: 'A5', font: { family: 'times', size: 11 } });
book.text('Chapter 1', { size: 18, bold: true, marginBottom: 8 });
const paragraph = 'A long paragraph wraps inside the margins and continues on the next page when the page is full. ';
for (let i = 0; i < 6; i++) book.text(paragraph.repeat(8), { align: 'justify', paragraphGap: 6 });
book.moveDown(20); // empty space, pt
book.ensureSpace(120); // a new page unless 120 pt are left
book.addPage({ format: 'A4', orientation: 'landscape' });
book.setCursorY(200); // continue the flow at y 200 of the current page
book.text('A wide page.', { size: 24 });
VibeFoxJSPdf.createPdfViewer('#app', { document: book, sidebar: 'thumbnails', zoom: 0.7 });Text
Text in a box aligned to the right, a line under it at the measured bottom of the box, and a rotated half-transparent stamp text.
document.getElementById('app').style.height = '440px';
const output = document.createElement('pre');
output.style.cssText = 'height: 30px; overflow: auto; margin: 6px 0 0; padding: 4px 8px; border: 1px solid #d0d7de; font: 12px ui-monospace, Consolas, monospace; white-space: pre-wrap';
document.getElementById('app').after(output);
const show = (...lines) => { output.textContent = lines.join('\n'); };
const doc = new VibeFoxJSPdf.PdfDocument();
doc.text('Invoice 2026-017', { size: 20, bold: true });
doc.text('Bold, italic and colored text in the standard fonts.', { italic: true, color: '#475569' });
const p = doc.page(0);
const box = p.text('Total: 1 250.00 EUR', { x: 350, y: 200, width: 195, align: 'right', size: 14, bold: true });
p.drawLine(350, box.bottom + 4, 545, box.bottom + 4, { color: '#94a3b8', width: 0.5 });
p.text('PAID', { x: 420, y: 260, size: 28, color: '#16a34a', rotate: 15, opacity: 0.6 });
show('width of "Total": ' + p.measureText('Total', { size: 14, bold: true }).toFixed(1) + ' pt');
VibeFoxJSPdf.createPdfViewer('#app', { document: doc, zoom: 0.9 });Shapes and paths
A circle, a triangle, a Bézier curve, a rounded dashed box and a half-transparent group cut by a clip path — all vector graphics of the page.
document.getElementById('app').style.height = '380px';
const doc = new VibeFoxJSPdf.PdfDocument();
const pg = doc.addPage();
pg.drawCircle(80, 80, 20, { fill: '#fde68a', stroke: '#b45309', lineWidth: 1.5 });
pg.drawPolygon([[130, 100], [150, 60], [170, 100]], { fill: '#bfdbfe', close: true });
pg.drawPath([{ c: 'M', x: 200, y: 100 }, { c: 'C', x1: 220, y1: 50, x2: 260, y2: 110, x: 280, y: 70 }], { stroke: '#7c3aed', lineWidth: 2 });
pg.group({ opacity: 0.5, clip: [{ c: 'M', x: 300, y: 60 }, { c: 'L', x: 360, y: 60 }, { c: 'L', x: 360, y: 100 }, { c: 'L', x: 300, y: 100 }, { c: 'Z' }] }, (g) => {
g.drawRect(290, 50, 90, 60, { fill: '#ef4444' });
});
pg.drawRect(390, 55, 140, 50, { stroke: '#0f766e', lineWidth: 1.5, dash: [4, 3], radius: 10 });
pg.drawEllipse(300, 200, 220, 50, { fill: '#ecfeff', stroke: '#0891b2' });
pg.drawLine(80, 280, 520, 280, { color: '#64748b', width: 1, dash: [2, 2] });
VibeFoxJSPdf.createPdfViewer('#app', { document: doc, zoom: 0.9 });Images and SVG
A PNG with transparency (drawn into a canvas right in the example), placed twice from one registered image, and an SVG with a gradient drawn as vector graphics — zoom in to see that it stays sharp.
document.getElementById('app').style.height = '460px';
// a PNG with transparency, made in a canvas (in your page: the bytes or a data URL of your image)
const canvas = document.createElement('canvas');
canvas.width = canvas.height = 96;
const ctx = canvas.getContext('2d');
const grad = ctx.createRadialGradient(48, 40, 6, 48, 48, 46);
grad.addColorStop(0, '#fef08a');
grad.addColorStop(1, '#f97316');
ctx.fillStyle = grad;
ctx.beginPath();
ctx.arc(48, 48, 46, 0, Math.PI * 2);
ctx.fill();
const doc = new VibeFoxJSPdf.PdfDocument();
const logo = doc.addImage(canvas.toDataURL('image/png')); // registered once
doc.addPage().drawImage(logo, 50, 50, { width: 48 });
doc.moveDown(60);
doc.image(logo, { width: 120, align: 'center', marginBottom: 12 });
doc.svg('<svg xmlns="http://www.w3.org/2000/svg" width="300" height="120">' +
'<defs><linearGradient id="g"><stop offset="0" stop-color="#0ea5e9"/><stop offset="1" stop-color="#6366f1"/></linearGradient></defs>' +
'<rect x="10" y="10" width="280" height="100" rx="12" fill="url(#g)"/>' +
'<text x="150" y="70" text-anchor="middle" font-size="28" fill="#fff">Vector</text>' +
'</svg>', { width: 240, align: 'center' });
VibeFoxJSPdf.createPdfViewer('#app', { document: doc, zoom: 0.75 });Tables
A table of 80 rows: the rows go on over three pages and the colored header row is repeated on each. Scroll the pages or click a thumbnail.
document.getElementById('app').style.height = '480px';
const output = document.createElement('pre');
output.style.cssText = 'height: 30px; overflow: auto; margin: 6px 0 0; padding: 4px 8px; border: 1px solid #d0d7de; font: 12px ui-monospace, Consolas, monospace; white-space: pre-wrap';
document.getElementById('app').after(output);
const show = (...lines) => { output.textContent = lines.join('\n'); };
const doc = new VibeFoxJSPdf.PdfDocument({ footer: { text: '{page} / {pages}', align: 'center', size: 9 } });
doc.text('Order lines', { size: 18, bold: true, marginBottom: 8 });
const res = doc.table({
columns: [
{ header: 'Item', field: 'item' },
{ header: 'Qty', field: 'qty', width: 50, align: 'right' },
{ header: 'Amount', field: 'amount', width: 90, align: 'right', format: (v) => v.toFixed(2) },
],
rows: Array.from({ length: 80 }, (_, i) => ({ item: 'Item ' + (i + 1), qty: i % 5 + 1, amount: (i + 1) * 9.5 })),
zebra: '#f8fafc',
headerStyle: { fill: '#0f766e', color: '#ffffff' },
fontSize: 9,
});
show('pages: ' + res.pages + ', rows: ' + res.rows);
VibeFoxJSPdf.createPdfViewer('#app', { document: doc, sidebar: 'thumbnails', zoom: 0.7 });Header, footer and watermark
The title in the header, "Page n of m" in the footer and a DRAFT watermark across every page; they are drawn on all three pages.
document.getElementById('app').style.height = '460px';
const doc = new VibeFoxJSPdf.PdfDocument({ metadata: { title: 'Project plan' } });
for (let i = 1; i <= 3; i++) {
if (i > 1) doc.addPage();
doc.text('Section ' + i, { size: 18, bold: true, marginBottom: 8 });
doc.text('The header, the footer and the watermark are set once for the whole document. '.repeat(6));
}
doc.setHeader({ text: '{title}', align: 'left', size: 9, color: '#64748b' });
doc.setFooter({ text: 'Page {page} of {pages}', align: 'right', size: 9 });
doc.setWatermark('DRAFT', { opacity: 0.12, angle: 45, size: 90 });
VibeFoxJSPdf.createPdfViewer('#app', { document: doc, sidebar: 'thumbnails', zoom: 0.7 });Links, bookmarks and metadata
Bookmarks with a nested one in the sidebar (click one to open its page) and a link on page 1 that goes to page 3. The metadata title is used as the name of the document.
document.getElementById('app').style.height = '460px';
const doc = new VibeFoxJSPdf.PdfDocument();
['Contents', 'Terms', 'Payment'].forEach((title, i) => {
if (i) doc.addPage();
doc.text(title, { size: 22, bold: true, marginBottom: 10 });
});
const pageOne = doc.page(0);
pageOne.text('Go to the payment terms (page 3)', { x: 50, y: 100, color: '#2563eb' });
pageOne.drawLine(50, 115, 230, 115, { color: '#2563eb', width: 0.5 });
pageOne.link(50, 100, 200, 16, { page: 2, destY: 0 }); // to the third page
pageOne.link(50, 130, 200, 16, { url: 'https://example.com/terms' }); // external link
pageOne.text('example.com/terms (external link)', { x: 50, y: 130, color: '#2563eb' });
const chapter = doc.addBookmark('Terms', { page: 1 });
doc.addBookmark('Payment', { page: 2, parent: chapter }); // nested bookmark
doc.setMetadata({ title: 'Invoice 2026-001', author: 'Example Ltd', subject: 'Invoice', keywords: 'invoice, 2026' });
VibeFoxJSPdf.createPdfViewer('#app', { document: doc, sidebar: 'outline', zoom: 0.7 });Saving
The buttons save the same document in different ways: a download, a file without compression, and the size of the bytes. The viewer shows what is saved.
function addButtons(buttons) {
const bar = document.createElement('div');
for (const [text, onClick] of buttons) {
const button = document.createElement('button');
button.textContent = text;
button.onclick = onClick;
bar.append(button);
}
document.getElementById('app').before(bar);
return bar;
}
document.getElementById('app').style.height = '400px';
const output = document.createElement('pre');
output.style.cssText = 'height: 40px; overflow: auto; margin: 6px 0 0; padding: 4px 8px; border: 1px solid #d0d7de; font: 12px ui-monospace, Consolas, monospace; white-space: pre-wrap';
document.getElementById('app').after(output);
const show = (...lines) => { output.textContent = lines.join('\n'); };
const doc = new VibeFoxJSPdf.PdfDocument({ metadata: { title: 'Saved in the browser' } });
doc.text('Saved in the browser', { size: 20, bold: true });
doc.text('The file is written by the library: PDF 1.7, compressed when the browser has CompressionStream.');
doc.on('save', (e) => show('saved ' + e.filename + ', ' + e.size + ' bytes'));
show('press a button');
addButtons([
['Download', () => doc.save('saved.pdf')],
['Download uncompressed', () => doc.save('plain.pdf', { compress: false })],
['Size of the bytes', async () => show('toBytes(): ' + (await doc.toBytes()).length + ' bytes, without compression: ' + (await doc.toBytes({ compress: false })).length + ' bytes')],
]);
VibeFoxJSPdf.createPdfViewer('#app', { document: doc, zoom: 0.75 });JSON snapshots and merging
The document is saved as JSON, restored from it, and both are merged into one document of four pages (the thumbnails show them).
document.getElementById('app').style.height = '460px';
const output = document.createElement('pre');
output.style.cssText = 'height: 30px; overflow: auto; margin: 6px 0 0; padding: 4px 8px; border: 1px solid #d0d7de; font: 12px ui-monospace, Consolas, monospace; white-space: pre-wrap';
document.getElementById('app').after(output);
const show = (...lines) => { output.textContent = lines.join('\n'); };
const doc = new VibeFoxJSPdf.PdfDocument();
doc.text('Original, page 1', { size: 20, bold: true });
doc.addPage();
doc.text('Original, page 2', { size: 20, bold: true });
const json = JSON.stringify(doc.toJSON());
const copy = VibeFoxJSPdf.PdfDocument.fromJSON(JSON.parse(json));
copy.page(0).text('Restored from JSON', { x: 50, y: 100, size: 16, color: '#b45309' });
const both = VibeFoxJSPdf.PdfDocument.merge(doc, copy);
show('JSON: ' + json.length + ' characters, merged pages: ' + both.pageCount);
VibeFoxJSPdf.createPdfViewer('#app', { document: both, sidebar: 'thumbnails', zoom: 0.6 });Fonts
Standard fonts
The standard PDF fonts every reader has: Helvetica, Times and Courier with bold and italic, Symbol and ZapfDingbats; Latin and Cyrillic text in one line. No font file is loaded.
document.getElementById('app').style.height = '480px';
const doc = new VibeFoxJSPdf.PdfDocument();
for (const family of ['helvetica', 'times', 'courier']) {
doc.text(family, { font: family, size: 16, bold: true, marginBottom: 2 });
doc.text('Regular: The quick brown fox · Привет, мир', { font: family });
doc.text('Bold: The quick brown fox', { font: family, bold: true });
doc.text('Italic: The quick brown fox', { font: family, italic: true });
doc.text('Bold italic: Grüße aus Köln', { font: family, bold: true, italic: true, marginBottom: 10 });
}
doc.text('abgdpWS αβγ ∑ √ ∞ ≠', { font: 'symbol', size: 18 }); // Greek letters and math signs
doc.text('3456789 ✔ ☎ ✈ ❶ ①', { font: 'zapfdingbats', size: 18 }); // dingbats
VibeFoxJSPdf.createPdfViewer('#app', { document: doc, zoom: 0.8 });HTML to PDF
An HTML string with its CSS laid out by the library: a styled heading, a link, a table with borders, a list and the page footer. Then a second part is added on a new page.
document.getElementById('app').style.height = '500px';
const html = '<style>' +
'h1 { color: #0f766e; } table { border-collapse: collapse; width: 100%; }' +
'td, th { border: 1px solid #cbd5e1; padding: 4px 8px; } th { background: #f1f5f9; }' +
'.note { border-left: 4px solid #f59e0b; background: #fffbeb; padding: 6px 10px; }' +
'</style>' +
'<h1>Quarterly report</h1>' +
'<p>Revenue grew by <b>12 %</b>. Details: <a href="https://example.com/q3">example.com/q3</a>.</p>' +
'<table><thead><tr><th>Region</th><th>Sales</th></tr></thead>' +
'<tbody><tr><td>North</td><td>120</td></tr><tr><td>South</td><td>98</td></tr></tbody></table>' +
'<p class="note">Rounded numbers, in thousands of EUR.</p>' +
'<ol><li>Open a second shop</li><li>Hire two people</li></ol>';
const report = await VibeFoxJSPdf.htmlToPdf(html, {
format: 'A4',
footer: '<p style="text-align: right">{page} / {pages}</p>',
});
await report.addHtml('<h2>Notes</h2><ul><li>First</li><li>Second</li></ul>', { newPage: true });
VibeFoxJSPdf.createPdfViewer('#app', { document: report, sidebar: 'thumbnails', zoom: 0.7 });VibeFox JS components in a PDF
A chart of VibeFox JS Charts (above the viewer) drawn into the PDF as vector graphics, once full width and twice in a grid of two columns. Zoom in: the bars and texts stay sharp.
document.getElementById('app').style.height = '440px';
// VibeFox JS Charts is also on the page (its file loaded)
const box = document.createElement('div');
box.style.cssText = 'width: 480px; max-width: 100%; height: 220px';
document.getElementById('app').before(box);
const chart = VibeFoxJSCharts.createChart(box, { categories: ['A', 'B', 'C', 'D'], series: [{ type: 'column', name: 'Sales', data: [5, 8, 3, 6] }] });
const report = new VibeFoxJSPdf.PdfDocument();
report.text('Sales by product', { size: 16, bold: true, marginBottom: 8 });
await report.addComponent(chart); // content width, the on-screen aspect ratio
await report.addComponents([chart, chart], { columns: 2, gap: 12, height: 160 }); // a layout grid
VibeFoxJSPdf.createPdfViewer('#app', { document: report, zoom: 0.7 });Opening and changing PDF files
A PDF file (here made in the code and saved to bytes) is opened, gets an APPROVED text and a COPY watermark, and is saved as an incremental update. The box shows what the reader found in the file.
function addButtons(buttons) {
const bar = document.createElement('div');
for (const [text, onClick] of buttons) {
const button = document.createElement('button');
button.textContent = text;
button.onclick = onClick;
bar.append(button);
}
document.getElementById('app').before(bar);
return bar;
}
document.getElementById('app').style.height = '420px';
const output = document.createElement('pre');
output.style.cssText = 'height: 50px; overflow: auto; margin: 6px 0 0; padding: 4px 8px; border: 1px solid #d0d7de; font: 12px ui-monospace, Consolas, monospace; white-space: pre-wrap';
document.getElementById('app').after(output);
const show = (...lines) => { output.textContent = lines.join('\n'); };
// the bytes of a PDF file: from <input type="file">, fetch() or, as here, made by the library
const source = new VibeFoxJSPdf.PdfDocument({ metadata: { title: 'Purchase order' } });
source.text('Purchase order 7781', { size: 20, bold: true });
source.text('Ten chairs and two tables, delivery in May.');
const fileBytes = await source.toBytes();
const pdf = await VibeFoxJSPdf.loadPdf(fileBytes);
pdf.page(0).text('APPROVED', { x: 400, y: 40, size: 18, bold: true, color: '#16a34a' });
pdf.setWatermark('COPY', { opacity: 0.1 });
pdf.setMetadata({ subject: 'Approved copy' });
const updated = await pdf.toBytes(); // incremental update: the original bytes stay unchanged
show('version ' + pdf.sourceInfo.version + ', pages ' + pdf.pageCount + ', title "' + pdf.metadata.title + '"',
'original ' + fileBytes.length + ' bytes, with the update ' + updated.length + ' bytes');
addButtons([['Download', () => pdf.save('approved.pdf')]]);
VibeFoxJSPdf.createPdfViewer('#app', { document: pdf, zoom: 0.75 });Page operations
Two pages of a document drawn side by side on one landscape sheet, followed by the pages of the source after it was rotated, reordered and shortened.
document.getElementById('app').style.height = '460px';
const doc = new VibeFoxJSPdf.PdfDocument();
['#dbeafe', '#dcfce7', '#fef9c3', '#fee2e2'].forEach((color, i) => {
const page = doc.addPage();
page.drawRect(40, 40, 515, 760, { fill: color, radius: 12 });
page.text('Page ' + (i + 1), { x: 60, y: 380, width: 475, align: 'center', size: 48, bold: true });
});
// two pages on one landscape sheet
const sheet = new VibeFoxJSPdf.PdfDocument({ margins: { top: 0, right: 0, bottom: 0, left: 0 } });
const s = sheet.addPage({ orientation: 'landscape' });
s.drawPage(doc, 0, 20, 20, { width: 390, height: 555 });
s.drawPage(doc, 1, 432, 20, { width: 390, height: 555 });
const pdf = await VibeFoxJSPdf.loadPdf(await doc.toBytes());
pdf.rotatePage(0, 90);
pdf.movePage(2, 0);
pdf.removePage(3);
const all = VibeFoxJSPdf.PdfDocument.merge(sheet, pdf);
VibeFoxJSPdf.createPdfViewer('#app', { document: all, sidebar: 'thumbnails', zoom: 0.5 });Viewer
The viewer with its toolbar: thumbnails, page field, previous / next, zoom, fit width, rotation, search, print and download. It shows the classic style, the second page and a rotated view.
document.getElementById('app').style.height = '520px';
const report = new VibeFoxJSPdf.PdfDocument({ metadata: { title: 'Annual report 2026' }, footer: { text: 'Page {page} of {pages}', align: 'right', size: 9 } });
const chapters = [
['Introduction', 'This report describes the year of a small company that makes garden furniture. Sales grew in every region, and the new online shop brought many first-time customers.'],
['Sales', 'Sales reached 4.2 million EUR, 12 % more than last year. Chairs and tables sold best; the invoice total of the largest order was 48 000 EUR.'],
['Customers', 'Most customers are families and small cafes. Write to [email protected] or call +47 22 33 44 55 for a catalog.'],
['Outlook', 'Next year the company plans a second workshop and a winter collection. The search below finds the word invoice on page 2.'],
];
chapters.forEach(([title, body], i) => {
if (i) report.addPage();
report.addBookmark(title, { page: i });
report.text((i + 1) + '. ' + title, { size: 22, bold: true, color: '#0f766e', marginBottom: 10 });
report.text(body + ' ' + body, { size: 12, align: 'justify', marginBottom: 14 });
const page = report.page(i);
[60, 95, 75, 120, 105].forEach((h, j) => page.drawRect(70 + j * 90, 420 - h, 60, h, { fill: ['#99f6e4', '#5eead4', '#2dd4bf', '#14b8a6', '#0d9488'][j] }));
page.drawLine(60, 420, 520, 420, { color: '#64748b', width: 1 });
});
const v = VibeFoxJSPdf.createPdfViewer('#app', {
document: report,
page: 2, // the page to open (1-based)
zoom: 0.75, // 1 = 100 %; minZoom 0.25, maxZoom 4
layout: 'continuous', // every page in one scroll; 'single' = one page at a time
sidebar: 'thumbnails', // 'thumbnails' | 'outline' | 'layers' | 'signatures' | null
filename: 'report.pdf', // name of the downloaded file
style: 'classic', // ready-made look (see Theming and styling)
});Navigation and zoom
The buttons call the methods of the viewer: pages, zoom, fit, rotation and the single-page layout. The box shows the state after each step.
function addButtons(buttons) {
const bar = document.createElement('div');
for (const [text, onClick] of buttons) {
const button = document.createElement('button');
button.textContent = text;
button.onclick = onClick;
bar.append(button);
}
document.getElementById('app').before(bar);
return bar;
}
document.getElementById('app').style.height = '440px';
const output = document.createElement('pre');
output.style.cssText = 'height: 30px; overflow: auto; margin: 6px 0 0; padding: 4px 8px; border: 1px solid #d0d7de; font: 12px ui-monospace, Consolas, monospace; white-space: pre-wrap';
document.getElementById('app').after(output);
const show = (...lines) => { output.textContent = lines.join('\n'); };
const report = new VibeFoxJSPdf.PdfDocument({ metadata: { title: 'Annual report 2026' }, footer: { text: 'Page {page} of {pages}', align: 'right', size: 9 } });
const chapters = [
['Introduction', 'This report describes the year of a small company that makes garden furniture. Sales grew in every region, and the new online shop brought many first-time customers.'],
['Sales', 'Sales reached 4.2 million EUR, 12 % more than last year. Chairs and tables sold best; the invoice total of the largest order was 48 000 EUR.'],
['Customers', 'Most customers are families and small cafes. Write to [email protected] or call +47 22 33 44 55 for a catalog.'],
['Outlook', 'Next year the company plans a second workshop and a winter collection. The search below finds the word invoice on page 2.'],
];
chapters.forEach(([title, body], i) => {
if (i) report.addPage();
report.addBookmark(title, { page: i });
report.text((i + 1) + '. ' + title, { size: 22, bold: true, color: '#0f766e', marginBottom: 10 });
report.text(body + ' ' + body, { size: 12, align: 'justify', marginBottom: 14 });
const page = report.page(i);
[60, 95, 75, 120, 105].forEach((h, j) => page.drawRect(70 + j * 90, 420 - h, 60, h, { fill: ['#99f6e4', '#5eead4', '#2dd4bf', '#14b8a6', '#0d9488'][j] }));
page.drawLine(60, 420, 520, 420, { color: '#64748b', width: 1 });
});
const viewer = VibeFoxJSPdf.createPdfViewer('#app', { document: report, zoom: 0.6 });
const state = () => show(JSON.stringify(viewer.getState()));
viewer.on('pageChange', state);
viewer.on('zoomChange', state);
viewer.on('rotationChange', state);
addButtons([
['Previous', () => viewer.prevPage()],
['Next', () => viewer.nextPage()],
['Page 4', () => viewer.goToPage(4)],
['Zoom out', () => viewer.zoomOut()],
['Zoom in', () => viewer.zoomIn()],
['Fit width', () => viewer.fitWidth()],
['Fit page', () => viewer.fitPage()],
['Rotate', () => viewer.rotate(90)],
['Single page', () => viewer.setOptions({ layout: 'single' })],
['Continuous', () => viewer.setOptions({ layout: 'continuous' })],
]);Sidebar: thumbnails, bookmarks, layers, signatures
The sidebar with the bookmarks of the report as a tree; the buttons switch it to thumbnails or close it. Click a bookmark or a thumbnail to open its page.
function addButtons(buttons) {
const bar = document.createElement('div');
for (const [text, onClick] of buttons) {
const button = document.createElement('button');
button.textContent = text;
button.onclick = onClick;
bar.append(button);
}
document.getElementById('app').before(bar);
return bar;
}
document.getElementById('app').style.height = '440px';
const report = new VibeFoxJSPdf.PdfDocument({ metadata: { title: 'Annual report 2026' }, footer: { text: 'Page {page} of {pages}', align: 'right', size: 9 } });
const chapters = [
['Introduction', 'This report describes the year of a small company that makes garden furniture. Sales grew in every region, and the new online shop brought many first-time customers.'],
['Sales', 'Sales reached 4.2 million EUR, 12 % more than last year. Chairs and tables sold best; the invoice total of the largest order was 48 000 EUR.'],
['Customers', 'Most customers are families and small cafes. Write to [email protected] or call +47 22 33 44 55 for a catalog.'],
['Outlook', 'Next year the company plans a second workshop and a winter collection. The search below finds the word invoice on page 2.'],
];
chapters.forEach(([title, body], i) => {
if (i) report.addPage();
report.addBookmark(title, { page: i });
report.text((i + 1) + '. ' + title, { size: 22, bold: true, color: '#0f766e', marginBottom: 10 });
report.text(body + ' ' + body, { size: 12, align: 'justify', marginBottom: 14 });
const page = report.page(i);
[60, 95, 75, 120, 105].forEach((h, j) => page.drawRect(70 + j * 90, 420 - h, 60, h, { fill: ['#99f6e4', '#5eead4', '#2dd4bf', '#14b8a6', '#0d9488'][j] }));
page.drawLine(60, 420, 520, 420, { color: '#64748b', width: 1 });
});
const viewer = VibeFoxJSPdf.createPdfViewer('#app', { document: report, sidebar: 'outline', zoom: 0.6 });
addButtons([
['Thumbnails', () => viewer.showSidebar('thumbnails')],
['Bookmarks', () => viewer.showSidebar('outline')],
['Hide', () => viewer.hideSidebar()],
]);A floor plan with three layers (optional content, as made by CAD or DTP programs). Turn the layers off and on in the Layers tab or with the button.
function addButtons(buttons) {
const bar = document.createElement('div');
for (const [text, onClick] of buttons) {
const button = document.createElement('button');
button.textContent = text;
button.onclick = onClick;
bar.append(button);
}
document.getElementById('app').before(bar);
return bar;
}
document.getElementById('app').style.height = '460px';
// a small PDF file with three layers, written here by hand (usually it comes from a CAD or DTP program)
const objects = [
'<< /Type /Catalog /Pages 2 0 R /OCProperties << /OCGs [5 0 R 6 0 R 7 0 R] /D << /Order [5 0 R 6 0 R 7 0 R] /ON [5 0 R 6 0 R 7 0 R] >> >> >>',
'<< /Type /Pages /Kids [3 0 R] /Count 1 >>',
'<< /Type /Page /Parent 2 0 R /MediaBox [0 0 595 842] /Resources << /Font << /F1 8 0 R >> /Properties << /L1 5 0 R /L2 6 0 R /L3 7 0 R >> >> /Contents 4 0 R >>',
null,
'<< /Type /OCG /Name (Walls) >>',
'<< /Type /OCG /Name (Wiring) >>',
'<< /Type /OCG /Name (Furniture) >>',
'<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>',
];
const content = 'BT /F1 22 Tf 60 770 Td (Floor plan) Tj ET\n' +
'/OC /L1 BDC 0.2 0.2 0.2 RG 8 w 60 280 475 440 re S 300 280 m 300 560 l S EMC\n' +
'/OC /L2 BDC 0.92 0.35 0.05 RG 2 w [6 4] 0 d 90 690 m 270 690 l 270 320 l S 330 690 m 500 690 l 500 420 l S EMC\n' +
'/OC /L3 BDC 0.05 0.46 0.43 rg 110 330 120 70 re f 360 330 90 90 re f 400 600 m 400 627.6 377.6 650 350 650 c 322.4 650 300 627.6 300 600 c f EMC\n';
objects[3] = '<< /Length ' + content.length + ' >>\nstream\n' + content + 'endstream';
let text = '%PDF-1.7\n';
const offsets = objects.map((o, i) => { const at = text.length; text += (i + 1) + ' 0 obj\n' + o + '\nendobj\n'; return at; });
const xref = text.length;
text += 'xref\n0 ' + (objects.length + 1) + '\n0000000000 65535 f \n' + offsets.map((o) => String(o).padStart(10, '0') + ' 00000 n \n').join('') +
'trailer\n<< /Size ' + (objects.length + 1) + ' /Root 1 0 R >>\nstartxref\n' + xref + '\n%%EOF\n';
const plan = await VibeFoxJSPdf.loadPdf(new TextEncoder().encode(text));
const viewer = VibeFoxJSPdf.createPdfViewer('#app', { document: plan, sidebar: 'layers', zoom: 0.6 });
let on = true;
addButtons([['Toggle wiring', () => { on = !on; viewer.setLayerVisible('Wiring', on); }]]);Text selection and search
The word "invoice" is found and highlighted (the current match in a stronger color); Next moves to the next match. Select text with the mouse and copy it, or press Ctrl+F in the viewer.
function addButtons(buttons) {
const bar = document.createElement('div');
for (const [text, onClick] of buttons) {
const button = document.createElement('button');
button.textContent = text;
button.onclick = onClick;
bar.append(button);
}
document.getElementById('app').before(bar);
return bar;
}
document.getElementById('app').style.height = '440px';
const output = document.createElement('pre');
output.style.cssText = 'height: 30px; overflow: auto; margin: 6px 0 0; padding: 4px 8px; border: 1px solid #d0d7de; font: 12px ui-monospace, Consolas, monospace; white-space: pre-wrap';
document.getElementById('app').after(output);
const show = (...lines) => { output.textContent = lines.join('\n'); };
const report = new VibeFoxJSPdf.PdfDocument({ metadata: { title: 'Annual report 2026' }, footer: { text: 'Page {page} of {pages}', align: 'right', size: 9 } });
const chapters = [
['Introduction', 'This report describes the year of a small company that makes garden furniture. Sales grew in every region, and the new online shop brought many first-time customers.'],
['Sales', 'Sales reached 4.2 million EUR, 12 % more than last year. Chairs and tables sold best; the invoice total of the largest order was 48 000 EUR.'],
['Customers', 'Most customers are families and small cafes. Write to [email protected] or call +47 22 33 44 55 for a catalog.'],
['Outlook', 'Next year the company plans a second workshop and a winter collection. The search below finds the word invoice on page 2.'],
];
chapters.forEach(([title, body], i) => {
if (i) report.addPage();
report.addBookmark(title, { page: i });
report.text((i + 1) + '. ' + title, { size: 22, bold: true, color: '#0f766e', marginBottom: 10 });
report.text(body + ' ' + body, { size: 12, align: 'justify', marginBottom: 14 });
const page = report.page(i);
[60, 95, 75, 120, 105].forEach((h, j) => page.drawRect(70 + j * 90, 420 - h, 60, h, { fill: ['#99f6e4', '#5eead4', '#2dd4bf', '#14b8a6', '#0d9488'][j] }));
page.drawLine(60, 420, 520, 420, { color: '#64748b', width: 1 });
});
const viewer = VibeFoxJSPdf.createPdfViewer('#app', { document: report, zoom: 0.75 });
const report1 = (s) => s && show(s.total + ' matches, current ' + (s.index + 1) + ' on page ' + s.page);
report1(await viewer.search('invoice', { caseSensitive: false }));
addButtons([
['Next', () => report1(viewer.searchNext())],
['Previous', () => report1(viewer.searchPrev())],
['Search "sales"', async () => report1(await viewer.search('sales'))],
['Clear', () => { viewer.clearSearch(); show(''); }],
]);Page editing
The thumbnail panel with the page tool bar: select thumbnails (Ctrl / Shift + click), drag them to a new place, rotate or delete them, undo. The buttons do the same from code.
function addButtons(buttons) {
const bar = document.createElement('div');
for (const [text, onClick] of buttons) {
const button = document.createElement('button');
button.textContent = text;
button.onclick = onClick;
bar.append(button);
}
document.getElementById('app').before(bar);
return bar;
}
document.getElementById('app').style.height = '460px';
const output = document.createElement('pre');
output.style.cssText = 'height: 30px; overflow: auto; margin: 6px 0 0; padding: 4px 8px; border: 1px solid #d0d7de; font: 12px ui-monospace, Consolas, monospace; white-space: pre-wrap';
document.getElementById('app').after(output);
const show = (...lines) => { output.textContent = lines.join('\n'); };
const report = new VibeFoxJSPdf.PdfDocument({ metadata: { title: 'Annual report 2026' }, footer: { text: 'Page {page} of {pages}', align: 'right', size: 9 } });
const chapters = [
['Introduction', 'This report describes the year of a small company that makes garden furniture. Sales grew in every region, and the new online shop brought many first-time customers.'],
['Sales', 'Sales reached 4.2 million EUR, 12 % more than last year. Chairs and tables sold best; the invoice total of the largest order was 48 000 EUR.'],
['Customers', 'Most customers are families and small cafes. Write to [email protected] or call +47 22 33 44 55 for a catalog.'],
['Outlook', 'Next year the company plans a second workshop and a winter collection. The search below finds the word invoice on page 2.'],
];
chapters.forEach(([title, body], i) => {
if (i) report.addPage();
report.addBookmark(title, { page: i });
report.text((i + 1) + '. ' + title, { size: 22, bold: true, color: '#0f766e', marginBottom: 10 });
report.text(body + ' ' + body, { size: 12, align: 'justify', marginBottom: 14 });
const page = report.page(i);
[60, 95, 75, 120, 105].forEach((h, j) => page.drawRect(70 + j * 90, 420 - h, 60, h, { fill: ['#99f6e4', '#5eead4', '#2dd4bf', '#14b8a6', '#0d9488'][j] }));
page.drawLine(60, 420, 520, 420, { color: '#64748b', width: 1 });
});
const ed = VibeFoxJSPdf.createPdfViewer('#app', { document: report, pageEditing: true, sidebar: 'thumbnails', zoom: 0.5 });
show('select thumbnails and use the page tool bar, or press the buttons');
ed.on('pageEdit', (e) => show(e.action + ' -> pages ' + e.pages.map((p) => p + 1).join(', ')));
addButtons([
['Rotate pages 1-2', () => { ed.selectPages([0, 1]); ed.rotatePages(); }],
['Page 3 to the front', () => ed.movePages([2], 0)],
['Delete page 4', () => ed.removePages([3])],
['Undo', () => ed.undo()],
['Redo', () => ed.redo()],
]);Printing
The print button of the toolbar or Print pages 1–2 opens the print dialog of the browser with the pages drawn at 200 dpi; the box shows the progress.
function addButtons(buttons) {
const bar = document.createElement('div');
for (const [text, onClick] of buttons) {
const button = document.createElement('button');
button.textContent = text;
button.onclick = onClick;
bar.append(button);
}
document.getElementById('app').before(bar);
return bar;
}
document.getElementById('app').style.height = '420px';
const output = document.createElement('pre');
output.style.cssText = 'height: 30px; overflow: auto; margin: 6px 0 0; padding: 4px 8px; border: 1px solid #d0d7de; font: 12px ui-monospace, Consolas, monospace; white-space: pre-wrap';
document.getElementById('app').after(output);
const show = (...lines) => { output.textContent = lines.join('\n'); };
const report = new VibeFoxJSPdf.PdfDocument({ metadata: { title: 'Annual report 2026' }, footer: { text: 'Page {page} of {pages}', align: 'right', size: 9 } });
const chapters = [
['Introduction', 'This report describes the year of a small company that makes garden furniture. Sales grew in every region, and the new online shop brought many first-time customers.'],
['Sales', 'Sales reached 4.2 million EUR, 12 % more than last year. Chairs and tables sold best; the invoice total of the largest order was 48 000 EUR.'],
['Customers', 'Most customers are families and small cafes. Write to [email protected] or call +47 22 33 44 55 for a catalog.'],
['Outlook', 'Next year the company plans a second workshop and a winter collection. The search below finds the word invoice on page 2.'],
];
chapters.forEach(([title, body], i) => {
if (i) report.addPage();
report.addBookmark(title, { page: i });
report.text((i + 1) + '. ' + title, { size: 22, bold: true, color: '#0f766e', marginBottom: 10 });
report.text(body + ' ' + body, { size: 12, align: 'justify', marginBottom: 14 });
const page = report.page(i);
[60, 95, 75, 120, 105].forEach((h, j) => page.drawRect(70 + j * 90, 420 - h, 60, h, { fill: ['#99f6e4', '#5eead4', '#2dd4bf', '#14b8a6', '#0d9488'][j] }));
page.drawLine(60, 420, 520, 420, { color: '#64748b', width: 1 });
});
const viewer = VibeFoxJSPdf.createPdfViewer('#app', { document: report, zoom: 0.6 });
show('press Print pages 1-2 or the print button of the toolbar');
viewer.on('printProgress', (e) => show('prepared ' + e.done + ' of ' + e.total));
addButtons([['Print pages 1-2', async () => {
const result = await viewer.print({ pages: '1-2', dpi: 200 });
if (result) show('printed pages ' + result.pages.map((p) => p + 1).join(', ') + ' at ' + result.dpi + ' dpi');
}]]);Drawing a page without the viewer
Each page of the report drawn into its own canvas at half size, without the viewer component.
const report = new VibeFoxJSPdf.PdfDocument({ metadata: { title: 'Annual report 2026' }, footer: { text: 'Page {page} of {pages}', align: 'right', size: 9 } });
const chapters = [
['Introduction', 'This report describes the year of a small company that makes garden furniture. Sales grew in every region, and the new online shop brought many first-time customers.'],
['Sales', 'Sales reached 4.2 million EUR, 12 % more than last year. Chairs and tables sold best; the invoice total of the largest order was 48 000 EUR.'],
['Customers', 'Most customers are families and small cafes. Write to [email protected] or call +47 22 33 44 55 for a catalog.'],
['Outlook', 'Next year the company plans a second workshop and a winter collection. The search below finds the word invoice on page 2.'],
];
chapters.forEach(([title, body], i) => {
if (i) report.addPage();
report.addBookmark(title, { page: i });
report.text((i + 1) + '. ' + title, { size: 22, bold: true, color: '#0f766e', marginBottom: 10 });
report.text(body + ' ' + body, { size: 12, align: 'justify', marginBottom: 14 });
const page = report.page(i);
[60, 95, 75, 120, 105].forEach((h, j) => page.drawRect(70 + j * 90, 420 - h, 60, h, { fill: ['#99f6e4', '#5eead4', '#2dd4bf', '#14b8a6', '#0d9488'][j] }));
page.drawLine(60, 420, 520, 420, { color: '#64748b', width: 1 });
});
const app = document.getElementById('app');
app.style.cssText = 'display: flex; flex-wrap: wrap; gap: 8px';
for (let i = 0; i < report.pageCount; i++) {
const canvas = document.createElement('canvas');
canvas.style.border = '1px solid #cbd5e1';
app.append(canvas);
await VibeFoxJSPdf.renderPageToCanvas(report, i, canvas, { scale: 0.3, background: '#ffffff' });
}Rendering PDF files
The renderer option and pdf.js
An opened PDF file drawn by the library's own renderer (renderer: 'lite'): vector shapes, text in the standard fonts and a transparent image; pdf.js is not used.
document.getElementById('app').style.height = '440px';
// the bytes of a PDF file (made here by the library; a file from the user works the same way)
const source = new VibeFoxJSPdf.PdfDocument();
source.text('A file opened from bytes', { size: 20, bold: true });
source.svg('<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200"><circle cx="100" cy="100" r="90" fill="#0ea5e9" opacity=".5"/>' +
'<circle cx="140" cy="100" r="60" fill="#f97316" opacity=".6"/></svg>', { width: 160 });
const bytes = await source.toBytes();
VibeFoxJSPdf.useLiteWorker(true); // default: the own renderer runs in a worker
VibeFoxJSPdf.createPdfViewer('#app', { document: await VibeFoxJSPdf.loadPdf(bytes), renderer: 'lite', zoom: 0.75 });Low-level functions
One page of an opened file painted into a canvas with renderPageLite; the text items it returned are outlined in red at their positions.
const source = new VibeFoxJSPdf.PdfDocument({ format: [320, 200], margins: { top: 30, right: 30, bottom: 30, left: 30 } });
source.text('Low-level rendering', { size: 16, bold: true });
source.text('renderPageLite paints the page and also returns the text items with their positions.');
const opened = await VibeFoxJSPdf.loadPdf(await source.toBytes());
const canvas = document.createElement('canvas');
canvas.style.border = '1px solid #cbd5e1';
document.getElementById('app').append(canvas);
const scale = 1.2;
const r = await VibeFoxJSPdf.renderPageLite(opened.page(0), canvas, { scale, text: true });
const ctx = canvas.getContext('2d');
const h = opened.page(0).height;
ctx.strokeStyle = '#dc2626';
for (const t of r.text) { // transform: PDF coordinates (from the bottom)
const [, , , , x, y] = t.transform;
ctx.strokeRect(x * scale, (h - y - t.height * 0.8) * scale, t.width * scale, t.height * scale);
}Text extraction and search
The text of the report read by the library; the amounts found by a regular expression and the e-mail found by the ready-made pattern are marked with highlight annotations in the viewer.
document.getElementById('app').style.height = '400px';
const output = document.createElement('pre');
output.style.cssText = 'height: 60px; overflow: auto; margin: 6px 0 0; padding: 4px 8px; border: 1px solid #d0d7de; font: 12px ui-monospace, Consolas, monospace; white-space: pre-wrap';
document.getElementById('app').after(output);
const show = (...lines) => { output.textContent = lines.join('\n'); };
const report = new VibeFoxJSPdf.PdfDocument({ metadata: { title: 'Annual report 2026' }, footer: { text: 'Page {page} of {pages}', align: 'right', size: 9 } });
const chapters = [
['Introduction', 'This report describes the year of a small company that makes garden furniture. Sales grew in every region, and the new online shop brought many first-time customers.'],
['Sales', 'Sales reached 4.2 million EUR, 12 % more than last year. Chairs and tables sold best; the invoice total of the largest order was 48 000 EUR.'],
['Customers', 'Most customers are families and small cafes. Write to [email protected] or call +47 22 33 44 55 for a catalog.'],
['Outlook', 'Next year the company plans a second workshop and a winter collection. The search below finds the word invoice on page 2.'],
];
chapters.forEach(([title, body], i) => {
if (i) report.addPage();
report.addBookmark(title, { page: i });
report.text((i + 1) + '. ' + title, { size: 22, bold: true, color: '#0f766e', marginBottom: 10 });
report.text(body + ' ' + body, { size: 12, align: 'justify', marginBottom: 14 });
const page = report.page(i);
[60, 95, 75, 120, 105].forEach((h, j) => page.drawRect(70 + j * 90, 420 - h, 60, h, { fill: ['#99f6e4', '#5eead4', '#2dd4bf', '#14b8a6', '#0d9488'][j] }));
page.drawLine(60, 420, 520, 420, { color: '#64748b', width: 1 });
});
const { text, pages } = await report.extractText();
const amounts = await report.searchText(/\d[\d ]*\d EUR/);
const emails = await report.searchText('email');
for (const hit of amounts.concat(emails)) {
report.addAnnotation(hit.page, { type: 'highlight', rects: hit.rects, color: '#fde047' });
}
show(pages.length + ' pages, ' + text.length + ' characters: ' + text.slice(0, 60) + '…',
'found: ' + amounts.concat(emails).map((h) => 'page ' + (h.page + 1) + ': ' + h.text).join('; '));
VibeFoxJSPdf.createPdfViewer('#app', { document: report, page: 3, zoom: 0.6 });Annotations
From code
A highlight, a note icon, a red box, an arrow, a text box and an APPROVED stamp added from code; they are saved as real PDF annotations.
document.getElementById('app').style.height = '460px';
const doc = new VibeFoxJSPdf.PdfDocument();
doc.text('Invoice 2026-017', { size: 20, bold: true });
doc.text('Total amount: 1 250.00 EUR', { size: 14 });
doc.addAnnotation(0, { type: 'highlight', rects: [[50, 82, 190, 20]], text: 'Check this number', author: 'Anna' });
doc.addAnnotation(0, { type: 'note', x: 500, y: 100, text: 'Please confirm the total.' });
doc.addAnnotation(0, { type: 'rect', rect: [45, 45, 200, 34], color: '#dc2626', width: 2 });
doc.addAnnotation(0, { type: 'line', points: [[300, 200], [250, 110]], arrow: true, color: '#2563eb' });
doc.addAnnotation(0, { type: 'freetext', rect: [300, 200, 200, 40], text: 'Updated in March', fontSize: 11 });
const stamp = doc.addAnnotation(0, { type: 'stamp', stamp: 'approved', x: 380, y: 60 });
doc.updateAnnotation(stamp.id, { rect: [360, 260, 160, 40] }); // move / resize
VibeFoxJSPdf.createPdfViewer('#app', { document: doc, zoom: 0.9 });Exchange and flattening
The annotations of a document exported as XFDF (the box), imported into a copy and flattened there: on the right page they are part of the page content and can no longer be edited.
document.getElementById('app').style.height = '420px';
const output = document.createElement('pre');
output.style.cssText = 'height: 70px; overflow: auto; margin: 6px 0 0; padding: 4px 8px; border: 1px solid #d0d7de; font: 12px ui-monospace, Consolas, monospace; white-space: pre-wrap';
document.getElementById('app').after(output);
const show = (...lines) => { output.textContent = lines.join('\n'); };
const doc = new VibeFoxJSPdf.PdfDocument();
doc.text('Original with annotations', { size: 18, bold: true });
doc.addAnnotation(0, { type: 'rect', rect: [45, 40, 260, 34], color: '#dc2626', width: 2 });
doc.addAnnotation(0, { type: 'stamp', stamp: 'reviewed', x: 330, y: 120 });
const xfdf = doc.exportAnnotations({ format: 'xfdf' }); // the XML exchange format of PDF readers
const copy = VibeFoxJSPdf.PdfDocument.fromJSON(doc.toJSON());
copy.importAnnotations(xfdf, { replace: true });
copy.page(0).text('Flattened copy', { x: 50, y: 90, size: 14, color: '#0f766e' });
copy.flatten({ annotations: true, forms: true }); // annotations become page content
show(xfdf);
VibeFoxJSPdf.createPdfViewer('#app', { document: VibeFoxJSPdf.PdfDocument.merge(doc, copy), zoom: 0.45, sidebar: 'thumbnails' });In the viewer
The annotation toolbar: pick highlight and select text, draw with ink, drag a box or an arrow, click to place a note, or pick a stamp; select an annotation to move, resize or delete it. The box lists what was added.
function addButtons(buttons) {
const bar = document.createElement('div');
for (const [text, onClick] of buttons) {
const button = document.createElement('button');
button.textContent = text;
button.onclick = onClick;
bar.append(button);
}
document.getElementById('app').before(bar);
return bar;
}
document.getElementById('app').style.height = '480px';
const output = document.createElement('pre');
output.style.cssText = 'height: 30px; overflow: auto; margin: 6px 0 0; padding: 4px 8px; border: 1px solid #d0d7de; font: 12px ui-monospace, Consolas, monospace; white-space: pre-wrap';
document.getElementById('app').after(output);
const show = (...lines) => { output.textContent = lines.join('\n'); };
const report = new VibeFoxJSPdf.PdfDocument({ metadata: { title: 'Annual report 2026' }, footer: { text: 'Page {page} of {pages}', align: 'right', size: 9 } });
const chapters = [
['Introduction', 'This report describes the year of a small company that makes garden furniture. Sales grew in every region, and the new online shop brought many first-time customers.'],
['Sales', 'Sales reached 4.2 million EUR, 12 % more than last year. Chairs and tables sold best; the invoice total of the largest order was 48 000 EUR.'],
['Customers', 'Most customers are families and small cafes. Write to [email protected] or call +47 22 33 44 55 for a catalog.'],
['Outlook', 'Next year the company plans a second workshop and a winter collection. The search below finds the word invoice on page 2.'],
];
chapters.forEach(([title, body], i) => {
if (i) report.addPage();
report.addBookmark(title, { page: i });
report.text((i + 1) + '. ' + title, { size: 22, bold: true, color: '#0f766e', marginBottom: 10 });
report.text(body + ' ' + body, { size: 12, align: 'justify', marginBottom: 14 });
const page = report.page(i);
[60, 95, 75, 120, 105].forEach((h, j) => page.drawRect(70 + j * 90, 420 - h, 60, h, { fill: ['#99f6e4', '#5eead4', '#2dd4bf', '#14b8a6', '#0d9488'][j] }));
page.drawLine(60, 420, 520, 420, { color: '#64748b', width: 1 });
});
const av = VibeFoxJSPdf.createPdfViewer('#app', { document: report, annotations: true, author: 'Anna', zoom: 0.75 });
show('pick a tool and draw on the page');
av.on('annotationAdd', (e) => show('added ' + e.annotation.type + ' on page ' + (e.page + 1)));
addButtons([
['Highlight', () => av.setTool('highlight')],
['Ink', () => av.setTool('ink')],
['Stamp "Paid"', () => av.placeStamp({ stamp: 'paid' })], // follows the pointer until a click
['Select', () => av.setTool('select')],
['Undo', () => av.undo()],
]);Visual signatures
A drawn signature with the date and a typed one in a cursive system font. Sign opens the signature dialog (Draw, Type, Image), then click the page to put the signature down.
function addButtons(buttons) {
const bar = document.createElement('div');
for (const [text, onClick] of buttons) {
const button = document.createElement('button');
button.textContent = text;
button.onclick = onClick;
bar.append(button);
}
document.getElementById('app').before(bar);
return bar;
}
document.getElementById('app').style.height = '440px';
const doc = new VibeFoxJSPdf.PdfDocument();
doc.text('Delivery note 448', { size: 20, bold: true });
doc.text('Received in good condition.');
const sig = VibeFoxJSPdf.drawnSignature([[[0, 20], [10, 2], [20, 28], [32, 4], [44, 26], [56, 6], [70, 22], [84, 10]]], { color: '#1e3a8a' });
doc.page(0).addSignature(sig, 50, 130, { width: 140, date: true, author: 'Anna Berg' });
const viewer = VibeFoxJSPdf.createPdfViewer('#app', { document: doc, annotations: true, zoom: 0.9 });
viewer.addSignature(VibeFoxJSPdf.typedSignature('Anna Berg'), { page: 1, x: 320, y: 130, width: 160 });
addButtons([['Sign', () => viewer.openSignatureDialog()]]);Forms
Creating fields
A fillable form made in code: text fields with number and e-mail formats, a check box, radio buttons, a combo box, a list, a signature field and buttons. Fill it in right here.
document.getElementById('app').style.height = '520px';
const form = new VibeFoxJSPdf.PdfDocument();
const fp = form.addPage();
fp.text('Order form', { x: 50, y: 20, size: 18, bold: true });
[['Name', 60], ['Amount', 90], ['E-mail', 120], ['Terms', 150], ['Plan', 180], ['Country', 210], ['Region', 240], ['Signature', 315]]
.forEach(([label, y]) => fp.text(label, { x: 50, y, size: 11 }));
fp.addTextField('name', 150, 55, 200, { required: true, tooltip: 'Your full name' });
fp.addTextField('amount', 150, 85, 100, { format: { type: 'number', decimals: 2, min: 0 }, align: 'right' });
fp.addTextField('email', 150, 115, 200, { format: 'email' });
fp.addCheckbox('terms', 150, 145, { checked: false });
fp.addRadioButton('plan', 'basic', 150, 175);
fp.addRadioButton('plan', 'pro', 220, 175, { checked: true });
fp.text('basic', { x: 168, y: 180, size: 9 });
fp.text('pro', { x: 238, y: 180, size: 9 });
fp.addComboBox('country', 150, 205, 150, { options: ['Norway', 'Sweden', { value: 'FI', label: 'Finland' }], value: 'Norway' });
fp.addListBox('region', 150, 235, 150, { options: ['North', 'South', 'East'], multiple: true, height: 50 });
fp.addSignatureField('signature', 150, 300, 180, 50);
fp.addButton('reset', 150, 370, 80, 24, { label: 'Reset', action: 'reset' });
VibeFoxJSPdf.createPdfViewer('#app', { document: form, forms: true, zoom: 0.9 });Filling and reading
The form is filled from code; the box shows the values read back, the result of the check and the data exported as XFDF. Reset amount and Fill again change the fields in the viewer too.
function addButtons(buttons) {
const bar = document.createElement('div');
for (const [text, onClick] of buttons) {
const button = document.createElement('button');
button.textContent = text;
button.onclick = onClick;
bar.append(button);
}
document.getElementById('app').before(bar);
return bar;
}
document.getElementById('app').style.height = '380px';
const output = document.createElement('pre');
output.style.cssText = 'height: 110px; overflow: auto; margin: 6px 0 0; padding: 4px 8px; border: 1px solid #d0d7de; font: 12px ui-monospace, Consolas, monospace; white-space: pre-wrap';
document.getElementById('app').after(output);
const show = (...lines) => { output.textContent = lines.join('\n'); };
const form = new VibeFoxJSPdf.PdfDocument();
const fp = form.addPage();
['Name', 'Amount', 'Terms', 'Country'].forEach((label, i) => fp.text(label, { x: 50, y: 60 + i * 30, size: 11 }));
fp.addTextField('name', 150, 55, 200, { required: true });
fp.addTextField('amount', 150, 85, 100, { format: { type: 'number', decimals: 2, min: 0 }, align: 'right' });
fp.addCheckbox('terms', 150, 115);
fp.addComboBox('country', 150, 145, 150, { options: ['Norway', 'Sweden', { value: 'FI', label: 'Finland' }] });
const report = () => {
const check = form.validateForm(); // { valid, errors: [{ name, code, message }] }
show('values: ' + JSON.stringify(form.getFieldValues()),
'valid: ' + check.valid + (check.errors.length ? ' (' + check.errors.map((e) => e.name + ': ' + e.code).join(', ') + ')' : ''),
form.exportFormData({ format: 'xfdf' }));
};
form.setFieldValues({ name: 'Anna Berg', amount: '42', terms: true, country: 'FI' });
report();
addButtons([
['Reset amount', () => { form.resetForm(['amount']); report(); }],
['Fill again', () => { form.setFieldValues({ amount: '99.5' }); report(); }],
]);
VibeFoxJSPdf.createPdfViewer('#app', { document: form, forms: true, zoom: 0.8 });Filling in the viewer
Type into the fields: each change is written into the box. Send never sends anything: it checks the form, marks the required empty field and gives the values to your code.
document.getElementById('app').style.height = '380px';
const output = document.createElement('pre');
output.style.cssText = 'height: 60px; overflow: auto; margin: 6px 0 0; padding: 4px 8px; border: 1px solid #d0d7de; font: 12px ui-monospace, Consolas, monospace; white-space: pre-wrap';
document.getElementById('app').after(output);
const show = (...lines) => { output.textContent = lines.join('\n'); };
const form = new VibeFoxJSPdf.PdfDocument();
const fp = form.addPage();
fp.text('Name *', { x: 50, y: 60, size: 11 });
fp.text('E-mail', { x: 50, y: 90, size: 11 });
fp.addTextField('name', 150, 55, 200, { required: true, tooltip: 'Name' });
fp.addTextField('email', 150, 85, 200, { format: 'email', tooltip: 'E-mail' });
fp.addButton('send', 150, 125, 80, 24, { label: 'Send', action: { submit: 'https://example.com/form', format: 'xfdf' } });
const fv = VibeFoxJSPdf.createPdfViewer('#app', { document: form, forms: true, zoom: 0.9 });
show('type into the fields, then press Send');
fv.on('fieldChange', (e) => show(e.name + ' = ' + JSON.stringify(e.value)));
fv.on('formSubmit', (e) => show(e.valid ? 'send these values yourself: ' + JSON.stringify(e.values) : 'not valid: ' + e.errors.map((x) => x.message).join('; ')));Redaction
The e-mail address and the phone number are marked for redaction (red boxes, listed in the panel). Press Apply redactions: the text under the marks is removed from the file and black boxes are drawn; then search for the address — it is gone.
function addButtons(buttons) {
const bar = document.createElement('div');
for (const [text, onClick] of buttons) {
const button = document.createElement('button');
button.textContent = text;
button.onclick = onClick;
bar.append(button);
}
document.getElementById('app').before(bar);
return bar;
}
document.getElementById('app').style.height = '460px';
const output = document.createElement('pre');
output.style.cssText = 'height: 30px; overflow: auto; margin: 6px 0 0; padding: 4px 8px; border: 1px solid #d0d7de; font: 12px ui-monospace, Consolas, monospace; white-space: pre-wrap';
document.getElementById('app').after(output);
const show = (...lines) => { output.textContent = lines.join('\n'); };
const report = new VibeFoxJSPdf.PdfDocument({ metadata: { title: 'Annual report 2026' }, footer: { text: 'Page {page} of {pages}', align: 'right', size: 9 } });
const chapters = [
['Introduction', 'This report describes the year of a small company that makes garden furniture. Sales grew in every region, and the new online shop brought many first-time customers.'],
['Sales', 'Sales reached 4.2 million EUR, 12 % more than last year. Chairs and tables sold best; the invoice total of the largest order was 48 000 EUR.'],
['Customers', 'Most customers are families and small cafes. Write to [email protected] or call +47 22 33 44 55 for a catalog.'],
['Outlook', 'Next year the company plans a second workshop and a winter collection. The search below finds the word invoice on page 2.'],
];
chapters.forEach(([title, body], i) => {
if (i) report.addPage();
report.addBookmark(title, { page: i });
report.text((i + 1) + '. ' + title, { size: 22, bold: true, color: '#0f766e', marginBottom: 10 });
report.text(body + ' ' + body, { size: 12, align: 'justify', marginBottom: 14 });
const page = report.page(i);
[60, 95, 75, 120, 105].forEach((h, j) => page.drawRect(70 + j * 90, 420 - h, 60, h, { fill: ['#99f6e4', '#5eead4', '#2dd4bf', '#14b8a6', '#0d9488'][j] }));
page.drawLine(60, 420, 520, 420, { color: '#64748b', width: 1 });
});
const marks = await report.redactText(['email', 'phone'], { text: 'REMOVED' });
const viewer = VibeFoxJSPdf.createPdfViewer('#app', { document: report, redaction: true, page: 3, zoom: 0.6 });
viewer.toggleRedactionList(true);
show(marks.length + ' marks: ' + marks.map((m) => m.match).join(', '));
addButtons([['Apply redactions', async () => {
const result = await viewer.applyRedactions();
if (result) show('removed: ' + result.removed.text + ' characters, ' + result.marks + ' marks');
}], ['Search "example.com"', async () => {
const hits = await report.searchText('example.com');
show('example.com found ' + hits.length + ' times');
}]]);Document comparison
Comparison view
Two versions of a contract side by side, scrolled together: deleted words marked on the old one, inserted words on the new one, and the list of changes. Next change and Overlay call the methods.
function addButtons(buttons) {
const bar = document.createElement('div');
for (const [text, onClick] of buttons) {
const button = document.createElement('button');
button.textContent = text;
button.onclick = onClick;
bar.append(button);
}
document.getElementById('app').before(bar);
return bar;
}
document.getElementById('app').style.height = '500px';
const contract = (price, days, extra) => {
const d = new VibeFoxJSPdf.PdfDocument();
d.text('Supply contract', { size: 20, bold: true, marginBottom: 10 });
d.text('The supplier delivers ten chairs and two tables to the buyer. The price is ' + price + ' EUR, paid within ' + days + ' days after the delivery.');
d.text('The goods are delivered to the shop of the buyer in Oslo.' + extra);
return d.toBytes();
};
const oldBytes = await contract('1 250', 30, '');
const newBytes = await contract('1 400', 14, ' Delivery is free of charge.');
const cv = VibeFoxJSPdf.createPdfCompare('#app', { from: oldBytes, to: newBytes, layout: 'side', list: true, zoom: 0.6 });
addButtons([
['Next change', () => cv.nextChange()],
['Overlay', () => cv.setLayout('overlay')],
['Side by side', () => cv.setLayout('side')],
['HTML report', () => cv.downloadReport('html')],
]);Digital signatures
Signing
A contract signed in the browser with WebCrypto and a test certificate written into the code; the signed file is opened in the viewer, which checks the signature and shows it in the status line and the Signatures tab.
function addButtons(buttons) {
const bar = document.createElement('div');
for (const [text, onClick] of buttons) {
const button = document.createElement('button');
button.textContent = text;
button.onclick = onClick;
bar.append(button);
}
document.getElementById('app').before(bar);
return bar;
}
document.getElementById('app').style.height = '520px';
// a test key and certificate (EC P-256, issued by a test root CA); in a real page: the user's .p12 file and password
const KEY = '-----BEGIN PRIVATE KEY-----\n' +
'MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgf3aXiL7utR4YWaMr\n' +
'gXP4APnDYB32vK5jw1yEVrF/zSChRANCAAQ9PMZ14PuMmLqVMYXKsNmALZUdSJy8\n' +
'Ne8gVsiQHrmHJ2Z5Oc8/qee5NTcXTaWLoId15h80MeUa7FUXuWX/sWIT\n' +
'-----END PRIVATE KEY-----';
const CERTIFICATE = '-----BEGIN CERTIFICATE-----\n' +
'MIIB4TCCAYagAwIBAgICA+kwCgYIKoZIzj0EAwIwUTELMAkGA1UEBhMCTk8xHDAa\n' +
'BgNVBAoME1ZpYmVGb3ggSlMgUERGIGRlbW8xJDAiBgNVBAMMG1ZpYmVGb3ggSlMg\n' +
'UERGIERlbW8gUm9vdCBDQTAeFw0yNjAxMDEwMDAwMDBaFw0zNjAxMDEwMDAwMDBa\n' +
'MD8xCzAJBgNVBAYTAk5PMRwwGgYDVQQKDBNWaWJlRm94IEpTIFBERiBkZW1vMRIw\n' +
'EAYDVQQDDAlBbm5hIEJlcmcwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAAQ9PMZ1\n' +
'4PuMmLqVMYXKsNmALZUdSJy8Ne8gVsiQHrmHJ2Z5Oc8/qee5NTcXTaWLoId15h80\n' +
'MeUa7FUXuWX/sWITo2AwXjAMBgNVHRMBAf8EAjAAMA4GA1UdDwEB/wQEAwIGwDAd\n' +
'BgNVHQ4EFgQU8Tx072BdCKzf8frOUCWvooXeP7cwHwYDVR0jBBgwFoAUKj3gs+Yq\n' +
'irMa5H2ELdw2kE3Rb6IwCgYIKoZIzj0EAwIDSQAwRgIhANNqjiZPIDkenUwPGp2/\n' +
'IaJUiKb79y4ZQIwpdOF5VLVVAiEA0Zyk6WggkVewnX3zcD8WH3cLbbQiTGDwCj9x\n' +
'HjMn6LA=\n' +
'-----END CERTIFICATE-----';
const ROOT = '-----BEGIN CERTIFICATE-----\n' +
'MIIB9DCCAZugAwIBAgICA+gwCgYIKoZIzj0EAwIwUTELMAkGA1UEBhMCTk8xHDAa\n' +
'BgNVBAoME1ZpYmVGb3ggSlMgUERGIGRlbW8xJDAiBgNVBAMMG1ZpYmVGb3ggSlMg\n' +
'UERGIERlbW8gUm9vdCBDQTAeFw0yNjAxMDEwMDAwMDBaFw0zNjAxMDEwMDAwMDBa\n' +
'MFExCzAJBgNVBAYTAk5PMRwwGgYDVQQKDBNWaWJlRm94IEpTIFBERiBkZW1vMSQw\n' +
'IgYDVQQDDBtWaWJlRm94IEpTIFBERiBEZW1vIFJvb3QgQ0EwWTATBgcqhkjOPQIB\n' +
'BggqhkjOPQMBBwNCAATo7kfiZysjnumTeCWdAnUWDe8vvFg3hQCMLQw2QoYt3Za4\n' +
'L2cqO7scSj7aC1CJ+spEtSZ1cos+h92jciLt/s+yo2MwYTAPBgNVHRMBAf8EBTAD\n' +
'AQH/MA4GA1UdDwEB/wQEAwIBBjAdBgNVHQ4EFgQUKj3gs+YqirMa5H2ELdw2kE3R\n' +
'b6IwHwYDVR0jBBgwFoAUKj3gs+YqirMa5H2ELdw2kE3Rb6IwCgYIKoZIzj0EAwID\n' +
'RwAwRAIgHVcAcrYTfj6KZg4o8YwGUIGkUDCTkZeJ/yoa67ana4wCIBJ/B25tsFmF\n' +
'x9Jo+OdOHcFTC6qRdHo8P4UlU+3pXn4I\n' +
'-----END CERTIFICATE-----';
const pkcs8 = Uint8Array.from(atob(KEY.replace(/-----[^-]+-----|\s/g, '')), (ch) => ch.charCodeAt(0));
const key = await crypto.subtle.importKey('pkcs8', pkcs8, { name: 'ECDSA', namedCurve: 'P-256' }, false, ['sign']);
const doc = new VibeFoxJSPdf.PdfDocument();
doc.text('Supply contract 14', { size: 20, bold: true });
doc.text('The supplier delivers ten chairs and two tables to the buyer.');
const signed = await VibeFoxJSPdf.signPdf(doc, {
key,
certificate: CERTIFICATE,
chain: [ROOT],
reason: 'Approved',
location: 'Oslo',
page: 0,
rect: [50, 120, 260, 60], // visible signature box; no rect = invisible
appearance: { signature: VibeFoxJSPdf.typedSignature('Anna Berg') },
});
const viewer = VibeFoxJSPdf.createPdfViewer('#app', {
document: await VibeFoxJSPdf.loadPdf(signed),
sidebar: 'signatures',
signatureValidation: { trusted: ROOT }, // the roots you trust
zoom: 0.75,
});
addButtons([['Download signed PDF', () => viewer.download()]]);Validation
The signed contract is checked with the test root as trusted (valid) and with nothing trusted (unknown signer); then text is added to the page after signing, and the viewer shows what the check says about that version in its status line and the Signatures tab.
document.getElementById('app').style.height = '440px';
const output = document.createElement('pre');
output.style.cssText = 'height: 90px; overflow: auto; margin: 6px 0 0; padding: 4px 8px; border: 1px solid #d0d7de; font: 12px ui-monospace, Consolas, monospace; white-space: pre-wrap';
document.getElementById('app').after(output);
const show = (...lines) => { output.textContent = lines.join('\n'); };
// the same test key and certificates as in the Signing example
const KEY = 'MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgf3aXiL7utR4YWaMrgXP4APnDYB32vK5jw1yEVrF/zSChRANCAAQ9PMZ14PuMmLqVMYXKsNmALZUdSJy8Ne8gVsiQHrmHJ2Z5Oc8/qee5NTcXTaWLoId15h80MeUa7FUXuWX/sWIT';
const CERTIFICATE = '-----BEGIN CERTIFICATE-----\n' +
'MIIB4TCCAYagAwIBAgICA+kwCgYIKoZIzj0EAwIwUTELMAkGA1UEBhMCTk8xHDAaBgNVBAoME1ZpYmVGb3ggSlMgUERGIGRlbW8xJDAiBgNVBAMMG1ZpYmVGb3ggSlMg' +
'UERGIERlbW8gUm9vdCBDQTAeFw0yNjAxMDEwMDAwMDBaFw0zNjAxMDEwMDAwMDBaMD8xCzAJBgNVBAYTAk5PMRwwGgYDVQQKDBNWaWJlRm94IEpTIFBERiBkZW1vMRIw' +
'EAYDVQQDDAlBbm5hIEJlcmcwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAAQ9PMZ14PuMmLqVMYXKsNmALZUdSJy8Ne8gVsiQHrmHJ2Z5Oc8/qee5NTcXTaWLoId15h80' +
'MeUa7FUXuWX/sWITo2AwXjAMBgNVHRMBAf8EAjAAMA4GA1UdDwEB/wQEAwIGwDAdBgNVHQ4EFgQU8Tx072BdCKzf8frOUCWvooXeP7cwHwYDVR0jBBgwFoAUKj3gs+Yq' +
'irMa5H2ELdw2kE3Rb6IwCgYIKoZIzj0EAwIDSQAwRgIhANNqjiZPIDkenUwPGp2/IaJUiKb79y4ZQIwpdOF5VLVVAiEA0Zyk6WggkVewnX3zcD8WH3cLbbQiTGDwCj9x' +
'HjMn6LA=\n' +
'-----END CERTIFICATE-----';
const ROOT = '-----BEGIN CERTIFICATE-----\n' +
'MIIB9DCCAZugAwIBAgICA+gwCgYIKoZIzj0EAwIwUTELMAkGA1UEBhMCTk8xHDAaBgNVBAoME1ZpYmVGb3ggSlMgUERGIGRlbW8xJDAiBgNVBAMMG1ZpYmVGb3ggSlMg' +
'UERGIERlbW8gUm9vdCBDQTAeFw0yNjAxMDEwMDAwMDBaFw0zNjAxMDEwMDAwMDBaMFExCzAJBgNVBAYTAk5PMRwwGgYDVQQKDBNWaWJlRm94IEpTIFBERiBkZW1vMSQw' +
'IgYDVQQDDBtWaWJlRm94IEpTIFBERiBEZW1vIFJvb3QgQ0EwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAATo7kfiZysjnumTeCWdAnUWDe8vvFg3hQCMLQw2QoYt3Za4' +
'L2cqO7scSj7aC1CJ+spEtSZ1cos+h92jciLt/s+yo2MwYTAPBgNVHRMBAf8EBTADAQH/MA4GA1UdDwEB/wQEAwIBBjAdBgNVHQ4EFgQUKj3gs+YqirMa5H2ELdw2kE3R' +
'b6IwHwYDVR0jBBgwFoAUKj3gs+YqirMa5H2ELdw2kE3Rb6IwCgYIKoZIzj0EAwIDRwAwRAIgHVcAcrYTfj6KZg4o8YwGUIGkUDCTkZeJ/yoa67ana4wCIBJ/B25tsFmF' +
'x9Jo+OdOHcFTC6qRdHo8P4UlU+3pXn4I\n' +
'-----END CERTIFICATE-----';
const pkcs8 = Uint8Array.from(atob(KEY), (ch) => ch.charCodeAt(0));
const key = await crypto.subtle.importKey('pkcs8', pkcs8, { name: 'ECDSA', namedCurve: 'P-256' }, false, ['sign']);
const doc = new VibeFoxJSPdf.PdfDocument();
doc.text('Supply contract 14', { size: 20, bold: true });
const signed = await VibeFoxJSPdf.signPdf(doc, { key, certificate: CERTIFICATE, chain: [ROOT], reason: 'Approved' });
const line = (title, r) => title + ': ' + r.status + ' — ' + r.signatures.map((s) =>
s.signer.commonName + ', ' + s.status + ', certificate ' + (s.certificate ? s.certificate.status : '-') + ', changes after signing: ' + s.modifications.changes.length).join('; ');
const trusted = await VibeFoxJSPdf.verifyPdf(signed, { trusted: ROOT });
const nothingTrusted = await VibeFoxJSPdf.verifyPdf(signed);
const changed = await VibeFoxJSPdf.loadPdf(signed);
changed.page(0).text('Price changed after signing', { x: 50, y: 300, color: '#dc2626' });
const afterChange = await VibeFoxJSPdf.verifyPdf(await changed.toBytes(), { trusted: ROOT });
show(line('trusted root', trusted), line('nothing trusted', nothingTrusted), line('page changed after signing', afterChange));
VibeFoxJSPdf.createPdfViewer('#app', { document: await VibeFoxJSPdf.loadPdf(await changed.toBytes()), signatureValidation: { trusted: ROOT }, sidebar: 'signatures', zoom: 0.6 });Encryption and permissions
A document saved with AES-256 encryption that opens without a password but does not allow printing or copying: the viewer opens it, and its print button is disabled. The box shows the permissions read from the file.
document.getElementById('app').style.height = '380px';
const output = document.createElement('pre');
output.style.cssText = 'height: 50px; overflow: auto; margin: 6px 0 0; padding: 4px 8px; border: 1px solid #d0d7de; font: 12px ui-monospace, Consolas, monospace; white-space: pre-wrap';
document.getElementById('app').after(output);
const show = (...lines) => { output.textContent = lines.join('\n'); };
const doc = new VibeFoxJSPdf.PdfDocument();
doc.text('Confidential price list', { size: 20, bold: true });
doc.text('Opens without a password; printing and copying are not allowed.');
doc.setEncryption({
userPassword: '', // opens without a password
ownerPassword: 'owner-secret',
permissions: { copy: false, modify: false, print: false },
});
const protectedBytes = await doc.toBytes();
const opened = await VibeFoxJSPdf.loadPdf(protectedBytes);
const viewer = VibeFoxJSPdf.createPdfViewer('#app', { document: opened, zoom: 0.8 });
const e = opened.sourceInfo.encrypted;
show(e.method + ', allowed: ' + Object.entries(e.allowed).filter(([, v]) => v).map(([k]) => k).join(', '),
'not allowed: ' + Object.entries(e.allowed).filter(([, v]) => !v).map(([k]) => k).join(', ') + '; canPrint(): ' + viewer.canPrint());PDF/A, PDF/UA and tagged PDF
Structure
A tagged document: a heading, paragraphs, a list, a figure with alternative text and a table; the box shows the structure tree that screen readers use.
document.getElementById('app').style.height = '380px';
const output = document.createElement('pre');
output.style.cssText = 'height: 130px; overflow: auto; margin: 6px 0 0; padding: 4px 8px; border: 1px solid #d0d7de; font: 12px ui-monospace, Consolas, monospace; white-space: pre-wrap';
document.getElementById('app').after(output);
const show = (...lines) => { output.textContent = lines.join('\n'); };
const doc = new VibeFoxJSPdf.PdfDocument({ tagged: true, lang: 'en-US', metadata: { title: 'Annual report 2026' } });
doc.text('Annual report', { size: 20, tag: 'H1' });
doc.text('Revenue grew by 12 %.'); // a P element
doc.svg('<svg xmlns="http://www.w3.org/2000/svg" width="120" height="60"><rect width="120" height="60" rx="8" fill="#0f766e"/></svg>', { width: 80, alt: 'Company logo' });
doc.tag('L', () => {
doc.tag('LI', () => doc.text('First point'));
doc.tag('LI', () => doc.text('Second point'));
});
doc.table({ columns: [{ header: 'Region', field: 'r' }, { header: 'Sales', field: 's', align: 'right' }], rows: [{ r: 'North', s: 120 }, { r: 'South', s: 98 }] });
const tree = (node, depth = 0) => ' '.repeat(depth) + node.role + (node.alt ? ' (alt: ' + node.alt + ')' : '') + '\n' +
(node.children || []).map((child) => tree(child, depth + 1)).join('');
show(tree(doc.getStructure()));
VibeFoxJSPdf.createPdfViewer('#app', { document: doc, zoom: 0.8 });Checks and conversion
The built-in checks run on a plain document: the PDF/A-2b and PDF/UA errors with the clauses of the standards, then a conversion to PDF/A with what it fixed and what it could not fix (the standard fonts are not embedded; give a font file of yours for that).
const output = document.createElement('pre');
output.style.cssText = 'height: 240px; overflow: auto; margin: 6px 0 0; padding: 4px 8px; border: 1px solid #d0d7de; font: 12px ui-monospace, Consolas, monospace; white-space: pre-wrap';
document.getElementById('app').after(output);
const show = (...lines) => { output.textContent = lines.join('\n'); };
const doc = new VibeFoxJSPdf.PdfDocument({ metadata: { title: 'Price list' } });
doc.text('Price list', { size: 20, bold: true });
const pdfBytes = await doc.toBytes();
const list = (items) => items.map((e) => ' ' + e.rule + ' ' + e.message).join('\n');
const pdfaReport = await VibeFoxJSPdf.checkPdfA(pdfBytes, { part: 2, conformance: 'b' });
const uaReport = await VibeFoxJSPdf.checkPdfUA(pdfBytes);
const { report } = await VibeFoxJSPdf.convertToPdfA(pdfBytes, { pdfa: '2b' });
show('PDF/A-2b conforms: ' + pdfaReport.conforms, list(pdfaReport.errors),
'PDF/UA-1 conforms: ' + uaReport.conforms, list(uaReport.errors),
'convertToPdfA fixed:', list(report.fixed), 'could not fix:', list(report.failed));Templates and the designer
Templates
An invoice template with placeholders, expressions and filters, a QR code and an EAN-13 bar code, filled with data; the table sums its rows in the footer. The comment line is a fillable field.
document.getElementById('app').style.height = '500px';
const template = {
name: 'Invoice',
page: { format: 'A4', margins: { top: 40, right: 40, bottom: 40, left: 40 } },
pages: [{ elements: [
{ type: 'heading', x: 40, y: 40, width: 380, height: 30, text: 'Invoice {{number}}', fontSize: 22 },
{ type: 'text', x: 40, y: 80, width: 300, height: 40, text: '{{customer.name}}\n{{customer.city}}' },
{ type: 'barcode', format: 'qr', x: 465, y: 40, width: 90, height: 90, value: 'INV-{{number}}' },
{ type: 'table', x: 40, y: 150, width: 515, height: 200, data: 'items', columns: [
{ header: 'Item', value: '{{name}}', width: 3 },
{ header: 'Qty', value: '{{qty}}', align: 'right' },
{ header: 'Price', value: '{{price | number:2}}', align: 'right', footer: '{{items | sum:qty*price | number:2}}' },
] },
{ type: 'text', x: 40, y: 380, width: 300, height: 20, text: '{{paid ? "PAID" : "DUE"}} by {{due | date:dd.MM.yyyy}}', if: 'total > 0', bold: true },
{ type: 'barcode', format: 'ean13', x: 40, y: 420, width: 160, height: 60, value: '4006381333931' },
{ type: 'textfield', name: 'comment', x: 40, y: 500, width: 300, height: 24, tooltip: 'Comment' },
] }],
};
const data = { number: 'A-17', paid: false, total: 1250, due: '2026-11-30', customer: { name: 'Anna Berg', city: 'Oslo' },
items: [{ name: 'Widget', qty: 2, price: 450 }, { name: 'Gadget', qty: 1, price: 350 }] };
const invoice = await VibeFoxJSPdf.renderTemplate(template, data, { locale: 'en-US' });
VibeFoxJSPdf.createPdfViewer('#app', { document: invoice, forms: true, zoom: 0.75 });Designer
The template designer with the preview data: drag elements from the palette onto the page, select, move and resize them with snapping, edit their properties in the side panel, undo and redo.
document.getElementById('app').style.height = '620px';
const template = {
name: 'Invoice',
page: { format: 'A4' },
pages: [{ elements: [
{ type: 'heading', x: 40, y: 40, width: 380, height: 30, text: 'Invoice {{number}}', fontSize: 22 },
{ type: 'text', x: 40, y: 80, width: 300, height: 40, text: '{{customer.name}}\n{{customer.city}}' },
{ type: 'barcode', format: 'qr', x: 465, y: 40, width: 90, height: 90, value: 'INV-{{number}}' },
{ type: 'table', x: 40, y: 150, width: 515, height: 160, data: 'items', columns: [
{ header: 'Item', value: '{{name}}', width: 3 },
{ header: 'Qty', value: '{{qty}}', align: 'right' },
{ header: 'Price', value: '{{price | number:2}}', align: 'right' },
] },
] }],
};
const data = { number: 'A-17', customer: { name: 'Anna Berg', city: 'Oslo' },
items: [{ name: 'Widget', qty: 2, price: 450 }, { name: 'Gadget', qty: 1, price: 350 }] };
const designer = VibeFoxJSPdf.createPdfDesigner('#app', { template, data, preview: true, zoom: 'fit' });
const el = designer.addElement('text', { x: 40, y: 340, width: 300, height: 20, text: 'Thank you, {{customer.name}}!' });
designer.select(el.id);
designer.align('center', { to: 'page' });Theming and styling
Themes
Pick a theme from the list: the built-in light, dark and high-contrast, the theme pack of vibefox-js-themes.css, and an own theme "brand" registered with registerTheme.
function addSelect(label, values, onChange) {
const select = document.createElement('select');
select.setAttribute('aria-label', label);
for (const value of values) select.append(new Option(label + ': ' + value, value));
select.onchange = () => onChange(select.value);
document.getElementById('app').before(select);
return select;
}
document.getElementById('app').style.height = '420px';
const report = new VibeFoxJSPdf.PdfDocument({ metadata: { title: 'Annual report 2026' }, footer: { text: 'Page {page} of {pages}', align: 'right', size: 9 } });
const chapters = [
['Introduction', 'This report describes the year of a small company that makes garden furniture. Sales grew in every region, and the new online shop brought many first-time customers.'],
['Sales', 'Sales reached 4.2 million EUR, 12 % more than last year. Chairs and tables sold best; the invoice total of the largest order was 48 000 EUR.'],
['Customers', 'Most customers are families and small cafes. Write to [email protected] or call +47 22 33 44 55 for a catalog.'],
['Outlook', 'Next year the company plans a second workshop and a winter collection. The search below finds the word invoice on page 2.'],
];
chapters.forEach(([title, body], i) => {
if (i) report.addPage();
report.addBookmark(title, { page: i });
report.text((i + 1) + '. ' + title, { size: 22, bold: true, color: '#0f766e', marginBottom: 10 });
report.text(body + ' ' + body, { size: 12, align: 'justify', marginBottom: 14 });
const page = report.page(i);
[60, 95, 75, 120, 105].forEach((h, j) => page.drawRect(70 + j * 90, 420 - h, 60, h, { fill: ['#99f6e4', '#5eead4', '#2dd4bf', '#14b8a6', '#0d9488'][j] }));
page.drawLine(60, 420, 520, 420, { color: '#64748b', width: 1 });
});
VibeFoxJSPdf.registerTheme('brand', { accent: '#e11d48', 'accent-fg': '#ffffff', radius: '10px' });
const viewer = VibeFoxJSPdf.createPdfViewer('#app', { document: report, sidebar: 'thumbnails', zoom: 0.5 });
addSelect('Theme', ['light', 'dark', 'high-contrast', 'paper', 'glass', 'classic', 'ocean', 'ocean-dark', 'forest', 'sunset', 'graphite-dark', 'brand'],
(theme) => viewer.setTheme(theme));CSS variables
The viewer with a few --vfjs-pdf-* variables set on its element: a dark toolbar with dark buttons, a gray workspace, wider gaps between the pages without a shadow, and a teal accent.
document.getElementById('app').style.height = '420px';
const report = new VibeFoxJSPdf.PdfDocument({ metadata: { title: 'Annual report 2026' }, footer: { text: 'Page {page} of {pages}', align: 'right', size: 9 } });
const chapters = [
['Introduction', 'This report describes the year of a small company that makes garden furniture. Sales grew in every region, and the new online shop brought many first-time customers.'],
['Sales', 'Sales reached 4.2 million EUR, 12 % more than last year. Chairs and tables sold best; the invoice total of the largest order was 48 000 EUR.'],
['Customers', 'Most customers are families and small cafes. Write to [email protected] or call +47 22 33 44 55 for a catalog.'],
['Outlook', 'Next year the company plans a second workshop and a winter collection. The search below finds the word invoice on page 2.'],
];
chapters.forEach(([title, body], i) => {
if (i) report.addPage();
report.addBookmark(title, { page: i });
report.text((i + 1) + '. ' + title, { size: 22, bold: true, color: '#0f766e', marginBottom: 10 });
report.text(body + ' ' + body, { size: 12, align: 'justify', marginBottom: 14 });
const page = report.page(i);
[60, 95, 75, 120, 105].forEach((h, j) => page.drawRect(70 + j * 90, 420 - h, 60, h, { fill: ['#99f6e4', '#5eead4', '#2dd4bf', '#14b8a6', '#0d9488'][j] }));
page.drawLine(60, 420, 520, 420, { color: '#64748b', width: 1 });
});
const style = document.createElement('style');
style.textContent = '.vfjs { --vfjs-accent: #0f766e; --vfjs-radius: 4px; }' +
'#app { --vfjs-pdf-bg: #e2e8f0; --vfjs-pdf-page-gap: 24px; --vfjs-pdf-page-shadow: none;' +
' --vfjs-pdf-toolbar-bg: #0f172a; --vfjs-pdf-toolbar-fg: #e2e8f0; --vfjs-pdf-muted: #94a3b8;' +
' --vfjs-pdf-btn-bg: #1e293b; --vfjs-pdf-btn-fg: #e2e8f0; --vfjs-pdf-btn-border: #334155; --vfjs-pdf-btn-hover-bg: #334155; }';
document.head.append(style);
VibeFoxJSPdf.createPdfViewer('#app', { document: report, zoom: 0.5 });Ready-made styles
The ready-made looks of the viewer: pick classic, dark, minimal, compact or paper from the list.
function addSelect(label, values, onChange) {
const select = document.createElement('select');
select.setAttribute('aria-label', label);
for (const value of values) select.append(new Option(label + ': ' + value, value));
select.onchange = () => onChange(select.value);
document.getElementById('app').before(select);
return select;
}
document.getElementById('app').style.height = '420px';
const report = new VibeFoxJSPdf.PdfDocument({ metadata: { title: 'Annual report 2026' }, footer: { text: 'Page {page} of {pages}', align: 'right', size: 9 } });
const chapters = [
['Introduction', 'This report describes the year of a small company that makes garden furniture. Sales grew in every region, and the new online shop brought many first-time customers.'],
['Sales', 'Sales reached 4.2 million EUR, 12 % more than last year. Chairs and tables sold best; the invoice total of the largest order was 48 000 EUR.'],
['Customers', 'Most customers are families and small cafes. Write to [email protected] or call +47 22 33 44 55 for a catalog.'],
['Outlook', 'Next year the company plans a second workshop and a winter collection. The search below finds the word invoice on page 2.'],
];
chapters.forEach(([title, body], i) => {
if (i) report.addPage();
report.addBookmark(title, { page: i });
report.text((i + 1) + '. ' + title, { size: 22, bold: true, color: '#0f766e', marginBottom: 10 });
report.text(body + ' ' + body, { size: 12, align: 'justify', marginBottom: 14 });
const page = report.page(i);
[60, 95, 75, 120, 105].forEach((h, j) => page.drawRect(70 + j * 90, 420 - h, 60, h, { fill: ['#99f6e4', '#5eead4', '#2dd4bf', '#14b8a6', '#0d9488'][j] }));
page.drawLine(60, 420, 520, 420, { color: '#64748b', width: 1 });
});
const viewer = VibeFoxJSPdf.createPdfViewer('#app', { document: report, sidebar: 'thumbnails', zoom: 0.5, style: 'classic' });
addSelect('Style', VibeFoxJSPdf.PDF_VIEWER_STYLES, (style) => viewer.setStyle(style));Density and size
The density of the container changes the heights and paddings of the toolbar and the sidebar; pick compact, normal or comfortable.
function addSelect(label, values, onChange) {
const select = document.createElement('select');
select.setAttribute('aria-label', label);
for (const value of values) select.append(new Option(label + ': ' + value, value));
select.onchange = () => onChange(select.value);
document.getElementById('app').before(select);
return select;
}
document.getElementById('app').style.height = '420px';
const report = new VibeFoxJSPdf.PdfDocument({ metadata: { title: 'Annual report 2026' }, footer: { text: 'Page {page} of {pages}', align: 'right', size: 9 } });
const chapters = [
['Introduction', 'This report describes the year of a small company that makes garden furniture. Sales grew in every region, and the new online shop brought many first-time customers.'],
['Sales', 'Sales reached 4.2 million EUR, 12 % more than last year. Chairs and tables sold best; the invoice total of the largest order was 48 000 EUR.'],
['Customers', 'Most customers are families and small cafes. Write to [email protected] or call +47 22 33 44 55 for a catalog.'],
['Outlook', 'Next year the company plans a second workshop and a winter collection. The search below finds the word invoice on page 2.'],
];
chapters.forEach(([title, body], i) => {
if (i) report.addPage();
report.addBookmark(title, { page: i });
report.text((i + 1) + '. ' + title, { size: 22, bold: true, color: '#0f766e', marginBottom: 10 });
report.text(body + ' ' + body, { size: 12, align: 'justify', marginBottom: 14 });
const page = report.page(i);
[60, 95, 75, 120, 105].forEach((h, j) => page.drawRect(70 + j * 90, 420 - h, 60, h, { fill: ['#99f6e4', '#5eead4', '#2dd4bf', '#14b8a6', '#0d9488'][j] }));
page.drawLine(60, 420, 520, 420, { color: '#64748b', width: 1 });
});
const viewer = VibeFoxJSPdf.createPdfViewer('#app', { document: report, sidebar: 'thumbnails', zoom: 0.5 });
document.body.dataset.vfjsDensity = 'compact';
addSelect('Density', ['compact', 'normal', 'comfortable'], (density) => { document.body.dataset.vfjsDensity = density; });Localization and RTL
Languages
The viewer in Russian, with the language pack vibefox-js-pdf.ru.js loaded after the library; English is built in. Switch it with the list. The texts of the PDF itself do not change.
function addSelect(label, values, onChange) {
const select = document.createElement('select');
select.setAttribute('aria-label', label);
for (const value of values) select.append(new Option(label + ': ' + value, value));
select.onchange = () => onChange(select.value);
document.getElementById('app').before(select);
return select;
}
document.getElementById('app').style.height = '420px';
const report = new VibeFoxJSPdf.PdfDocument({ metadata: { title: 'Annual report 2026' }, footer: { text: 'Page {page} of {pages}', align: 'right', size: 9 } });
const chapters = [
['Introduction', 'This report describes the year of a small company that makes garden furniture. Sales grew in every region, and the new online shop brought many first-time customers.'],
['Sales', 'Sales reached 4.2 million EUR, 12 % more than last year. Chairs and tables sold best; the invoice total of the largest order was 48 000 EUR.'],
['Customers', 'Most customers are families and small cafes. Write to [email protected] or call +47 22 33 44 55 for a catalog.'],
['Outlook', 'Next year the company plans a second workshop and a winter collection. The search below finds the word invoice on page 2.'],
];
chapters.forEach(([title, body], i) => {
if (i) report.addPage();
report.addBookmark(title, { page: i });
report.text((i + 1) + '. ' + title, { size: 22, bold: true, color: '#0f766e', marginBottom: 10 });
report.text(body + ' ' + body, { size: 12, align: 'justify', marginBottom: 14 });
const page = report.page(i);
[60, 95, 75, 120, 105].forEach((h, j) => page.drawRect(70 + j * 90, 420 - h, 60, h, { fill: ['#99f6e4', '#5eead4', '#2dd4bf', '#14b8a6', '#0d9488'][j] }));
page.drawLine(60, 420, 520, 420, { color: '#64748b', width: 1 });
});
const viewer = VibeFoxJSPdf.createPdfViewer('#app', { document: report, sidebar: 'thumbnails', zoom: 0.5, locale: 'ru' });
addSelect('Language', ['ru', 'en'], (locale) => viewer.setOptions({ locale }));Adding a language
A partial German dictionary added with addLocale: the zoom and download buttons are German, the keys it does not have fall back to English. Hover the buttons to see the tooltips.
document.getElementById('app').style.height = '420px';
const report = new VibeFoxJSPdf.PdfDocument({ metadata: { title: 'Annual report 2026' }, footer: { text: 'Page {page} of {pages}', align: 'right', size: 9 } });
const chapters = [
['Introduction', 'This report describes the year of a small company that makes garden furniture. Sales grew in every region, and the new online shop brought many first-time customers.'],
['Sales', 'Sales reached 4.2 million EUR, 12 % more than last year. Chairs and tables sold best; the invoice total of the largest order was 48 000 EUR.'],
['Customers', 'Most customers are families and small cafes. Write to [email protected] or call +47 22 33 44 55 for a catalog.'],
['Outlook', 'Next year the company plans a second workshop and a winter collection. The search below finds the word invoice on page 2.'],
];
chapters.forEach(([title, body], i) => {
if (i) report.addPage();
report.addBookmark(title, { page: i });
report.text((i + 1) + '. ' + title, { size: 22, bold: true, color: '#0f766e', marginBottom: 10 });
report.text(body + ' ' + body, { size: 12, align: 'justify', marginBottom: 14 });
const page = report.page(i);
[60, 95, 75, 120, 105].forEach((h, j) => page.drawRect(70 + j * 90, 420 - h, 60, h, { fill: ['#99f6e4', '#5eead4', '#2dd4bf', '#14b8a6', '#0d9488'][j] }));
page.drawLine(60, 420, 520, 420, { color: '#64748b', width: 1 });
});
VibeFoxJSPdf.addLocale('de', {
pdfZoomIn: 'Vergrößern',
pdfZoomOut: 'Verkleinern',
pdfDownload: 'PDF herunterladen',
});
VibeFoxJSPdf.createPdfViewer('#app', { document: report, zoom: 0.5, locale: 'de' });Right-to-left
The viewer laid out right-to-left: the toolbar and the sidebar are mirrored and the previous / next buttons point the reading direction; the pages keep their geometry.
document.getElementById('app').style.height = '420px';
const report = new VibeFoxJSPdf.PdfDocument({ metadata: { title: 'Annual report 2026' }, footer: { text: 'Page {page} of {pages}', align: 'right', size: 9 } });
const chapters = [
['Introduction', 'This report describes the year of a small company that makes garden furniture. Sales grew in every region, and the new online shop brought many first-time customers.'],
['Sales', 'Sales reached 4.2 million EUR, 12 % more than last year. Chairs and tables sold best; the invoice total of the largest order was 48 000 EUR.'],
['Customers', 'Most customers are families and small cafes. Write to [email protected] or call +47 22 33 44 55 for a catalog.'],
['Outlook', 'Next year the company plans a second workshop and a winter collection. The search below finds the word invoice on page 2.'],
];
chapters.forEach(([title, body], i) => {
if (i) report.addPage();
report.addBookmark(title, { page: i });
report.text((i + 1) + '. ' + title, { size: 22, bold: true, color: '#0f766e', marginBottom: 10 });
report.text(body + ' ' + body, { size: 12, align: 'justify', marginBottom: 14 });
const page = report.page(i);
[60, 95, 75, 120, 105].forEach((h, j) => page.drawRect(70 + j * 90, 420 - h, 60, h, { fill: ['#99f6e4', '#5eead4', '#2dd4bf', '#14b8a6', '#0d9488'][j] }));
page.drawLine(60, 420, 520, 420, { color: '#64748b', width: 1 });
});
VibeFoxJSPdf.createPdfViewer('#app', { document: report, sidebar: 'thumbnails', zoom: 0.5, rtl: true });Using with other VibeFox JS libraries
Connecting components with link()
Two viewers connected with link(): turn the pages of the left one and the right one follows.
const report = new VibeFoxJSPdf.PdfDocument({ metadata: { title: 'Annual report 2026' }, footer: { text: 'Page {page} of {pages}', align: 'right', size: 9 } });
const chapters = [
['Introduction', 'This report describes the year of a small company that makes garden furniture. Sales grew in every region, and the new online shop brought many first-time customers.'],
['Sales', 'Sales reached 4.2 million EUR, 12 % more than last year. Chairs and tables sold best; the invoice total of the largest order was 48 000 EUR.'],
['Customers', 'Most customers are families and small cafes. Write to [email protected] or call +47 22 33 44 55 for a catalog.'],
['Outlook', 'Next year the company plans a second workshop and a winter collection. The search below finds the word invoice on page 2.'],
];
chapters.forEach(([title, body], i) => {
if (i) report.addPage();
report.addBookmark(title, { page: i });
report.text((i + 1) + '. ' + title, { size: 22, bold: true, color: '#0f766e', marginBottom: 10 });
report.text(body + ' ' + body, { size: 12, align: 'justify', marginBottom: 14 });
const page = report.page(i);
[60, 95, 75, 120, 105].forEach((h, j) => page.drawRect(70 + j * 90, 420 - h, 60, h, { fill: ['#99f6e4', '#5eead4', '#2dd4bf', '#14b8a6', '#0d9488'][j] }));
page.drawLine(60, 420, 520, 420, { color: '#64748b', width: 1 });
});
const app = document.getElementById('app');
app.style.cssText = 'display: grid; grid-template-columns: 1fr 1fr; gap: 8px; height: 400px';
const left = document.createElement('div');
const right = document.createElement('div');
app.append(left, right);
const viewer = VibeFoxJSPdf.createPdfViewer(left, { document: report, zoom: 0.4, layout: 'single' });
const second = VibeFoxJSPdf.createPdfViewer(right, { document: report, zoom: 0.4, layout: 'single', toolbar: false });
const unlink = VibeFoxJSPdf.link(viewer, 'pageChange', second, 'goToPage', (e) => e.page);