fix(core): update

This commit is contained in:
Philipp Kunz 2022-06-15 22:14:55 +02:00
parent 5e15729045
commit 0b5ec86780
10 changed files with 2755 additions and 10528 deletions

13165
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -13,21 +13,22 @@
"build": "(tsbuild --web --allowimplicitany)" "build": "(tsbuild --web --allowimplicitany)"
}, },
"devDependencies": { "devDependencies": {
"@gitzone/tsbuild": "^2.1.61", "@gitzone/tsbuild": "^2.1.63",
"@gitzone/tsrun": "^1.2.32", "@gitzone/tsrun": "^1.2.35",
"@gitzone/tstest": "^1.0.69", "@gitzone/tstest": "^1.0.71",
"@pushrocks/tapbundle": "^5.0.3", "@pushrocks/tapbundle": "^5.0.3",
"@types/node": "^17.0.23" "@types/node": "^17.0.42"
}, },
"dependencies": { "dependencies": {
"@pushrocks/smartfile": "^9.0.6", "@pushrocks/smartfile": "^10.0.2",
"@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.0",
"@pushrocks/smartunique": "^3.0.3", "@pushrocks/smartunique": "^3.0.3",
"@tsclass/tsclass": "^4.0.15",
"@types/express": "^4.17.13", "@types/express": "^4.17.13",
"express": "^4.17.3", "express": "^4.18.1",
"pdf-merger-js": "^3.4.0", "pdf-merger-js": "^3.4.0",
"pdf2json": "^2.0.0" "pdf2json": "^2.0.0"
}, },

View File

@ -37,7 +37,7 @@ 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://fitnessloft.de/impressum/', '2.pdf')
@ -45,9 +45,9 @@ tap.test('should create a valid PDFResult', async () => {
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);
}) })

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@pushrocks/smartpdf', name: '@pushrocks/smartpdf',
version: '3.0.4', version: '3.0.5',
description: 'create pdfs on the fly' description: 'create pdfs on the fly'
} }

View File

@ -6,5 +6,4 @@ declare global {
} }
// normal // normal
export * from './interfaces/index.js';
export * from './smartpdf.classes.smartpdf.js'; export * from './smartpdf.classes.smartpdf.js';

View File

@ -1 +0,0 @@
export * from './interface.pdfresult.js';

View File

@ -1,8 +0,0 @@
export interface IPdfResult {
name: string;
id: string;
metadata: {
textExtraction: string;
};
buffer: Buffer;
}

View File

@ -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;
@ -63,7 +69,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;
@ -103,7 +109,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 +141,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 +166,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 +179,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(pdf.buffer as 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);
}); });

View File

@ -14,6 +14,13 @@ import * as smartunique from '@pushrocks/smartunique';
export { smartfile, smartpromise, smartpath, smartpuppeteer, smartunique, smartnetwork }; export { smartfile, 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';
// @ts-ignore // @ts-ignore

View File

@ -3,6 +3,7 @@
"experimentalDecorators": true, "experimentalDecorators": true,
"target": "ES2022", "target": "ES2022",
"module": "ES2022", "module": "ES2022",
"moduleResolution": "nodenext" "moduleResolution": "nodenext",
"allowSyntheticDefaultImports": true
} }
} }