New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

simple-pdf-generator

Package Overview
Dependencies
Maintainers
2
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simple-pdf-generator - npm Package Compare versions

Comparing version 2.1.1 to 3.0.0

.prettierrc.json

1

lib/index.d.ts
export * from './PdfFiller';
export * from './PdfGenerator';
export * from './Server';
export * from './utils';

@@ -19,3 +19,2 @@ "use strict";

__exportStar(require("./PdfGenerator"), exports);
__exportStar(require("./Server"), exports);
__exportStar(require("./utils"), exports);
/// <reference types="node" />
import { Asset } from './PdfGenerator';
import puppeteer from 'puppeteer';
interface PdfFieldOptions {
fieldName?: string;
}
export declare function PdfField(options?: PdfFieldOptions): (target: any, propertyKey: string) => void;
interface PdfTableOptions {
tableName?: string;
}
export declare function PdfTable(options?: PdfTableOptions): (target: any, propertyKey: string) => void;
interface PdfTemplateOptions {
import { PDFOptions } from 'puppeteer';
export declare function PdfField(): (target: object, propertyKey: string) => void;
export declare function PdfTable(): (target: object, propertyKey: string) => void;
type PdfTemplateOptions = {
templatePath: string;
pdfOptions?: puppeteer.PDFOptions;
pdfOptions?: PDFOptions;
includes?: Asset[];
}
export declare function PdfTemplate(options: PdfTemplateOptions): (target: any) => void;
};
export declare function PdfTemplate(options: PdfTemplateOptions): (target: object) => void;
export declare abstract class PdfFiller {
[key: string]: unknown;
private static searchRegex;
fill(): Promise<Buffer>;
fill(pdfOptions: puppeteer.PDFOptions): Promise<Buffer>;
fill(outputPath: string): Promise<Buffer>;
fill(outputPath: string, pdfOptions: puppeteer.PDFOptions): Promise<Buffer>;
fill(outputPath?: string): Promise<Buffer>;
fill(outputPath?: string, pdfOptions?: PDFOptions): Promise<Buffer>;
}
export {};

@@ -11,5 +11,7 @@ "use strict";

const utils_1 = require("./utils");
function PdfField(options) {
const mime_1 = __importDefault(require("mime"));
const validator_1 = __importDefault(require("validator"));
function PdfField() {
return function (target, propertyKey) {
PdfFieldClass.registerDecorator(target, propertyKey, options);
PdfFieldClass.registerDecorator(target, propertyKey);
};

@@ -19,9 +21,11 @@ }

class PdfFieldClass {
static registerDecorator(target, property, options) {
static registerDecorator(target, property) {
let keys = this.decoratorsMap.get(target);
if (!keys) {
keys = [];
keys = new Map();
this.decoratorsMap.set(target, keys);
}
keys.push({ propertyName: property, constructor: target.constructor, fieldOptions: options });
keys.set(property, {
propertyName: property,
});
}

@@ -34,5 +38,5 @@ static getDecorators(target) {

PdfFieldClass.decoratorsMap = new Map();
function PdfTable(options) {
function PdfTable() {
return function (target, propertyKey) {
PdfTableClass.registerDecorator(target, propertyKey, options);
PdfTableClass.registerDecorator(target, propertyKey);
};

@@ -42,3 +46,3 @@ }

class PdfTableClass {
static registerDecorator(target, property, options) {
static registerDecorator(target, property) {
let keys = this.decoratorsMap.get(target);

@@ -49,3 +53,5 @@ if (!keys) {

}
keys.push({ propertyName: property, constructor: target.constructor, fieldOptions: options });
keys.push({
propertyName: property,
});
}

@@ -66,3 +72,6 @@ static getDecorators(target) {

static registerDecorator(target, options) {
this.decoratorMap.set(target, { className: target.constructor.name, options: options });
this.decoratorMap.set(target, {
className: target.constructor.name,
options: options,
});
}

@@ -75,66 +84,80 @@ static getDecorators(target) {

class PdfFiller {
async fill(args, pdfOptions) {
var _a;
let outputPath = null;
let __pdfOptions = null;
if (typeof (args) == 'string') {
outputPath = args;
}
else {
__pdfOptions = args;
}
if (pdfOptions != null) {
__pdfOptions = pdfOptions;
}
async fill(outputPath, pdfOptions) {
var _a, _b, _c;
const fieldDecorators = PdfFieldClass.getDecorators(this);
const tableDecorators = PdfTableClass.getDecorators(this);
const classDecorators = PdfTemplateClass.getDecorators(this);
if (classDecorators != null && fieldDecorators != null) {
let template = (await fs_1.default.promises.readFile(classDecorators.options.templatePath)).toString();
fieldDecorators.forEach(property => {
var _a, _b;
template = template.replace(new RegExp(`%%${(_b = (_a = property.fieldOptions) === null || _a === void 0 ? void 0 : _a.fieldName) !== null && _b !== void 0 ? _b : property.propertyName}%%`, 'g'), Reflect.get(this, property.propertyName));
});
const includes = new Array();
includes.push({ path: path_1.default.join(__dirname, '..', 'template', 'css', 'bootstrap.min.css') });
includes.push({ path: path_1.default.join(__dirname, '..', 'template', 'js', 'jquery.min.js') });
includes.push({ path: path_1.default.join(__dirname, '..', 'template', 'js', 'bootstrap.min.js') });
(_a = classDecorators === null || classDecorators === void 0 ? void 0 : classDecorators.options.includes) === null || _a === void 0 ? void 0 : _a.forEach(element => {
includes.push(element);
});
if (tableDecorators != null) {
const tableData = new Object();
tableDecorators.forEach(element => {
var _a, _b;
Object.defineProperty(tableData, (_b = (_a = element.fieldOptions) === null || _a === void 0 ? void 0 : _a.tableName) !== null && _b !== void 0 ? _b : element.propertyName, {
value: Reflect.get(this, element.propertyName),
enumerable: true,
});
});
let script = (await fs_1.default.promises.readFile(path_1.default.join(__dirname, '..', 'template', 'js', 'table-generator.js'))).toString();
script = script.replace('tablesData', `tablesData = ${(0, utils_1.stringify)(tableData)}`);
includes.push({ content: script, type: 'js' });
if (classDecorators == null) {
throw new Error('Missing mandatory decorators');
}
let template = (await fs_1.default.promises.readFile(classDecorators.options.templatePath)).toString();
const includes = new Array();
for (const include of (_a = classDecorators.options.includes) !== null && _a !== void 0 ? _a : []) {
includes.push(include);
}
const matches = template.matchAll(PdfFiller.searchRegex);
for (const match of matches) {
if (((_b = match.groups) === null || _b === void 0 ? void 0 : _b['propName']) != null) {
if (fieldDecorators != null) {
const decorator = fieldDecorators.get(match.groups.propName);
if (decorator != null) {
const value = Reflect.get(this, decorator.propertyName);
if (value != null) {
template = template.replace(match[0], validator_1.default.escape(value.toString()));
}
}
}
}
let _pdfOptions = {};
if (classDecorators.options.pdfOptions != null) {
_pdfOptions = Object.assign(_pdfOptions, classDecorators.options.pdfOptions);
else if (((_c = match.groups) === null || _c === void 0 ? void 0 : _c['imgSrc']) != null) {
const mimeType = mime_1.default.getType(match.groups.imgSrc);
if (mimeType != null) {
try {
let imgSrc = match.groups.imgSrc;
if (!path_1.default.isAbsolute(match.groups.imgSrc)) {
imgSrc = path_1.default.join(path_1.default.dirname(classDecorators.options.templatePath), match.groups.imgSrc);
}
const data = await fs_1.default.promises.readFile(imgSrc);
const base64 = data.toString('base64');
const newSrc = `data:${mimeType};base64,${base64}`;
template = template.replace(match[0], match[0].replace(match.groups.imgSrc, newSrc));
}
catch (e) {
console.error(e);
}
}
}
if (__pdfOptions != null) {
_pdfOptions = Object.assign(_pdfOptions, __pdfOptions);
}
if (tableDecorators != null) {
const tableData = new Object();
for (const decorator of tableDecorators) {
Object.defineProperty(tableData, decorator.propertyName, {
value: Reflect.get(this, decorator.propertyName),
enumerable: true,
});
}
const pdf = await PdfGenerator_1.PdfGenerator.getPdf({
options: {
template: template,
includes: includes,
},
pdfOptions: _pdfOptions,
});
if (outputPath != null) {
await fs_1.default.promises.writeFile(outputPath, pdf);
}
return pdf;
let script = PdfGenerator_1.PdfGenerator.tableGeneratorScript;
script = script.replace('tablesData', `tablesData = ${(0, utils_1.stringify)(tableData)}`);
includes.push({ content: script, type: 'js' });
}
throw new Error('Missing mandatory decorators');
let _pdfOptions = {};
if (classDecorators.options.pdfOptions != null) {
_pdfOptions = Object.assign(_pdfOptions, classDecorators.options.pdfOptions);
}
if (pdfOptions != null) {
_pdfOptions = Object.assign(_pdfOptions, pdfOptions);
}
const pdf = await PdfGenerator_1.PdfGenerator.getPdf({
options: {
template: template,
includes: includes,
},
pdfOptions: _pdfOptions,
});
if (outputPath != null) {
await fs_1.default.promises.writeFile(outputPath, pdf);
}
return pdf;
}
}
exports.PdfFiller = PdfFiller;
PdfFiller.searchRegex = new RegExp('(?:%%(?<propName>.*)%%)|(?:<img[^>]*src="(?<imgSrc>.*?)"[^>]*>)', 'g');
/// <reference types="node" />
import puppeteer from 'puppeteer';
import { PDFOptions } from 'puppeteer';
export interface Asset {

@@ -14,3 +14,3 @@ path?: string;

export interface PdfGeneratorOptions {
pdfOptions?: puppeteer.PDFOptions;
pdfOptions?: PDFOptions;
options?: PdfGeneratorTemplateOptions;

@@ -20,13 +20,8 @@ }

private static _browser;
private static _server;
private static _browserDisconnected;
static get staticFilePath(): string | undefined;
static set staticFilePath(val: string | undefined);
static start(): Promise<void>;
static stop(): Promise<void>;
private static _tableGeneratorScript;
static get tableGeneratorScript(): string;
private static _startBrowser;
private static _startServer;
static getPdf(options: PdfGeneratorOptions): Promise<Buffer>;
private static includeAssets;
private static _readContentOrFile;
private static _includeAssets;
}

@@ -7,34 +7,22 @@ "use strict";

exports.PdfGenerator = void 0;
const Server_1 = require("./Server");
const puppeteer_1 = __importDefault(require("puppeteer"));
const lodash_1 = __importDefault(require("lodash"));
const fs_1 = __importDefault(require("fs"));
const utils_1 = require("./utils");
const path_1 = __importDefault(require("path"));
class PdfGenerator {
static get staticFilePath() {
return this._server.staticFolderPath;
static get tableGeneratorScript() {
if (this._tableGeneratorScript === '') {
this._tableGeneratorScript = fs_1.default
.readFileSync(path_1.default.join(__dirname, '..', 'template', 'js', 'table-generator.js'))
.toString();
}
return this._tableGeneratorScript;
}
static set staticFilePath(val) {
this._server.staticFolderPath = val;
}
static async start() {
await this._startServer();
await this._startBrowser();
}
static async stop() {
var _a;
await ((_a = this._browser) === null || _a === void 0 ? void 0 : _a.close());
this._server.close();
}
static async _startBrowser() {
const browser_args = ['--proxy-server=\'direct://\'', '--proxy-bypass-list=*'];
const browser_args = ["--proxy-server='direct://'", '--proxy-bypass-list=*', '--disable-web-security'];
if (process.env.PUPPETEER_NO_SANDBOX === 'true') {
browser_args.push(...['--no-sandbox', '--disable-setuid-sandbox']);
}
let headless = true;
if (process.env.PUPPETEER_NO_HEADLESS === 'true') {
headless = false;
}
this._browser = await puppeteer_1.default.launch({
headless: headless,
headless: process.env.PUPPETEER_NO_HEADLESS === 'true' ? false : 'new',
args: browser_args,

@@ -51,9 +39,6 @@ defaultViewport: null,

}
static async _startServer() {
this._server = new Server_1.Server();
}
static async getPdf(options) {
var _a, _b, _c;
if (this._browserDisconnected) {
await new Promise(f => setTimeout(f, 10000));
await new Promise((f) => setTimeout(f, 10000));
await this._startBrowser();

@@ -64,33 +49,14 @@ }

}
if (this._server == null) {
await this._startServer();
}
if (this._browser == null) {
throw new Error('Browser is broken');
throw new Error('Browser not started');
}
const page = await this._browser.newPage();
await page.emulateMediaType('screen');
await page.goto(this._server.address);
const template = await this._readContentOrFile((_a = options.options) === null || _a === void 0 ? void 0 : _a.template, (_b = options.options) === null || _b === void 0 ? void 0 : _b.templatePath);
const template = await (0, utils_1.readContentOrFile)((_a = options.options) === null || _a === void 0 ? void 0 : _a.template, (_b = options.options) === null || _b === void 0 ? void 0 : _b.templatePath);
if (template !== '') {
if (/^\s*<!doctype html>/i.test(template)) {
await page.setContent(template);
}
else {
await page.evaluate((body) => {
const bodyElement = document.querySelector('body');
if (bodyElement != null) {
bodyElement.innerHTML = body;
}
}, template);
}
await page.setContent(template);
}
if (((_c = options.options) === null || _c === void 0 ? void 0 : _c.includes) != null) {
await this.includeAssets(page, options.options.includes);
await this._includeAssets(page, options.options.includes);
}
await page.waitForFunction('window.jQuery');
await page.waitForFunction('$(window).ready');
await page.evaluate(() => {
document.dispatchEvent(new CustomEvent('start'));
});
let pdfOptions = {

@@ -113,3 +79,3 @@ format: 'A4',

}
static async includeAssets(page, includes) {
static async _includeAssets(page, includes) {
for (const include of includes) {

@@ -124,20 +90,18 @@ let type = '';

if (type === 'css') {
await page.addStyleTag({ content: include.content, path: include.path });
await page.addStyleTag({
content: include.content,
path: include.path,
});
}
else if (type === 'js') {
await page.addScriptTag({ content: include.content, path: include.path });
await page.addScriptTag({
content: include.content,
path: include.path,
});
}
}
}
static async _readContentOrFile(content, path) {
if (content)
return lodash_1.default.toString(content);
if (!path)
return '';
if ((await fs_1.default.promises.stat(path)).isDirectory())
return '';
return (await fs_1.default.promises.readFile(path)).toString();
}
}
exports.PdfGenerator = PdfGenerator;
PdfGenerator._browserDisconnected = false;
PdfGenerator._tableGeneratorScript = '';
export declare function getFileExtension(filename: string): string;
export declare function stringify(obj: Record<string, any>): string;
export declare function readContentOrFile(content?: string, filePath?: string): Promise<string>;

@@ -6,4 +6,5 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.stringify = exports.getFileExtension = void 0;
exports.readContentOrFile = exports.stringify = exports.getFileExtension = void 0;
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
function getFileExtension(filename) {

@@ -20,1 +21,11 @@ return path_1.default.extname(filename).substring(1);

exports.stringify = stringify;
async function readContentOrFile(content, filePath) {
if (content)
return content;
if (!filePath)
return '';
if ((await fs_1.default.promises.stat(filePath)).isDirectory())
return '';
return (await fs_1.default.promises.readFile(filePath)).toString();
}
exports.readContentOrFile = readContentOrFile;
{
"name": "simple-pdf-generator",
"version": "2.1.1",
"version": "3.0.0",
"description": "Generator of PDF files from HTML templates using TS decorators",

@@ -28,15 +28,17 @@ "main": "./lib/index.js",

"devDependencies": {
"@types/lodash": "^4.14.182",
"@types/node": "^17.0.41",
"@types/stringify-object": "^4.0.1",
"@typescript-eslint/eslint-plugin": "^5.27.1",
"@typescript-eslint/parser": "^5.27.1",
"eslint": "^8.17.0",
"eslint-plugin-import": "^2.26.0",
"ts-node": "^10.8.1",
"typescript": "^4.7.3"
"@types/mime": "^3.0.1",
"@types/node": "^20.5.9",
"@types/stringify-object": "^4.0.2",
"@types/validator": "^13.11.1",
"@typescript-eslint/eslint-plugin": "^6.5.0",
"@typescript-eslint/parser": "^6.5.0",
"eslint": "^8.48.0",
"eslint-plugin-import": "^2.28.1",
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
},
"dependencies": {
"lodash": "^4.17.21",
"puppeteer": "^14.3.0"
"mime": "^3.0.0",
"puppeteer": "^21.1.1",
"validator": "^13.11.0"
},

@@ -43,0 +45,0 @@ "scripts": {

@@ -20,3 +20,2 @@ # Simple PDF Generator

- useses Bootstrap (5.1.0) and jQuery (3.6.0), they will be injected automatically in the HTML file;
- supports custom CSS and JS;

@@ -109,7 +108,7 @@ - fills custom fields in the HTML template;

export class Template extends PdfFiller {
@PdfField({ fieldName: 'custom-field-name' })
@PdfField()
field = '';
@PdfTable({ fieldName: 'data' })
tableData = new Array<TableRow>();
@PdfTable()
data = new Array<TableRow>();
}

@@ -177,8 +176,8 @@ ```

Extend abstract class `PdfFiller` and use the following decorators:
Extend abstract class `PdfFiller` and use the following decorators on the properties:
| Decorator | Parameters | HTML use |
|---|---|---|
| `PdfField` | `{fieldName: string}` | `%%fieldName%%` |
| `PdfTable` | `{tableName: string}` | `<inject-table :items="fieldName">`<br>&nbsp;&nbsp;&nbsp;&nbsp;`<inject-column prop="name" label="Name"/>`<br>&nbsp;&nbsp;&nbsp;&nbsp;`<inject-column prop="surname" label="Surname"/>`<br>`</inject-table>` |
| Decorator | HTML use |
|---|---|
| `PdfField` | `%%propertyName%%` |
| `PdfTable` | `<inject-table :items="propertyName">`<br>&nbsp;&nbsp;&nbsp;&nbsp;`<inject-column prop="name" label="Name"/>`<br>&nbsp;&nbsp;&nbsp;&nbsp;`<inject-column prop="surname" label="Surname"/>`<br>`</inject-table>` |

@@ -254,6 +253,6 @@ ### `fill`

export class Template extends PdfFiller {
@PdfField({ fieldName: 'firstField' })
@PdfField()
firstField = '';
@PdfField({ fieldName: 'secondField' })
@PdfField()
secondField = '';

@@ -260,0 +259,0 @@ }

@@ -18,8 +18,23 @@ /* eslint-disable no-undef */

newTable.setAttribute('class', table.getAttribute('class'));
newTable.setAttribute(':items', table.getAttribute(':items'));
newTable.setAttribute('id', table.getAttribute('id'));
newTable.setAttribute('style', table.getAttribute('style'));
const classAttribute = table.getAttribute('class');
if (classAttribute != null) {
newTable.className = classAttribute;
}
for (const column of columns) {
const idAttribute = table.getAttribute('id');
if (idAttribute != null) {
newTable.id = idAttribute;
}
const styleAttribute = table.getAttribute('style');
if (styleAttribute != null) {
newTable.style = styleAttribute;
}
const itemAttribute = table.getAttribute('items');
if (itemAttribute != null) {
newTable.setAttribute('items', itemAttribute);
}
for (const column of columns.values()) {
const cell = document.createElement('th');

@@ -52,6 +67,6 @@

function populateTable(table) {
const tableFound = tables.find(documentTable => documentTable === table);
if (tableFound == null) return;
const tableFound = tables.find((documentTable) => documentTable === table);
if (tableFound === undefined) return;
const data = Reflect.get(tablesData, tableFound.getAttribute(':items'));
const data = Reflect.get(tablesData, tableFound.getAttribute('items'));

@@ -79,1 +94,3 @@ const headColumns = [...tableFound.tHead.rows[0].cells];

}
createTables();

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc