Compare commits

..

2 Commits
v4.1.3 ... main

Author SHA1 Message Date
5b1615d359 v4.2.0
Some checks failed
Default (tags) / security (push) Failing after 0s
Default (tags) / test (push) Failing after 0s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2026-03-09 17:05:19 +00:00
c1208b5216 feat(smartpdf): replace internal Express server with @push.rocks/smartserve, add PDF→WebP rendering, improve start/stop handling and bump dependencies 2026-03-09 17:05:19 +00:00
7 changed files with 701 additions and 1752 deletions

View File

@@ -1,5 +1,15 @@
# Changelog # Changelog
## 2026-03-09 - 4.2.0 - feat(smartpdf)
replace internal Express server with @push.rocks/smartserve, add PDF→WebP rendering, improve start/stop handling and bump dependencies
- Replace internal Express HTTP implementation with @push.rocks/smartserve and update README wording to reflect HTTP server usage
- Add PDF→WebP rendering: use pdf.js in-page rendering, capture canvases via Puppeteer to produce WebP buffers; added robust wait/timeout and error handling
- Add start/stop guards: _isRunning flag, reset readiness Deferred on start, and throw if start called while running
- Remove direct http/express exports from plugins and stop exporting express; export smartserve from plugins
- Improve JPEG conversion to produce progressive JPEGs via SmartJimp (sharp mode)
- Bump dependencies/devDependencies: @push.rocks/smartfs to ^1.5.0, add @push.rocks/smartserve ^2.0.1; devDeps @git.zone/tsbuild ^4.3.0, @git.zone/tstest ^3.3.0, @types/node ^25.3.5
## 2026-03-01 - 4.1.3 - fix(tests) ## 2026-03-01 - 4.1.3 - fix(tests)
use example.com in image conversion test and relax JPEG size assertion use example.com in image conversion test and relax JPEG size assertion

View File

@@ -1,6 +1,6 @@
{ {
"name": "@push.rocks/smartpdf", "name": "@push.rocks/smartpdf",
"version": "4.1.3", "version": "4.2.0",
"private": false, "private": false,
"description": "A library for creating PDFs dynamically from HTML or websites with additional features like merging PDFs.", "description": "A library for creating PDFs dynamically from HTML or websites with additional features like merging PDFs.",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",
@@ -14,25 +14,24 @@
"buildDocs": "tsdoc" "buildDocs": "tsdoc"
}, },
"devDependencies": { "devDependencies": {
"@git.zone/tsbuild": "^4.1.2", "@git.zone/tsbuild": "^4.3.0",
"@git.zone/tsdoc": "^1.12.0", "@git.zone/tsdoc": "^1.12.0",
"@git.zone/tsrun": "^2.0.1", "@git.zone/tsrun": "^2.0.1",
"@git.zone/tstest": "^3.1.8", "@git.zone/tstest": "^3.3.0",
"@types/node": "^25.3.2" "@types/node": "^25.3.5"
}, },
"dependencies": { "dependencies": {
"@push.rocks/smartbuffer": "^3.0.5", "@push.rocks/smartbuffer": "^3.0.5",
"@push.rocks/smartdelay": "^3.0.5", "@push.rocks/smartdelay": "^3.0.5",
"@push.rocks/smartfs": "^1.3.1", "@push.rocks/smartfs": "^1.5.0",
"@push.rocks/smartjimp": "^1.2.0", "@push.rocks/smartjimp": "^1.2.0",
"@push.rocks/smartnetwork": "^4.4.0", "@push.rocks/smartnetwork": "^4.4.0",
"@push.rocks/smartpath": "^6.0.0", "@push.rocks/smartpath": "^6.0.0",
"@push.rocks/smartpromise": "^4.2.3", "@push.rocks/smartpromise": "^4.2.3",
"@push.rocks/smartpuppeteer": "^2.0.5", "@push.rocks/smartpuppeteer": "^2.0.5",
"@push.rocks/smartserve": "^2.0.1",
"@push.rocks/smartunique": "^3.0.9", "@push.rocks/smartunique": "^3.0.9",
"@tsclass/tsclass": "^9.3.0", "@tsclass/tsclass": "^9.3.0",
"@types/express": "^5.0.6",
"express": "^5.2.1",
"pdf-lib": "^1.17.1", "pdf-lib": "^1.17.1",
"pdf2json": "^4.0.2" "pdf2json": "^4.0.2"
}, },

1760
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@@ -73,7 +73,7 @@ interface IPdf {
## 📚 How It Works ## 📚 How It Works
SmartPDF spins up a lightweight Express server bound to `localhost` and a headless Chromium browser. When you call a generation method: SmartPDF spins up a lightweight HTTP server (via `@push.rocks/smartserve`) bound to `localhost` and a headless Chromium browser. When you call a generation method:
1. Your HTML is registered internally and served at `http://localhost:{port}/{id}` 1. Your HTML is registered internally and served at `http://localhost:{port}/{id}`
2. Puppeteer navigates to that URL, waits for the page to fully render, and captures a PDF 2. Puppeteer navigates to that URL, waits for the page to fully render, and captures a PDF
@@ -362,7 +362,7 @@ await Promise.all(instances.map(i => i.stop()));
| Property | Type | Description | | Property | Type | Description |
|----------|------|-------------| |----------|------|-------------|
| `serverPort` | `number` | The port the internal Express server is listening on | | `serverPort` | `number` | The port the internal HTTP server is listening on |
#### Instance Methods #### Instance Methods

View File

@@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/smartpdf', name: '@push.rocks/smartpdf',
version: '4.1.3', version: '4.2.0',
description: 'A library for creating PDFs dynamically from HTML or websites with additional features like merging PDFs.' description: 'A library for creating PDFs dynamically from HTML or websites with additional features like merging PDFs.'
} }

View File

@@ -1,6 +1,5 @@
import * as plugins from './smartpdf.plugins.js'; import * as plugins from './smartpdf.plugins.js';
import * as paths from './smartpdf.paths.js'; import * as paths from './smartpdf.paths.js';
import { Server } from 'http';
import { PdfCandidate } from './smartpdf.classes.pdfcandidate.js'; import { PdfCandidate } from './smartpdf.classes.pdfcandidate.js';
import { type IPdf } from '@tsclass/tsclass/dist_ts/business/pdf.js'; import { type IPdf } from '@tsclass/tsclass/dist_ts/business/pdf.js';
declare const document: any; declare const document: any;
@@ -32,13 +31,14 @@ export class SmartPdf {
} }
// INSTANCE // INSTANCE
htmlServerInstance: Server; private smartserveInstance: plugins.smartserve.SmartServe;
serverPort: number; serverPort: number;
headlessBrowser: plugins.smartpuppeteer.puppeteer.Browser; headlessBrowser: plugins.smartpuppeteer.puppeteer.Browser;
externalBrowserBool: boolean = false; externalBrowserBool: boolean = false;
private _readyDeferred: plugins.smartpromise.Deferred<void>; private _readyDeferred: plugins.smartpromise.Deferred<void>;
private _candidates: { [key: string]: PdfCandidate } = {}; private _candidates: { [key: string]: PdfCandidate } = {};
private _options: ISmartPdfOptions; private _options: ISmartPdfOptions;
private _isRunning: boolean = false;
constructor(optionsArg?: ISmartPdfOptions) { constructor(optionsArg?: ISmartPdfOptions) {
this._readyDeferred = new plugins.smartpromise.Deferred(); this._readyDeferred = new plugins.smartpromise.Deferred();
@@ -50,7 +50,13 @@ export class SmartPdf {
} }
async start(headlessBrowserArg?: plugins.smartpuppeteer.puppeteer.Browser) { async start(headlessBrowserArg?: plugins.smartpuppeteer.puppeteer.Browser) {
const done = plugins.smartpromise.defer(); if (this._isRunning) {
throw new Error('SmartPdf is already running. Call stop() before starting again.');
}
// Reset readiness deferred for this start cycle
this._readyDeferred = new plugins.smartpromise.Deferred();
// lets set the external browser in case one is provided // lets set the external browser in case one is provided
this.headlessBrowser = headlessBrowserArg; this.headlessBrowser = headlessBrowserArg;
// setup puppeteer // setup puppeteer
@@ -74,6 +80,7 @@ export class SmartPdf {
// Clean up browser if we created one // Clean up browser if we created one
if (!this.externalBrowserBool && this.headlessBrowser) { if (!this.externalBrowserBool && this.headlessBrowser) {
await this.headlessBrowser.close(); await this.headlessBrowser.close();
this.headlessBrowser = null;
} }
throw new Error(`Requested port ${this._options.port} is already in use`); throw new Error(`Requested port ${this._options.port} is already in use`);
} }
@@ -87,45 +94,62 @@ export class SmartPdf {
// Clean up browser if we created one // Clean up browser if we created one
if (!this.externalBrowserBool && this.headlessBrowser) { if (!this.externalBrowserBool && this.headlessBrowser) {
await this.headlessBrowser.close(); await this.headlessBrowser.close();
this.headlessBrowser = null;
} }
throw new Error(`No free ports available in range ${this._options.portRangeStart}-${this._options.portRangeEnd}`); throw new Error(`No free ports available in range ${this._options.portRangeStart}-${this._options.portRangeEnd}`);
} }
} }
// Now setup server after we know we have a valid port // Now setup server using smartserve
const app = plugins.express(); this.smartserveInstance = new plugins.smartserve.SmartServe({
app.get('/:pdfId', (req, res) => { port: this.serverPort,
const wantedCandidate = this._candidates[req.params.pdfId]; hostname: 'localhost',
if (!wantedCandidate) {
console.log(`${req.url} not attached to a candidate`);
return;
}
res.setHeader('pdf-id', wantedCandidate.pdfId);
res.send(wantedCandidate.htmlString);
}); });
this.htmlServerInstance = plugins.http.createServer(app);
this.htmlServerInstance.listen(this.serverPort, 'localhost'); this.smartserveInstance.setHandler(async (request) => {
this.htmlServerInstance.on('listening', () => { const url = new URL(request.url);
console.log(`SmartPdf server listening on port ${this.serverPort}`); const pdfId = url.pathname.slice(1); // Remove leading /
this._readyDeferred.resolve(); const candidate = this._candidates[pdfId];
done.resolve(); if (!candidate) {
console.log(`${url.pathname} not attached to a candidate`);
return new Response('Not found', { status: 404 });
}
return new Response(candidate.htmlString, {
headers: {
'Content-Type': 'text/html; charset=utf-8',
'pdf-id': candidate.pdfId,
},
}); });
await done.promise; });
await this.smartserveInstance.start();
console.log(`SmartPdf server listening on port ${this.serverPort}`);
this._isRunning = true;
this._readyDeferred.resolve();
} }
// stop // stop
async stop() { async stop() {
const done = plugins.smartpromise.defer<void>(); if (!this._isRunning) {
this.htmlServerInstance.close(() => { return;
done.resolve();
});
if (!this.externalBrowserBool) {
await this.headlessBrowser.close();
} }
await done.promise; this._isRunning = false;
// Close browser first to cleanly terminate keepalive connections
// before the server shuts down (prevents ECONNRESET errors)
if (!this.externalBrowserBool && this.headlessBrowser) {
await this.headlessBrowser.close();
}
this.headlessBrowser = null;
if (this.smartserveInstance) {
await this.smartserveInstance.stop();
this.smartserveInstance = null;
}
// Clear any remaining candidates
this._candidates = {};
} }
/** /**
@@ -135,7 +159,9 @@ export class SmartPdf {
await this._readyDeferred.promise; await this._readyDeferred.promise;
const pdfCandidate = new PdfCandidate(htmlStringArg); const pdfCandidate = new PdfCandidate(htmlStringArg);
this._candidates[pdfCandidate.pdfId] = pdfCandidate; this._candidates[pdfCandidate.pdfId] = pdfCandidate;
const page = await this.headlessBrowser.newPage(); let page: plugins.smartpuppeteer.puppeteer.Page;
try {
page = await this.headlessBrowser.newPage();
await page.setViewport({ await page.setViewport({
width: 794, width: 794,
height: 1122, height: 1122,
@@ -171,10 +197,19 @@ export class SmartPdf {
}, },
buffer: nodePdfBuffer, buffer: nodePdfBuffer,
}; };
} catch (err) {
// Clean up candidate on error
delete this._candidates[pdfCandidate.pdfId];
if (page) {
await page.close().catch(() => {});
}
throw err;
}
} }
async getPdfResultForWebsite(websiteUrl: string): Promise<plugins.tsclass.business.IPdf> { async getPdfResultForWebsite(websiteUrl: string): Promise<plugins.tsclass.business.IPdf> {
const page = await this.headlessBrowser.newPage(); const page = await this.headlessBrowser.newPage();
try {
await page.setViewport({ await page.setViewport({
width: 1980, width: 1980,
height: 1200, height: 1200,
@@ -205,10 +240,15 @@ export class SmartPdf {
}, },
buffer: nodePdfBuffer, buffer: nodePdfBuffer,
}; };
} catch (err) {
await page.close().catch(() => {});
throw err;
}
} }
async getFullWebsiteAsSinglePdf(websiteUrl: string): Promise<plugins.tsclass.business.IPdf> { async getFullWebsiteAsSinglePdf(websiteUrl: string): Promise<plugins.tsclass.business.IPdf> {
const page = await this.headlessBrowser.newPage(); const page = await this.headlessBrowser.newPage();
try {
await page.setViewport({ await page.setViewport({
width: 1920, width: 1920,
height: 1200, height: 1200,
@@ -253,6 +293,10 @@ export class SmartPdf {
}, },
buffer: nodePdfBuffer, buffer: nodePdfBuffer,
}; };
} catch (err) {
await page.close().catch(() => {});
throw err;
}
} }
public async mergePdfs(inputPdfBuffers: Uint8Array[]): Promise<Uint8Array> { public async mergePdfs(inputPdfBuffers: Uint8Array[]): Promise<Uint8Array> {
@@ -318,6 +362,7 @@ export class SmartPdf {
// Create a new page using the headless browser. // Create a new page using the headless browser.
const page = await this.headlessBrowser.newPage(); const page = await this.headlessBrowser.newPage();
try {
// Prepare PDF data as a base64 string. // Prepare PDF data as a base64 string.
const base64Pdf: string = Buffer.from(pdfBytes).toString('base64'); const base64Pdf: string = Buffer.from(pdfBytes).toString('base64');
@@ -401,6 +446,10 @@ export class SmartPdf {
await page.close(); await page.close();
return pngBuffers; return pngBuffers;
} catch (err) {
await page.close().catch(() => {});
throw err;
}
} }
/** /**
@@ -424,6 +473,7 @@ export class SmartPdf {
// Create a new page using the headless browser // Create a new page using the headless browser
const page = await this.headlessBrowser.newPage(); const page = await this.headlessBrowser.newPage();
try {
// Prepare PDF data as a base64 string // Prepare PDF data as a base64 string
const base64Pdf: string = Buffer.from(pdfBytes).toString('base64'); const base64Pdf: string = Buffer.from(pdfBytes).toString('base64');
@@ -512,6 +562,10 @@ export class SmartPdf {
await page.close(); await page.close();
return webpBuffers; return webpBuffers;
} catch (err) {
await page.close().catch(() => {});
throw err;
}
} }
/** /**
@@ -550,8 +604,6 @@ export class SmartPdf {
{ {
format: 'jpeg', format: 'jpeg',
progressive: true, progressive: true,
// SmartJimp uses a different quality scale, need to check if adjustment is needed
// For now, pass through the quality value
quality quality
} }
); );

View File

@@ -1,8 +1,7 @@
// native // native
import * as http from 'http';
import * as path from 'path'; import * as path from 'path';
export { http, path }; export { path };
// @pushrocks // @pushrocks
import * as smartbuffer from '@push.rocks/smartbuffer'; import * as smartbuffer from '@push.rocks/smartbuffer';
@@ -12,6 +11,7 @@ import * as smartpromise from '@push.rocks/smartpromise';
import * as smartpath from '@push.rocks/smartpath'; import * as smartpath from '@push.rocks/smartpath';
import * as smartpuppeteer from '@push.rocks/smartpuppeteer'; import * as smartpuppeteer from '@push.rocks/smartpuppeteer';
import * as smartnetwork from '@push.rocks/smartnetwork'; import * as smartnetwork from '@push.rocks/smartnetwork';
import * as smartserve from '@push.rocks/smartserve';
import * as smartunique from '@push.rocks/smartunique'; import * as smartunique from '@push.rocks/smartunique';
import * as smartjimp from '@push.rocks/smartjimp'; import * as smartjimp from '@push.rocks/smartjimp';
@@ -24,6 +24,7 @@ export {
smartpuppeteer, smartpuppeteer,
smartunique, smartunique,
smartnetwork, smartnetwork,
smartserve,
smartjimp, smartjimp,
}; };
@@ -33,8 +34,7 @@ import * as tsclass from '@tsclass/tsclass';
export { tsclass }; export { tsclass };
// thirdparty // thirdparty
import express from 'express';
import pdf2json from 'pdf2json'; import pdf2json from 'pdf2json';
import pdfLib from 'pdf-lib'; import pdfLib from 'pdf-lib';
export { express, pdf2json, pdfLib, }; export { pdf2json, pdfLib };