Compare commits

..

12 Commits

Author SHA1 Message Date
30e4b4665c 1.0.17 2019-06-03 17:09:16 +02:00
74a0a27fc1 fix(core): update 2019-06-03 17:09:16 +02:00
59eed53644 1.0.16 2019-06-03 16:39:21 +02:00
473aaa004a fix(core): update 2019-06-03 16:39:21 +02:00
ecfd4115a1 1.0.15 2019-06-03 13:56:44 +02:00
0fbbfaac7c fix(core): update 2019-06-03 13:56:43 +02:00
86d2fc2c5b 1.0.14 2019-06-03 13:45:57 +02:00
f97866fe82 fix(core): update 2019-06-03 13:45:57 +02:00
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
4 changed files with 26 additions and 11 deletions

2
package-lock.json generated
View File

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

View File

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

View File

@ -5,12 +5,13 @@ import { PdfCandidate } from './smartpdf.classes.pdfcandidate';
declare const document;
import { IPdfResult } from './interfaces';
import * as interfaces from './interfaces';
export class SmartPdf {
htmlServerInstance: Server;
serverPort: number;
headlessBrowser: plugins.puppeteer.Browser;
externalBrowserBool: boolean = false;
private _readyDeferred: plugins.smartpromise.Deferred<void>;
private _candidates: { [key: string]: PdfCandidate } = {};
@ -18,9 +19,21 @@ export class SmartPdf {
this._readyDeferred = new plugins.smartpromise.Deferred();
}
async start() {
async start(headlessBrowserArg?) {
// lets set the external browser in case one is provided
this.headlessBrowser = headlessBrowserArg
// setup puppeteer
this.headlessBrowser = await plugins.puppeteer.launch();
if (this.headlessBrowser) {
this.externalBrowserBool = true;
} else {
let chromeArgs: string[] = [];
if(process.env.CI) {
chromeArgs = chromeArgs.concat(['--no-sandbox', '--disable-setuid-sandbox'])
}
this.headlessBrowser = await plugins.puppeteer.launch({
args: chromeArgs
});
}
// setup server
const app = plugins.express();
@ -37,19 +50,24 @@ export class SmartPdf {
});
}
// stop
async stop() {
const done = plugins.smartpromise.defer<void>();
this.htmlServerInstance.close(() => {
done.resolve();
});
if (!this.externalBrowserBool) {
await this.headlessBrowser.close();
}
await done.promise;
}
/**
* returns a pdf for a given html string;
*/
async getPdfForHtmlString(htmlStringArg: string): Promise<IPdfResult> {
async getPdfForHtmlString(htmlStringArg: string): Promise<interfaces.IPdfResult> {
await this._readyDeferred.promise;
const pdfCandidate = new PdfCandidate(htmlStringArg);
this._candidates[pdfCandidate.pdfId] = pdfCandidate;
@ -79,7 +97,7 @@ export class SmartPdf {
};
}
async getPdfForWebsite(websiteUrl: string): Promise<IPdfResult> {
async getPdfForWebsite(websiteUrl: string): Promise<interfaces.IPdfResult> {
const page = await this.headlessBrowser.newPage();
page.emulateMedia('screen');
const response = await page.goto(websiteUrl, { waitUntil: 'networkidle2' });

View File

@ -1,6 +1,3 @@
import * as plugins from './smartpdf.plugins';
export const packageDir = plugins.path.join(__dirname, '../');
export const pdfDir = plugins.path.join(packageDir, 'assets/pdfdir');
plugins.smartfile.fs.ensureDirSync(pdfDir);