Compare commits

..

10 Commits

Author SHA1 Message Date
796aa905d2 2.0.18 2022-01-06 13:23:05 +01:00
253fb95143 fix(core): update 2022-01-06 13:23:04 +01:00
f3ea075b72 2.0.17 2022-01-06 13:10:12 +01:00
af725a7f78 fix(core): update 2022-01-06 13:10:12 +01:00
016e0db797 2.0.16 2022-01-05 23:55:38 +01:00
4cf8b2e1f8 fix(core): update 2022-01-05 23:55:37 +01:00
67b0aa9d47 2.0.15 2022-01-05 17:20:28 +01:00
567c6eafea fix(core): update 2022-01-05 17:20:28 +01:00
ff890fb2af 2.0.14 2022-01-05 16:32:48 +01:00
a512fd64b5 fix(core): update 2022-01-05 16:32:47 +01:00
6 changed files with 114 additions and 24 deletions

48
package-lock.json generated
View File

@ -1,12 +1,12 @@
{
"name": "@pushrocks/smartpdf",
"version": "2.0.13",
"version": "2.0.18",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@pushrocks/smartpdf",
"version": "2.0.13",
"version": "2.0.18",
"license": "MIT",
"dependencies": {
"@pushrocks/smartfile": "^9.0.5",
@ -16,7 +16,8 @@
"@pushrocks/smartunique": "^3.0.3",
"@types/express": "^4.17.13",
"express": "^4.17.2",
"pdf-merger-js": "^3.2.1"
"pdf-merger-js": "^3.2.1",
"pdf2json": "^2.0.0"
},
"devDependencies": {
"@gitzone/tsbuild": "^2.1.28",
@ -10978,6 +10979,33 @@
"pdfjs": "^2.4.5"
}
},
"node_modules/pdf2json": {
"version": "2.0.0",
"resolved": "https://verdaccio.lossless.one/pdf2json/-/pdf2json-2.0.0.tgz",
"integrity": "sha512-+FZy7GSvLOLc+zksg0SoMvXqIqcku5lBlEPuYJJkhMWB2x6yfthzEhhSbZc20UheClMPagH/+NXnMRbvQMQR1w==",
"bundleDependencies": [
"@xmldom/xmldom"
],
"license": "Apache-2.0",
"dependencies": {
"@xmldom/xmldom": "^0.7.5"
},
"bin": {
"pdf2json": "bin/pdf2json"
},
"engines": {
"node": ">=14.18.0",
"npm": ">=6.14.15"
}
},
"node_modules/pdf2json/node_modules/@xmldom/xmldom": {
"version": "0.7.5",
"inBundle": true,
"license": "MIT",
"engines": {
"node": ">=10.0.0"
}
},
"node_modules/pdfjs": {
"version": "2.4.6",
"resolved": "https://verdaccio.lossless.one/pdfjs/-/pdfjs-2.4.6.tgz",
@ -23633,6 +23661,20 @@
"pdfjs": "^2.4.5"
}
},
"pdf2json": {
"version": "2.0.0",
"resolved": "https://verdaccio.lossless.one/pdf2json/-/pdf2json-2.0.0.tgz",
"integrity": "sha512-+FZy7GSvLOLc+zksg0SoMvXqIqcku5lBlEPuYJJkhMWB2x6yfthzEhhSbZc20UheClMPagH/+NXnMRbvQMQR1w==",
"requires": {
"@xmldom/xmldom": "^0.7.5"
},
"dependencies": {
"@xmldom/xmldom": {
"version": "0.7.5",
"bundled": true
}
}
},
"pdfjs": {
"version": "2.4.6",
"resolved": "https://verdaccio.lossless.one/pdfjs/-/pdfjs-2.4.6.tgz",

View File

@ -1,6 +1,6 @@
{
"name": "@pushrocks/smartpdf",
"version": "2.0.13",
"version": "2.0.18",
"private": false,
"description": "create pdfs on the fly",
"main": "dist_ts/index.js",
@ -28,7 +28,8 @@
"@pushrocks/smartunique": "^3.0.3",
"@types/express": "^4.17.13",
"express": "^4.17.2",
"pdf-merger-js": "^3.2.1"
"pdf-merger-js": "^3.2.1",
"pdf2json": "^2.0.0"
},
"files": [
"ts/**/*",

View File

@ -13,7 +13,11 @@ tap.test('should start the instance', async () => {
});
tap.test('should create a pdf from html string', async () => {
await testSmartPdf.getPdfResultForHtmlString('hi');
await testSmartPdf.getA4PdfResultForHtmlString('hi');
});
tap.test('should create a pdf from html string', async () => {
await testSmartPdf.getA4PdfResultForHtmlString('hi');
});
tap.test('should create a pdf from website as A4', async () => {
@ -36,7 +40,7 @@ tap.test('should create a valid PDFResult', async () => {
fs.writeFileSync(`.nogit/${fileName}`, pdfResult.buffer);
};
await writePDfToDisk('https://maintainedby.lossless.com/', '1.pdf')
await writePDfToDisk('https://lossless.com/', '2.pdf')
await writePDfToDisk('https://rendertron.lossless.one/render/https://lossless.com', '2.pdf')
});
tap.test('should combine pdfs', async () => {

View File

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

View File

@ -63,15 +63,15 @@ export class SmartPdf {
/**
* returns a pdf for a given html string;
*/
async getPdfResultForHtmlString(htmlStringArg: string): Promise<interfaces.IPdfResult> {
async getA4PdfResultForHtmlString(htmlStringArg: string): Promise<interfaces.IPdfResult> {
await this._readyDeferred.promise;
const pdfCandidate = new PdfCandidate(htmlStringArg);
this._candidates[pdfCandidate.pdfId] = pdfCandidate;
const page = await this.headlessBrowser.newPage();
await page.setViewport({
height: 842,
width: 595,
})
width: 794,
height: 1122,
});
const response = await page.goto(`http://localhost:3210/${pdfCandidate.pdfId}`, {
waitUntil: 'networkidle2',
});
@ -84,10 +84,10 @@ export class SmartPdf {
}
const pdfBuffer = await page.pdf({
format: 'a4',
width: 794,
height: 1122,
printBackground: true,
displayHeaderFooter: false,
preferCSSPageSize: true,
});
await page.close();
delete this._candidates[pdfCandidate.pdfId];
@ -96,12 +96,19 @@ export class SmartPdf {
return {
id: pdfCandidate.pdfId,
name: `${pdfCandidate.pdfId}.js`,
metadata: {
textExtraction: await this.extractTextFromPdfBuffer(pdfBuffer),
},
buffer: pdfBuffer,
};
}
async getPdfResultForWebsite(websiteUrl: string): Promise<interfaces.IPdfResult> {
const page = await this.headlessBrowser.newPage();
await page.setViewport({
width: 1980,
height: 1200,
});
await page.emulateMediaType('screen');
const response = await page.goto(websiteUrl, { waitUntil: 'networkidle2' });
const pdfId = plugins.smartunique.shortId();
@ -112,44 +119,56 @@ export class SmartPdf {
};
});
const pdfBuffer = await page.pdf({
format: 'a4',
height: documentWidth,
height: documentHeight,
width: documentWidth,
printBackground: true,
displayHeaderFooter: false,
preferCSSPageSize: true,
});
await page.close();
return {
id: pdfId,
name: `${pdfId}.js`,
metadata: {
textExtraction: await this.extractTextFromPdfBuffer(pdfBuffer),
},
buffer: pdfBuffer,
};
}
async getFullWebsiteAsSinglePdf(websiteUrl: string) {
async getFullWebsiteAsSinglePdf(websiteUrl: string): Promise<interfaces.IPdfResult> {
const page = await this.headlessBrowser.newPage();
await page.setViewport({
width: 1920,
height: 1200,
});
page.emulateMediaType('screen');
const response = await page.goto(websiteUrl, { waitUntil: 'networkidle2' });
const pdfId = plugins.smartunique.shortId();
const { documentHeight, documentWidth } = await page.evaluate(() => {
return {
documentHeight: document.height,
documentWidth: document.width,
documentHeight: document.body.scrollHeight,
documentWidth: document.body.clientWidth,
};
});
await page.setViewport({
width: 1920,
height: documentHeight,
});
const pdfBuffer = await page.pdf({
format: 'a4',
height: documentWidth,
width: documentWidth,
height: documentHeight,
width: 1920,
printBackground: true,
displayHeaderFooter: false,
preferCSSPageSize: true,
scale: 1,
pageRanges: '1'
});
await page.close();
return {
id: pdfId,
name: `${pdfId}.js`,
metadata: {
textExtraction: await this.extractTextFromPdfBuffer(pdfBuffer),
},
buffer: pdfBuffer,
};
}
@ -161,4 +180,23 @@ export class SmartPdf {
}
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 = ''
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);
});
pdfParser.parseBuffer(pdfBufferArg);
return deferred.promise;
}
}

View File

@ -15,6 +15,8 @@ export { smartfile, smartpromise, smartpuppeteer, smartunique, smartnetwork };
// thirdparty
import pdfMerger from 'pdf-merger-js';
// @ts-ignore
import pdf2json from 'pdf2json';
import express from 'express';
export { pdfMerger, express };
export { pdfMerger, pdf2json, express };