Compare commits
22 Commits
Author | SHA1 | Date | |
---|---|---|---|
ffeaec7fe9 | |||
a31e196e5b | |||
57851d90a5 | |||
1e4b16b734 | |||
876466b18d | |||
82b1c187ee | |||
4f93d258b8 | |||
60993fc005 | |||
db4965c8f5 | |||
36a964d931 | |||
6d90f4c9b4 | |||
839eafd73f | |||
f4141bf201 | |||
5a46028053 | |||
18d26647e1 | |||
0b5ec86780 | |||
5e15729045 | |||
c98a5f1ac3 | |||
7f02146a0e | |||
5dab8c7351 | |||
505e0e9a30 | |||
9e3510955a |
9
.snyk
9
.snyk
@ -1,9 +0,0 @@
|
|||||||
# Snyk (https://snyk.io) policy file, patches or ignores known vulnerabilities.
|
|
||||||
version: v1.13.5
|
|
||||||
# ignores vulnerabilities until expiry date; change duration by modifying expiry date
|
|
||||||
ignore:
|
|
||||||
SNYK-JS-LODASH-450202:
|
|
||||||
- '@pushrocks/smartnetwork > portscanner > async > lodash':
|
|
||||||
reason: None given
|
|
||||||
expires: '2019-09-13T09:39:26.276Z'
|
|
||||||
patch: {}
|
|
16491
package-lock.json
generated
16491
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
23
package.json
23
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartpdf",
|
"name": "@pushrocks/smartpdf",
|
||||||
"version": "3.0.1",
|
"version": "3.0.12",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "create pdfs on the fly",
|
"description": "create pdfs on the fly",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
@ -13,21 +13,24 @@
|
|||||||
"build": "(tsbuild --web --allowimplicitany)"
|
"build": "(tsbuild --web --allowimplicitany)"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.1.61",
|
"@gitzone/tsbuild": "^2.1.65",
|
||||||
"@gitzone/tsrun": "^1.2.32",
|
"@gitzone/tsdoc": "^1.1.12",
|
||||||
"@gitzone/tstest": "^1.0.69",
|
"@gitzone/tsrun": "^1.2.35",
|
||||||
"@pushrocks/tapbundle": "^5.0.3",
|
"@gitzone/tstest": "^1.0.73",
|
||||||
"@types/node": "^17.0.23"
|
"@pushrocks/tapbundle": "^5.0.4",
|
||||||
|
"@types/node": "^18.7.18"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/smartfile": "^9.0.6",
|
"@pushrocks/smartdelay": "^2.0.13",
|
||||||
|
"@pushrocks/smartfile": "^10.0.5",
|
||||||
"@pushrocks/smartnetwork": "^3.0.0",
|
"@pushrocks/smartnetwork": "^3.0.0",
|
||||||
"@pushrocks/smartpath": "^5.0.5",
|
"@pushrocks/smartpath": "^5.0.5",
|
||||||
"@pushrocks/smartpromise": "^3.1.7",
|
"@pushrocks/smartpromise": "^3.1.7",
|
||||||
"@pushrocks/smartpuppeteer": "^2.0.0",
|
"@pushrocks/smartpuppeteer": "^2.0.2",
|
||||||
"@pushrocks/smartunique": "^3.0.3",
|
"@pushrocks/smartunique": "^3.0.3",
|
||||||
"@types/express": "^4.17.13",
|
"@tsclass/tsclass": "^4.0.21",
|
||||||
"express": "^4.17.3",
|
"@types/express": "^4.17.14",
|
||||||
|
"express": "^4.18.1",
|
||||||
"pdf-merger-js": "^3.4.0",
|
"pdf-merger-js": "^3.4.0",
|
||||||
"pdf2json": "^2.0.0"
|
"pdf2json": "^2.0.0"
|
||||||
},
|
},
|
||||||
|
10
test/test.ts
10
test/test.ts
@ -37,17 +37,17 @@ tap.test('should create a valid PDFResult', async () => {
|
|||||||
if (!fs.existsSync('.nogit/')) {
|
if (!fs.existsSync('.nogit/')) {
|
||||||
fs.mkdirSync('.nogit/');
|
fs.mkdirSync('.nogit/');
|
||||||
}
|
}
|
||||||
fs.writeFileSync(`.nogit/${fileName}`, pdfResult.buffer);
|
fs.writeFileSync(`.nogit/${fileName}`, pdfResult.buffer as Buffer);
|
||||||
};
|
};
|
||||||
await writePDfToDisk('https://maintainedby.lossless.com/', '1.pdf')
|
await writePDfToDisk('https://maintainedby.lossless.com/', '1.pdf')
|
||||||
await writePDfToDisk('https://rendertron.lossless.one/render/https://fitnessloft.de/impressum/', '2.pdf')
|
await writePDfToDisk('https://rendertron.lossless.one/render/https://layer.io', '2.pdf')
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should combine pdfs', async () => {
|
tap.test('should combine pdfs', async () => {
|
||||||
const fs = await import('fs');
|
const fs = await import('fs');
|
||||||
const buffer1 = fs.readFileSync('.nogit/1.pdf');
|
const pdf1 = await testSmartPdf.readFileToPdfObject('.nogit/1.pdf');
|
||||||
const buffer2 = fs.readFileSync('.nogit/2.pdf');
|
const pdf2 = await testSmartPdf.readFileToPdfObject('.nogit/2.pdf');
|
||||||
fs.writeFileSync(`.nogit/combined.pdf`, await testSmartPdf.mergePdfBuffers([buffer1, buffer2]));
|
fs.writeFileSync(`.nogit/combined.pdf`, (await testSmartPdf.mergePdfs([pdf1, pdf2])).buffer as Buffer);
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
8
ts/00_commitinfo_data.ts
Normal file
8
ts/00_commitinfo_data.ts
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
/**
|
||||||
|
* autocreated commitinfo by @pushrocks/commitinfo
|
||||||
|
*/
|
||||||
|
export const commitinfo = {
|
||||||
|
name: '@pushrocks/smartpdf',
|
||||||
|
version: '3.0.12',
|
||||||
|
description: 'create pdfs on the fly'
|
||||||
|
}
|
@ -1 +0,0 @@
|
|||||||
export * from './interface.pdfresult.js';
|
|
@ -1,8 +0,0 @@
|
|||||||
export interface IPdfResult {
|
|
||||||
name: string;
|
|
||||||
id: string;
|
|
||||||
metadata: {
|
|
||||||
textExtraction: string;
|
|
||||||
};
|
|
||||||
buffer: Buffer;
|
|
||||||
}
|
|
@ -1,3 +1,7 @@
|
|||||||
|
/*
|
||||||
|
* pdf candidate is a construct used internally for mapping html
|
||||||
|
* to pdf buffers delivered by puppeteer
|
||||||
|
*/
|
||||||
import * as plugins from './smartpdf.plugins.js';
|
import * as plugins from './smartpdf.plugins.js';
|
||||||
|
|
||||||
export class PdfCandidate {
|
export class PdfCandidate {
|
||||||
|
@ -2,12 +2,18 @@ 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 { Server } from 'http';
|
||||||
import { PdfCandidate } from './smartpdf.classes.pdfcandidate.js';
|
import { PdfCandidate } from './smartpdf.classes.pdfcandidate.js';
|
||||||
|
import { IPdf } from '@tsclass/tsclass/dist_ts/business/pdf.js';
|
||||||
|
|
||||||
declare const document: any;
|
declare const document: any;
|
||||||
|
|
||||||
import * as interfaces from './interfaces/index.js';
|
|
||||||
|
|
||||||
export class SmartPdf {
|
export class SmartPdf {
|
||||||
|
// STATIC
|
||||||
|
public static async create() {
|
||||||
|
const smartpdfInstance = new SmartPdf();
|
||||||
|
return smartpdfInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
// INSTANCE
|
||||||
htmlServerInstance: Server;
|
htmlServerInstance: Server;
|
||||||
serverPort: number;
|
serverPort: number;
|
||||||
headlessBrowser: plugins.smartpuppeteer.puppeteer.Browser;
|
headlessBrowser: plugins.smartpuppeteer.puppeteer.Browser;
|
||||||
@ -20,6 +26,7 @@ export class SmartPdf {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async start(headlessBrowserArg?: plugins.smartpuppeteer.puppeteer.Browser) {
|
async start(headlessBrowserArg?: plugins.smartpuppeteer.puppeteer.Browser) {
|
||||||
|
const done = plugins.smartpromise.defer();
|
||||||
// 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
|
||||||
@ -43,7 +50,9 @@ export class SmartPdf {
|
|||||||
this.htmlServerInstance.listen(3210, 'localhost');
|
this.htmlServerInstance.listen(3210, 'localhost');
|
||||||
this.htmlServerInstance.on('listening', () => {
|
this.htmlServerInstance.on('listening', () => {
|
||||||
this._readyDeferred.resolve();
|
this._readyDeferred.resolve();
|
||||||
|
done.resolve();
|
||||||
});
|
});
|
||||||
|
await done.promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
// stop
|
// stop
|
||||||
@ -63,7 +72,7 @@ export class SmartPdf {
|
|||||||
/**
|
/**
|
||||||
* returns a pdf for a given html string;
|
* returns a pdf for a given html string;
|
||||||
*/
|
*/
|
||||||
async getA4PdfResultForHtmlString(htmlStringArg: string): Promise<interfaces.IPdfResult> {
|
async getA4PdfResultForHtmlString(htmlStringArg: string): Promise<plugins.tsclass.business.IPdf> {
|
||||||
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;
|
||||||
@ -75,6 +84,7 @@ export class SmartPdf {
|
|||||||
const response = await page.goto(`http://localhost:3210/${pdfCandidate.pdfId}`, {
|
const response = await page.goto(`http://localhost:3210/${pdfCandidate.pdfId}`, {
|
||||||
waitUntil: 'networkidle2',
|
waitUntil: 'networkidle2',
|
||||||
});
|
});
|
||||||
|
// await plugins.smartdelay.delayFor(1000);
|
||||||
const headers = response.headers();
|
const headers = response.headers();
|
||||||
if (headers['pdf-id'] !== pdfCandidate.pdfId) {
|
if (headers['pdf-id'] !== pdfCandidate.pdfId) {
|
||||||
console.log('Error! Headers do not match. For security reasons no pdf is being emitted!');
|
console.log('Error! Headers do not match. For security reasons no pdf is being emitted!');
|
||||||
@ -103,7 +113,7 @@ export class SmartPdf {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async getPdfResultForWebsite(websiteUrl: string): Promise<interfaces.IPdfResult> {
|
async getPdfResultForWebsite(websiteUrl: string): Promise<plugins.tsclass.business.IPdf> {
|
||||||
const page = await this.headlessBrowser.newPage();
|
const page = await this.headlessBrowser.newPage();
|
||||||
await page.setViewport({
|
await page.setViewport({
|
||||||
width: 1980,
|
width: 1980,
|
||||||
@ -135,7 +145,7 @@ export class SmartPdf {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async getFullWebsiteAsSinglePdf(websiteUrl: string): Promise<interfaces.IPdfResult> {
|
async getFullWebsiteAsSinglePdf(websiteUrl: string): Promise<plugins.tsclass.business.IPdf> {
|
||||||
const page = await this.headlessBrowser.newPage();
|
const page = await this.headlessBrowser.newPage();
|
||||||
await page.setViewport({
|
await page.setViewport({
|
||||||
width: 1920,
|
width: 1920,
|
||||||
@ -160,7 +170,7 @@ export class SmartPdf {
|
|||||||
printBackground: true,
|
printBackground: true,
|
||||||
displayHeaderFooter: false,
|
displayHeaderFooter: false,
|
||||||
scale: 1,
|
scale: 1,
|
||||||
pageRanges: '1'
|
pageRanges: '1',
|
||||||
});
|
});
|
||||||
await page.close();
|
await page.close();
|
||||||
return {
|
return {
|
||||||
@ -173,26 +183,43 @@ export class SmartPdf {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public async mergePdfBuffers(pdfBuffers: Buffer[]): Promise<Buffer> {
|
public async mergePdfs(pdfArrayArg: plugins.tsclass.business.IPdf[]): Promise<IPdf> {
|
||||||
const merger = new plugins.pdfMerger();
|
const merger = new plugins.pdfMerger();
|
||||||
for (const buffer of pdfBuffers) {
|
for (const pdf of pdfArrayArg) {
|
||||||
merger.add(buffer);
|
merger.add(Buffer.from(pdf.buffer));
|
||||||
|
}
|
||||||
|
const resultBuffer = await merger.saveAsBuffer();
|
||||||
|
return {
|
||||||
|
name: 'mergedPdf',
|
||||||
|
buffer: resultBuffer,
|
||||||
|
id: null,
|
||||||
|
metadata: null
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public async readFileToPdfObject(pathArg: string): Promise<plugins.tsclass.business.IPdf> {
|
||||||
|
const path = plugins.smartpath.transform.makeAbsolute(pathArg);
|
||||||
|
const parsedPath = plugins.path.parse(path);
|
||||||
|
const buffer = await plugins.smartfile.fs.toBuffer(path);
|
||||||
|
return {
|
||||||
|
name: parsedPath.base,
|
||||||
|
buffer,
|
||||||
|
id: null,
|
||||||
|
metadata: null
|
||||||
}
|
}
|
||||||
return merger.saveAsBuffer();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async extractTextFromPdfBuffer(pdfBufferArg: Buffer): Promise<string> {
|
public async extractTextFromPdfBuffer(pdfBufferArg: Buffer): Promise<string> {
|
||||||
const deferred = plugins.smartpromise.defer<string>();
|
const deferred = plugins.smartpromise.defer<string>();
|
||||||
const pdfParser: any = new plugins.pdf2json();
|
const pdfParser: any = new plugins.pdf2json();
|
||||||
pdfParser.on('pdfParser_dataReady', (pdfData: any) => {
|
pdfParser.on('pdfParser_dataReady', (pdfData: any) => {
|
||||||
let finalText = ''
|
let finalText = '';
|
||||||
for (const page of pdfData.Pages) {
|
for (const page of pdfData.Pages) {
|
||||||
for(const text of page.Texts) {
|
for (const text of page.Texts) {
|
||||||
for (const letter of text.R) {
|
for (const letter of text.R) {
|
||||||
finalText = finalText + letter.T;
|
finalText = finalText + letter.T;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
|
||||||
}
|
}
|
||||||
deferred.resolve(finalText);
|
deferred.resolve(finalText);
|
||||||
});
|
});
|
||||||
|
@ -6,13 +6,21 @@ export { http, path };
|
|||||||
|
|
||||||
// @pushrocks
|
// @pushrocks
|
||||||
import * as smartfile from '@pushrocks/smartfile';
|
import * as smartfile from '@pushrocks/smartfile';
|
||||||
|
import * as smartdelay from '@pushrocks/smartdelay';
|
||||||
import * as smartpromise from '@pushrocks/smartpromise';
|
import * as smartpromise from '@pushrocks/smartpromise';
|
||||||
import * as smartpath from '@pushrocks/smartpath';
|
import * as smartpath from '@pushrocks/smartpath';
|
||||||
import * as smartpuppeteer from '@pushrocks/smartpuppeteer';
|
import * as smartpuppeteer from '@pushrocks/smartpuppeteer';
|
||||||
import * as smartnetwork from '@pushrocks/smartnetwork';
|
import * as smartnetwork from '@pushrocks/smartnetwork';
|
||||||
import * as smartunique from '@pushrocks/smartunique';
|
import * as smartunique from '@pushrocks/smartunique';
|
||||||
|
|
||||||
export { smartfile, smartpromise, smartpath, smartpuppeteer, smartunique, smartnetwork };
|
export { smartfile, smartdelay, smartpromise, smartpath, smartpuppeteer, smartunique, smartnetwork };
|
||||||
|
|
||||||
|
// tsclass scope
|
||||||
|
import * as tsclass from '@tsclass/tsclass';
|
||||||
|
|
||||||
|
export {
|
||||||
|
tsclass
|
||||||
|
}
|
||||||
|
|
||||||
// thirdparty
|
// thirdparty
|
||||||
import pdfMerger from 'pdf-merger-js';
|
import pdfMerger from 'pdf-merger-js';
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
"experimentalDecorators": true,
|
"experimentalDecorators": true,
|
||||||
"target": "ES2022",
|
"target": "ES2022",
|
||||||
"module": "ES2022",
|
"module": "ES2022",
|
||||||
"moduleResolution": "nodenext"
|
"moduleResolution": "nodenext",
|
||||||
|
"allowSyntheticDefaultImports": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user