Compare commits

..

16 Commits

Author SHA1 Message Date
57851d90a5 3.0.11 2022-09-15 20:52:22 +02:00
1e4b16b734 fix(core): update 2022-09-15 20:52:22 +02:00
876466b18d 3.0.10 2022-09-13 18:39:38 +02:00
82b1c187ee fix(core): update 2022-09-13 18:39:38 +02:00
4f93d258b8 3.0.9 2022-09-13 18:26:15 +02:00
60993fc005 fix(core): update 2022-09-13 18:26:14 +02:00
db4965c8f5 3.0.8 2022-09-05 01:02:05 +02:00
36a964d931 fix(core): update 2022-09-05 01:02:05 +02:00
6d90f4c9b4 3.0.7 2022-06-29 00:24:44 +02:00
839eafd73f fix(core): update 2022-06-29 00:24:43 +02:00
f4141bf201 3.0.6 2022-06-15 22:16:44 +02:00
5a46028053 fix(core): update 2022-06-15 22:16:44 +02:00
18d26647e1 3.0.5 2022-06-15 22:14:55 +02:00
0b5ec86780 fix(core): update 2022-06-15 22:14:55 +02:00
5e15729045 3.0.4 2022-06-12 19:26:49 +02:00
c98a5f1ac3 fix(core): update 2022-06-12 19:26:49 +02:00
11 changed files with 5551 additions and 10873 deletions

21
license Normal file
View File

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2016 Push.Rocks
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

16239
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{
"name": "@pushrocks/smartpdf",
"version": "3.0.3",
"version": "3.0.11",
"private": false,
"description": "create pdfs on the fly",
"main": "dist_ts/index.js",
@ -13,21 +13,23 @@
"build": "(tsbuild --web --allowimplicitany)"
},
"devDependencies": {
"@gitzone/tsbuild": "^2.1.61",
"@gitzone/tsrun": "^1.2.32",
"@gitzone/tstest": "^1.0.69",
"@pushrocks/tapbundle": "^5.0.3",
"@types/node": "^17.0.23"
"@gitzone/tsbuild": "^2.1.65",
"@gitzone/tsrun": "^1.2.35",
"@gitzone/tstest": "^1.0.73",
"@pushrocks/tapbundle": "^5.0.4",
"@types/node": "^18.7.18"
},
"dependencies": {
"@pushrocks/smartfile": "^9.0.6",
"@pushrocks/smartdelay": "^2.0.13",
"@pushrocks/smartfile": "^10.0.5",
"@pushrocks/smartnetwork": "^3.0.0",
"@pushrocks/smartpath": "^5.0.5",
"@pushrocks/smartpromise": "^3.1.7",
"@pushrocks/smartpuppeteer": "^2.0.0",
"@pushrocks/smartpuppeteer": "^2.0.2",
"@pushrocks/smartunique": "^3.0.3",
"@types/express": "^4.17.13",
"express": "^4.17.3",
"@tsclass/tsclass": "^4.0.21",
"@types/express": "^4.17.14",
"express": "^4.18.1",
"pdf-merger-js": "^3.4.0",
"pdf2json": "^2.0.0"
},

View File

@ -37,17 +37,17 @@ tap.test('should create a valid PDFResult', async () => {
if (!fs.existsSync('.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://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 () => {
const fs = await import('fs');
const buffer1 = fs.readFileSync('.nogit/1.pdf');
const buffer2 = fs.readFileSync('.nogit/2.pdf');
fs.writeFileSync(`.nogit/combined.pdf`, await testSmartPdf.mergePdfBuffers([buffer1, buffer2]));
const pdf1 = await testSmartPdf.readFileToPdfObject('.nogit/1.pdf');
const pdf2 = await testSmartPdf.readFileToPdfObject('.nogit/2.pdf');
fs.writeFileSync(`.nogit/combined.pdf`, (await testSmartPdf.mergePdfs([pdf1, pdf2])).buffer as Buffer);
})

View File

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

View File

@ -6,5 +6,4 @@ declare global {
}
// normal
export * from './interfaces/index.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 { Server } from 'http';
import { PdfCandidate } from './smartpdf.classes.pdfcandidate.js';
import { IPdf } from '@tsclass/tsclass/dist_ts/business/pdf.js';
declare const document: any;
import * as interfaces from './interfaces/index.js';
export class SmartPdf {
// STATIC
public static async create() {
const smartpdfInstance = new SmartPdf();
return smartpdfInstance;
}
// INSTANCE
htmlServerInstance: Server;
serverPort: number;
headlessBrowser: plugins.smartpuppeteer.puppeteer.Browser;
@ -20,6 +26,7 @@ export class SmartPdf {
}
async start(headlessBrowserArg?: plugins.smartpuppeteer.puppeteer.Browser) {
const done = plugins.smartpromise.defer();
// lets set the external browser in case one is provided
this.headlessBrowser = headlessBrowserArg;
// setup puppeteer
@ -43,7 +50,9 @@ export class SmartPdf {
this.htmlServerInstance.listen(3210, 'localhost');
this.htmlServerInstance.on('listening', () => {
this._readyDeferred.resolve();
done.resolve();
});
await done.promise;
}
// stop
@ -63,7 +72,7 @@ export class SmartPdf {
/**
* 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;
const pdfCandidate = new PdfCandidate(htmlStringArg);
this._candidates[pdfCandidate.pdfId] = pdfCandidate;
@ -75,6 +84,7 @@ export class SmartPdf {
const response = await page.goto(`http://localhost:3210/${pdfCandidate.pdfId}`, {
waitUntil: 'networkidle2',
});
// await plugins.smartdelay.delayFor(1000);
const headers = response.headers();
if (headers['pdf-id'] !== pdfCandidate.pdfId) {
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();
await page.setViewport({
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();
await page.setViewport({
width: 1920,
@ -160,7 +170,7 @@ export class SmartPdf {
printBackground: true,
displayHeaderFooter: false,
scale: 1,
pageRanges: '1'
pageRanges: '1',
});
await page.close();
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();
for (const buffer of pdfBuffers) {
merger.add(buffer);
for (const pdf of pdfArrayArg) {
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> {
const deferred = plugins.smartpromise.defer<string>();
const pdfParser: any = new plugins.pdf2json();
pdfParser.on('pdfParser_dataReady', (pdfData: any) => {
let finalText = ''
let finalText = '';
for (const page of pdfData.Pages) {
for (const text of page.Texts) {
for (const letter of text.R) {
finalText = finalText + letter.T;
}
};
}
}
deferred.resolve(finalText);
});

View File

@ -6,13 +6,21 @@ export { http, path };
// @pushrocks
import * as smartfile from '@pushrocks/smartfile';
import * as smartdelay from '@pushrocks/smartdelay';
import * as smartpromise from '@pushrocks/smartpromise';
import * as smartpath from '@pushrocks/smartpath';
import * as smartpuppeteer from '@pushrocks/smartpuppeteer';
import * as smartnetwork from '@pushrocks/smartnetwork';
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
import pdfMerger from 'pdf-merger-js';

View File

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