Compare commits

..

15 Commits

Author SHA1 Message Date
5c0b8c4df0 1.0.13 2019-06-03 13:02:01 +02:00
8da88be5e8 fix(core): update 2019-06-03 13:02:01 +02:00
4f0164965c 1.0.12 2019-06-03 13:00:06 +02:00
63f4321b04 fix(core): update 2019-06-03 13:00:06 +02:00
e4287e9943 1.0.11 2019-06-03 10:51:16 +02:00
337c299a5e fix(core): update 2019-06-03 10:51:15 +02:00
4ac4d8d049 1.0.10 2019-05-29 19:49:24 +02:00
5e8abaa5b4 fix(core): update 2019-05-29 19:49:23 +02:00
d8fd7f9956 1.0.9 2019-05-29 19:19:36 +02:00
1711aadb6b fix(core): update 2019-05-29 19:19:36 +02:00
06b9385e97 1.0.8 2019-05-29 19:18:44 +02:00
94386b0e02 fix(core): update 2019-05-29 19:18:43 +02:00
36fea0b0f2 Merge branch 'master' of gitlab.com:pushrocks/smartpdf 2019-05-29 19:17:54 +02:00
437d56e54d 1.0.7 2019-05-29 19:17:39 +02:00
1537705cde fix(core): update 2019-05-29 19:17:39 +02:00
13 changed files with 53 additions and 16 deletions

3
.gitignore vendored
View File

@ -19,4 +19,5 @@ dist_web/
dist_serve/ dist_serve/
dist_ts_web/ dist_ts_web/
# custom # custom
assets/pdfdir

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/smartpdf", "name": "@pushrocks/smartpdf",
"version": "1.0.7", "version": "1.0.13",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/smartpdf", "name": "@pushrocks/smartpdf",
"version": "1.0.7", "version": "1.0.13",
"private": false, "private": false,
"description": "create pdfs on the fly", "description": "create pdfs on the fly",
"main": "dist/index.js", "main": "dist/index.js",

View File

@ -24,6 +24,11 @@ tap.test('should create a pdf from website as single page PDF', async () => {
await testSmartPdf.getFullWebsiteAsSinglePdf('https://maintainedby.lossless.com'); await testSmartPdf.getFullWebsiteAsSinglePdf('https://maintainedby.lossless.com');
}); });
tap.test('should create a valid PDFResult', async () => {
const pdfResult = await testSmartPdf.getFullWebsiteAsSinglePdf('https://maintainedby.lossless.com');
expect(pdfResult.buffer).to.be.instanceOf(Buffer);
});
tap.test('should be able to close properly', async () => { tap.test('should be able to close properly', async () => {
await testSmartPdf.stop(); await testSmartPdf.stop();
}); });

1
ts/interfaces/index.ts Normal file
View File

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

View File

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

View File

@ -5,20 +5,28 @@ import { PdfCandidate } from './smartpdf.classes.pdfcandidate';
declare const document; declare const document;
import { IPdfResult } from './interfaces';
export class SmartPdf { export class SmartPdf {
htmlServerInstance: Server; htmlServerInstance: Server;
serverPort: number; serverPort: number;
headlessBrowser: plugins.puppeteer.Browser; headlessBrowser: plugins.puppeteer.Browser;
externalBrowser: boolean = false;
private _readyDeferred: plugins.smartpromise.Deferred<void>; private _readyDeferred: plugins.smartpromise.Deferred<void>;
private _candidates: { [key: string]: PdfCandidate } = {}; private _candidates: { [key: string]: PdfCandidate } = {};
constructor() { constructor(headlessBrowserArg?) {
this.headlessBrowser = headlessBrowserArg
this._readyDeferred = new plugins.smartpromise.Deferred(); this._readyDeferred = new plugins.smartpromise.Deferred();
} }
async start() { async start() {
// setup puppeteer // setup puppeteer
this.headlessBrowser = await plugins.puppeteer.launch(); if (!this.headlessBrowser) {
this.headlessBrowser = await plugins.puppeteer.launch();
} else {
this.externalBrowser = true;
}
// setup server // setup server
const app = plugins.express(); const app = plugins.express();
@ -40,14 +48,18 @@ export class SmartPdf {
this.htmlServerInstance.close(() => { this.htmlServerInstance.close(() => {
done.resolve(); done.resolve();
}); });
await this.headlessBrowser.close();
if (!this.externalBrowser) {
await this.headlessBrowser.close();
}
await done.promise; await done.promise;
} }
/** /**
* returns a pdf for a given html string; * returns a pdf for a given html string;
*/ */
async getPdfForHtmlString(htmlStringArg: string) { async getPdfForHtmlString(htmlStringArg: string): Promise<IPdfResult> {
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;
@ -63,43 +75,51 @@ export class SmartPdf {
console.log(`id security check passed for ${pdfCandidate.pdfId}`); console.log(`id security check passed for ${pdfCandidate.pdfId}`);
} }
await page.pdf({ const pdfBuffer = await page.pdf({
path: plugins.path.join(paths.pdfDir, `${pdfCandidate.pdfId}.pdf`),
format: 'A4' format: 'A4'
}); });
await page.close(); await page.close();
delete this._candidates[pdfCandidate.pdfId]; delete this._candidates[pdfCandidate.pdfId];
pdfCandidate.doneDeferred.resolve(); pdfCandidate.doneDeferred.resolve();
await pdfCandidate.doneDeferred.promise; await pdfCandidate.doneDeferred.promise;
return {
id: pdfCandidate.pdfId,
name: `${pdfCandidate.pdfId}.js`,
buffer: pdfBuffer
};
} }
async getPdfForWebsite(websiteUrl: string) { async getPdfForWebsite(websiteUrl: string): Promise<IPdfResult> {
const page = await this.headlessBrowser.newPage(); const page = await this.headlessBrowser.newPage();
page.emulateMedia('screen');
const response = await page.goto(websiteUrl, { waitUntil: 'networkidle2' }); const response = await page.goto(websiteUrl, { waitUntil: 'networkidle2' });
const pdfId = plugins.smartunique.shortId(); const pdfId = plugins.smartunique.shortId();
await page.pdf({ const pdfBuffer = await page.pdf({
path: plugins.path.join(paths.pdfDir, `${pdfId}.pdf`),
format: 'A4', format: 'A4',
printBackground: true, printBackground: true,
displayHeaderFooter: false, displayHeaderFooter: false,
preferCSSPageSize: true preferCSSPageSize: true
}); });
await page.close(); await page.close();
return {
id: pdfId,
name: `${pdfId}.js`,
buffer: pdfBuffer
};
} }
async getFullWebsiteAsSinglePdf(websiteUrl: string) { async getFullWebsiteAsSinglePdf(websiteUrl: string) {
const page = await this.headlessBrowser.newPage(); const page = await this.headlessBrowser.newPage();
page.emulateMedia('screen');
const response = await page.goto(websiteUrl, { waitUntil: 'networkidle2' }); const response = await page.goto(websiteUrl, { waitUntil: 'networkidle2' });
const pdfId = plugins.smartunique.shortId(); const pdfId = plugins.smartunique.shortId();
const {documentHeight, documentWidth} = await page.evaluate(() => { const { documentHeight, documentWidth } = await page.evaluate(() => {
return { return {
documentHeight: document.height, documentHeight: document.height,
documentWidth: document.width documentWidth: document.width
}; };
}); });
await page.pdf({ const pdfBuffer = await page.pdf({
path: plugins.path.join(paths.pdfDir, `${pdfId}.pdf`),
height: documentWidth, height: documentWidth,
width: documentWidth, width: documentWidth,
printBackground: true, printBackground: true,
@ -107,5 +127,10 @@ export class SmartPdf {
preferCSSPageSize: true preferCSSPageSize: true
}); });
await page.close(); await page.close();
return {
id: pdfId,
name: `${pdfId}.js`,
buffer: pdfBuffer
};
} }
} }