Compare commits

...

15 Commits

Author SHA1 Message Date
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
bb7cb4a6ab 1.0.7 2019-05-29 14:14:03 +02:00
bda43bf5e7 fix(core): update 2019-05-29 14:14:02 +02:00
58d923b14c 1.0.6 2019-05-29 00:27:44 +02:00
e981d61a54 fix(core): update 2019-05-29 00:27:43 +02:00
12 changed files with 621 additions and 356 deletions

3
.gitignore vendored
View File

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

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

873
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{
"name": "@pushrocks/smartpdf",
"version": "1.0.5",
"version": "1.0.11",
"private": false,
"description": "create pdfs on the fly",
"main": "dist/index.js",
@ -13,21 +13,21 @@
"build": "(tsbuild)"
},
"devDependencies": {
"@gitzone/tsbuild": "^2.1.8",
"@gitzone/tsrun": "^1.2.5",
"@gitzone/tstest": "^1.0.20",
"@gitzone/tsbuild": "^2.1.11",
"@gitzone/tsrun": "^1.2.6",
"@gitzone/tstest": "^1.0.24",
"@pushrocks/tapbundle": "^3.0.9",
"@types/node": "^11.13.2"
"@types/node": "^12.0.3"
},
"dependencies": {
"@pushrocks/smartfile": "^7.0.2",
"@pushrocks/smartnetwork": "^1.1.0",
"@pushrocks/smartnetwork": "^1.1.6",
"@pushrocks/smartpromise": "^3.0.2",
"@pushrocks/smartunique": "^3.0.1",
"@types/express": "^4.16.1",
"@types/puppeteer": "^1.12.3",
"express": "^4.16.4",
"puppeteer": "^1.14.0"
"@types/puppeteer": "^1.12.4",
"express": "^4.17.1",
"puppeteer": "^1.17.0"
},
"files": [
"ts/*",

View File

@ -8,16 +8,29 @@ tap.test('should create a valid instance of smartpdf', async () => {
expect(testSmartPdf).to.be.instanceof(smartpdf.SmartPdf);
});
tap.test('should start the instance', async () => {
await testSmartPdf.start();
});
tap.test('should create a pdf from html string', async () => {
await testSmartPdf.getPdfForHtmlString('hi');
});
tap.test('should create a pdf from website', async () => {
tap.test('should create a pdf from website as A4', async () => {
await testSmartPdf.getPdfForWebsite('https://maintainedby.lossless.com');
});
tap.test('should create a pdf from website as single page PDF', async () => {
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 () => {
await testSmartPdf.close();
await testSmartPdf.stop();
});
tap.start();

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

@ -3,6 +3,10 @@ import * as paths from './smartpdf.paths';
import { Server } from 'http';
import { PdfCandidate } from './smartpdf.classes.pdfcandidate';
declare const document;
import { IPdfResult } from './interfaces';
export class SmartPdf {
htmlServerInstance: Server;
serverPort: number;
@ -12,10 +16,9 @@ export class SmartPdf {
constructor() {
this._readyDeferred = new plugins.smartpromise.Deferred();
this.init();
}
async init() {
async start() {
// setup puppeteer
this.headlessBrowser = await plugins.puppeteer.launch();
@ -34,7 +37,7 @@ export class SmartPdf {
});
}
async close() {
async stop() {
const done = plugins.smartpromise.defer<void>();
this.htmlServerInstance.close(() => {
done.resolve();
@ -46,7 +49,7 @@ export class SmartPdf {
/**
* returns a pdf for a given html string;
*/
async getPdfForHtmlString(htmlStringArg: string) {
async getPdfForHtmlString(htmlStringArg: string): Promise<IPdfResult> {
await this._readyDeferred.promise;
const pdfCandidate = new PdfCandidate(htmlStringArg);
this._candidates[pdfCandidate.pdfId] = pdfCandidate;
@ -62,27 +65,62 @@ export class SmartPdf {
console.log(`id security check passed for ${pdfCandidate.pdfId}`);
}
await page.pdf({
path: plugins.path.join(paths.pdfDir, `${pdfCandidate.pdfId}.pdf`),
const pdfBuffer = await page.pdf({
format: 'A4'
});
await page.close();
delete this._candidates[pdfCandidate.pdfId];
pdfCandidate.doneDeferred.resolve();
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();
page.emulateMedia('screen');
const response = await page.goto(websiteUrl, { waitUntil: 'networkidle2' });
const pdfId = plugins.smartunique.shortId();
await page.pdf({
path: plugins.path.join(paths.pdfDir, `${pdfId}.pdf`),
const pdfBuffer = await page.pdf({
format: 'A4',
printBackground: true,
displayHeaderFooter: false,
preferCSSPageSize: true
});
await page.close();
return {
id: pdfId,
name: `${pdfId}.js`,
buffer: pdfBuffer
};
}
async getFullWebsiteAsSinglePdf(websiteUrl: string) {
const page = await this.headlessBrowser.newPage();
page.emulateMedia('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
};
});
const pdfBuffer = await page.pdf({
height: documentWidth,
width: documentWidth,
printBackground: true,
displayHeaderFooter: false,
preferCSSPageSize: true
});
await page.close();
return {
id: pdfId,
name: `${pdfId}.js`,
buffer: pdfBuffer
};
}
}

View File

@ -13,7 +13,7 @@ import * as smartunique from '@pushrocks/smartunique';
export { smartfile, smartpromise, smartunique, smartnetwork };
// thirdparty
import * as express from 'express';
import * as puppeteer from 'puppeteer';
import express from 'express';
import puppeteer from 'puppeteer';
export { express, puppeteer };