2018-10-06 13:25:45 +00:00
|
|
|
import * as plugins from './smartpdf.plugins';
|
|
|
|
import * as paths from './smartpdf.paths';
|
|
|
|
import { Server } from 'http';
|
|
|
|
import { PdfCandidate } from './smartpdf.classes.pdfcandidate';
|
|
|
|
|
2019-05-28 22:27:43 +00:00
|
|
|
declare const document;
|
|
|
|
|
2019-06-03 08:51:15 +00:00
|
|
|
import { IPdfResult } from './interfaces';
|
2019-05-29 17:49:23 +00:00
|
|
|
|
2018-10-06 13:25:45 +00:00
|
|
|
export class SmartPdf {
|
|
|
|
htmlServerInstance: Server;
|
|
|
|
serverPort: number;
|
|
|
|
headlessBrowser: plugins.puppeteer.Browser;
|
2019-06-03 11:00:06 +00:00
|
|
|
externalBrowser: boolean = false;
|
2018-10-06 13:25:45 +00:00
|
|
|
private _readyDeferred: plugins.smartpromise.Deferred<void>;
|
2018-10-06 15:35:26 +00:00
|
|
|
private _candidates: { [key: string]: PdfCandidate } = {};
|
2018-10-06 13:25:45 +00:00
|
|
|
|
2019-06-03 11:00:06 +00:00
|
|
|
constructor(headlessBrowserArg?) {
|
|
|
|
this.headlessBrowser = headlessBrowserArg
|
2018-10-06 13:25:45 +00:00
|
|
|
this._readyDeferred = new plugins.smartpromise.Deferred();
|
|
|
|
}
|
|
|
|
|
2019-05-29 12:14:02 +00:00
|
|
|
async start() {
|
2018-10-06 13:25:45 +00:00
|
|
|
// setup puppeteer
|
2019-06-03 11:00:06 +00:00
|
|
|
if (!this.headlessBrowser) {
|
|
|
|
this.headlessBrowser = await plugins.puppeteer.launch();
|
|
|
|
} else {
|
|
|
|
this.externalBrowser = true;
|
|
|
|
}
|
2018-10-06 13:25:45 +00:00
|
|
|
|
|
|
|
// 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();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-29 12:14:02 +00:00
|
|
|
async stop() {
|
2018-10-06 15:35:26 +00:00
|
|
|
const done = plugins.smartpromise.defer<void>();
|
|
|
|
this.htmlServerInstance.close(() => {
|
|
|
|
done.resolve();
|
|
|
|
});
|
|
|
|
await this.headlessBrowser.close();
|
|
|
|
await done.promise;
|
2019-05-28 21:57:50 +00:00
|
|
|
}
|
2018-10-06 15:35:26 +00:00
|
|
|
|
2018-10-06 13:25:45 +00:00
|
|
|
/**
|
|
|
|
* returns a pdf for a given html string;
|
|
|
|
*/
|
2019-05-29 17:49:23 +00:00
|
|
|
async getPdfForHtmlString(htmlStringArg: string): Promise<IPdfResult> {
|
2018-10-06 13:25:45 +00:00
|
|
|
await this._readyDeferred.promise;
|
|
|
|
const pdfCandidate = new PdfCandidate(htmlStringArg);
|
|
|
|
this._candidates[pdfCandidate.pdfId] = pdfCandidate;
|
|
|
|
const page = await this.headlessBrowser.newPage();
|
2018-10-06 15:35:26 +00:00
|
|
|
const response = await page.goto(`http://localhost:3210/${pdfCandidate.pdfId}`, {
|
|
|
|
waitUntil: 'networkidle2'
|
|
|
|
});
|
2018-10-06 13:25:45 +00:00
|
|
|
const headers = response.headers();
|
2018-10-06 15:35:26 +00:00
|
|
|
if (headers['pdf-id'] !== pdfCandidate.pdfId) {
|
2018-10-06 13:25:45 +00:00
|
|
|
console.log('Error! Headers do not match. For security reasons no pdf is being emitted!');
|
2018-10-06 15:35:26 +00:00
|
|
|
return;
|
2018-10-06 13:25:45 +00:00
|
|
|
} else {
|
|
|
|
console.log(`id security check passed for ${pdfCandidate.pdfId}`);
|
|
|
|
}
|
2018-10-06 15:35:26 +00:00
|
|
|
|
2019-05-29 17:49:23 +00:00
|
|
|
const pdfBuffer = await page.pdf({
|
2018-10-06 13:25:45 +00:00
|
|
|
format: 'A4'
|
|
|
|
});
|
|
|
|
await page.close();
|
|
|
|
delete this._candidates[pdfCandidate.pdfId];
|
|
|
|
pdfCandidate.doneDeferred.resolve();
|
|
|
|
await pdfCandidate.doneDeferred.promise;
|
2019-05-29 17:49:23 +00:00
|
|
|
return {
|
|
|
|
id: pdfCandidate.pdfId,
|
|
|
|
name: `${pdfCandidate.pdfId}.js`,
|
|
|
|
buffer: pdfBuffer
|
2019-06-03 08:51:15 +00:00
|
|
|
};
|
2018-10-06 13:25:45 +00:00
|
|
|
}
|
|
|
|
|
2019-05-29 17:49:23 +00:00
|
|
|
async getPdfForWebsite(websiteUrl: string): Promise<IPdfResult> {
|
2018-10-06 13:25:45 +00:00
|
|
|
const page = await this.headlessBrowser.newPage();
|
2019-05-29 17:17:39 +00:00
|
|
|
page.emulateMedia('screen');
|
2018-10-06 13:25:45 +00:00
|
|
|
const response = await page.goto(websiteUrl, { waitUntil: 'networkidle2' });
|
|
|
|
const pdfId = plugins.smartunique.shortId();
|
2019-05-29 17:49:23 +00:00
|
|
|
const pdfBuffer = await page.pdf({
|
2019-04-10 13:12:54 +00:00
|
|
|
format: 'A4',
|
|
|
|
printBackground: true,
|
|
|
|
displayHeaderFooter: false,
|
|
|
|
preferCSSPageSize: true
|
2018-10-06 13:25:45 +00:00
|
|
|
});
|
|
|
|
await page.close();
|
2019-05-29 17:49:23 +00:00
|
|
|
return {
|
|
|
|
id: pdfId,
|
|
|
|
name: `${pdfId}.js`,
|
|
|
|
buffer: pdfBuffer
|
2019-06-03 08:51:15 +00:00
|
|
|
};
|
2018-10-06 13:25:45 +00:00
|
|
|
}
|
2019-05-28 22:27:43 +00:00
|
|
|
|
|
|
|
async getFullWebsiteAsSinglePdf(websiteUrl: string) {
|
|
|
|
const page = await this.headlessBrowser.newPage();
|
2019-05-29 17:17:39 +00:00
|
|
|
page.emulateMedia('screen');
|
2019-05-28 22:27:43 +00:00
|
|
|
const response = await page.goto(websiteUrl, { waitUntil: 'networkidle2' });
|
|
|
|
const pdfId = plugins.smartunique.shortId();
|
2019-06-03 08:51:15 +00:00
|
|
|
const { documentHeight, documentWidth } = await page.evaluate(() => {
|
2019-05-28 22:27:43 +00:00
|
|
|
return {
|
|
|
|
documentHeight: document.height,
|
|
|
|
documentWidth: document.width
|
|
|
|
};
|
|
|
|
});
|
2019-05-29 17:49:23 +00:00
|
|
|
const pdfBuffer = await page.pdf({
|
2019-05-28 22:27:43 +00:00
|
|
|
height: documentWidth,
|
|
|
|
width: documentWidth,
|
|
|
|
printBackground: true,
|
|
|
|
displayHeaderFooter: false,
|
|
|
|
preferCSSPageSize: true
|
|
|
|
});
|
|
|
|
await page.close();
|
2019-05-29 17:49:23 +00:00
|
|
|
return {
|
|
|
|
id: pdfId,
|
|
|
|
name: `${pdfId}.js`,
|
|
|
|
buffer: pdfBuffer
|
2019-06-03 08:51:15 +00:00
|
|
|
};
|
2019-05-28 22:27:43 +00:00
|
|
|
}
|
2018-10-06 13:25:45 +00:00
|
|
|
}
|