initial implementation

This commit is contained in:
Phil Kunz
2018-10-06 13:25:45 +00:00
parent 2d70a4e229
commit f2766ab639
13 changed files with 1916 additions and 0 deletions

1
ts/index.ts Normal file
View File

@@ -0,0 +1 @@
export * from './smartpdf.classes.smartpdf';

View File

@@ -0,0 +1,8 @@
import * as plugins from './smartpdf.plugins';
export class PdfCandidate {
pdfId = plugins.smartunique.shortId();
doneDeferred = plugins.smartpromise.defer();
constructor(public htmlString) {}
}

View File

@@ -0,0 +1 @@
import * as plugins from './smartpdf.plugins';

View File

@@ -0,0 +1,74 @@
import * as plugins from './smartpdf.plugins';
import * as paths from './smartpdf.paths';
import { Server } from 'http';
import { PdfCandidate } from './smartpdf.classes.pdfcandidate';
export class SmartPdf {
htmlServerInstance: Server;
serverPort: number;
headlessBrowser: plugins.puppeteer.Browser;
private _readyDeferred: plugins.smartpromise.Deferred<void>;
private _candidates: {[key: string]: PdfCandidate} = {};
constructor() {
this._readyDeferred = new plugins.smartpromise.Deferred();
this.init();
}
async init() {
// setup puppeteer
this.headlessBrowser = await plugins.puppeteer.launch();
// setup server
const app = plugins.express();
app.get('/:pdfId', (req, res) => {
res.setHeader('PDF-ID', this._candidates[req.params.pdfId].pdfId);
res.send(this._candidates[req.params.pdfId].htmlString);
});
this.htmlServerInstance = plugins.http.createServer(app);
const smartnetworkInstance = new plugins.smartnetwork.SmartNetwork();
const portAvailable = smartnetworkInstance.isLocalPortAvailable(3210);
this.htmlServerInstance.listen(3210, 'localhost');
this.htmlServerInstance.on('listening', () => {
this._readyDeferred.resolve();
});
}
/**
* returns a pdf for a given html string;
*/
async getPdfForHtmlString(htmlStringArg: string) {
await this._readyDeferred.promise;
const pdfCandidate = new PdfCandidate(htmlStringArg);
this._candidates[pdfCandidate.pdfId] = pdfCandidate;
const page = await this.headlessBrowser.newPage();
const response = await page.goto(`http://localhost:3210/${pdfCandidate.pdfId}`, { waitUntil: 'networkidle2' });
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!');
return
} else {
console.log(`id security check passed for ${pdfCandidate.pdfId}`);
}
await page.pdf({
path: plugins.path.join(paths.pdfDir, `${pdfCandidate.pdfId}.pdf`),
format: 'A4'
});
await page.close();
delete this._candidates[pdfCandidate.pdfId];
pdfCandidate.doneDeferred.resolve();
await pdfCandidate.doneDeferred.promise;
}
async getPdfForWebsite(websiteUrl: string) {
const page = await this.headlessBrowser.newPage();
const response = await page.goto(websiteUrl, { waitUntil: 'networkidle2' });
const pdfId = plugins.smartunique.shortId();
await page.pdf({
path: plugins.path.join(paths.pdfDir, `${pdfId}.pdf`),
format: 'A4'
});
await page.close();
}
}

6
ts/smartpdf.paths.ts Normal file
View File

@@ -0,0 +1,6 @@
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);

30
ts/smartpdf.plugins.ts Normal file
View File

@@ -0,0 +1,30 @@
// native
import * as http from 'http';
import * as path from 'path';
export {
http,
path
}
// @pushrocks
import * as smartfile from '@pushrocks/smartfile';
import * as smartpromise from '@pushrocks/smartpromise';
import * as smartnetwork from '@pushrocks/smartnetwork';
import * as smartunique from 'smartunique';
export {
smartfile,
smartpromise,
smartunique,
smartnetwork
}
// thirdparty
import * as express from 'express';
import * as puppeteer from 'puppeteer';
export {
express,
puppeteer
}