Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
377318a62a | |||
671c871304 | |||
e0cc6b5655 | |||
e74b44b49c | |||
d6f0d88d4a | |||
9674e5b8dc | |||
ea4cf777a9 | |||
8a308fa9e3 | |||
56fa53b701 | |||
2a4ddd4e41 | |||
3d8a63fddd | |||
9d9b1d0399 |
23789
package-lock.json
generated
23789
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
25
package.json
25
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@pushrocks/smartpdf",
|
||||
"version": "2.0.7",
|
||||
"version": "2.0.13",
|
||||
"private": false,
|
||||
"description": "create pdfs on the fly",
|
||||
"main": "dist_ts/index.js",
|
||||
@ -12,22 +12,23 @@
|
||||
"build": "(tsbuild --web)"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.1.25",
|
||||
"@gitzone/tsrun": "^1.2.12",
|
||||
"@gitzone/tstest": "^1.0.52",
|
||||
"@pushrocks/tapbundle": "^3.2.14",
|
||||
"@types/node": "^14.14.37",
|
||||
"@gitzone/tsbuild": "^2.1.28",
|
||||
"@gitzone/tsrun": "^1.2.18",
|
||||
"@gitzone/tstest": "^1.0.60",
|
||||
"@pushrocks/tapbundle": "^3.2.15",
|
||||
"@types/node": "^17.0.8",
|
||||
"tslint": "^6.1.3",
|
||||
"tslint-config-prettier": "^1.18.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@pushrocks/smartfile": "^8.0.9",
|
||||
"@pushrocks/smartnetwork": "^2.0.4",
|
||||
"@pushrocks/smartpromise": "^3.1.3",
|
||||
"@pushrocks/smartpuppeteer": "^1.0.21",
|
||||
"@pushrocks/smartfile": "^9.0.5",
|
||||
"@pushrocks/smartnetwork": "^2.0.10",
|
||||
"@pushrocks/smartpromise": "^3.1.6",
|
||||
"@pushrocks/smartpuppeteer": "^1.0.36",
|
||||
"@pushrocks/smartunique": "^3.0.3",
|
||||
"@types/express": "^4.17.11",
|
||||
"express": "^4.17.1"
|
||||
"@types/express": "^4.17.13",
|
||||
"express": "^4.17.2",
|
||||
"pdf-merger-js": "^3.2.1"
|
||||
},
|
||||
"files": [
|
||||
"ts/**/*",
|
||||
|
28
test/test.ts
28
test/test.ts
@ -25,18 +25,28 @@ tap.test('should create a pdf from website as single page PDF', async () => {
|
||||
});
|
||||
|
||||
tap.test('should create a valid PDFResult', async () => {
|
||||
const pdfResult = await testSmartPdf.getFullWebsiteAsSinglePdf(
|
||||
'https://maintainedby.lossless.com'
|
||||
);
|
||||
expect(pdfResult.buffer).to.be.instanceOf(Buffer);
|
||||
const fs = await import('fs');
|
||||
const writePDfToDisk = async (urlArg: string, fileName: string) => {
|
||||
const pdfResult = await testSmartPdf.getFullWebsiteAsSinglePdf(urlArg);
|
||||
expect(pdfResult.buffer).to.be.instanceOf(Buffer);
|
||||
const fs = await import('fs');
|
||||
|
||||
if (!fs.existsSync('.nogit/')) {
|
||||
fs.mkdirSync('.nogit/');
|
||||
}
|
||||
fs.writeFileSync('.nogit/sample.pdf', pdfResult.buffer);
|
||||
if (!fs.existsSync('.nogit/')) {
|
||||
fs.mkdirSync('.nogit/');
|
||||
}
|
||||
fs.writeFileSync(`.nogit/${fileName}`, pdfResult.buffer);
|
||||
};
|
||||
await writePDfToDisk('https://maintainedby.lossless.com/', '1.pdf')
|
||||
await writePDfToDisk('https://lossless.com/', '2.pdf')
|
||||
});
|
||||
|
||||
tap.test('should combine pdfs', async () => {
|
||||
const fs = await import('fs');
|
||||
const buffer1 = fs.readFileSync('.nogit/1.pdf');
|
||||
const buffer2 = fs.readFileSync('.nogit/2.pdf');
|
||||
fs.writeFileSync(`.nogit/combined.pdf`, await testSmartPdf.mergePdfBuffers([buffer1, buffer2]));
|
||||
|
||||
})
|
||||
|
||||
tap.test('should be able to close properly', async () => {
|
||||
await testSmartPdf.stop();
|
||||
});
|
||||
|
@ -1,8 +1,11 @@
|
||||
import * as plugins from './smartpdf.plugins';
|
||||
|
||||
export class PdfCandidate {
|
||||
pdfId = plugins.smartunique.shortId();
|
||||
doneDeferred = plugins.smartpromise.defer();
|
||||
public htmlString: string;
|
||||
public pdfId = plugins.smartunique.shortId();
|
||||
public doneDeferred = plugins.smartpromise.defer();
|
||||
|
||||
constructor(public htmlString) {}
|
||||
constructor(htmlStringArg: string) {
|
||||
this.htmlString = htmlStringArg;
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ import * as paths from './smartpdf.paths';
|
||||
import { Server } from 'http';
|
||||
import { PdfCandidate } from './smartpdf.classes.pdfcandidate';
|
||||
|
||||
declare const document;
|
||||
declare const document: any;
|
||||
|
||||
import * as interfaces from './interfaces';
|
||||
|
||||
@ -19,7 +19,7 @@ export class SmartPdf {
|
||||
this._readyDeferred = new plugins.smartpromise.Deferred();
|
||||
}
|
||||
|
||||
async start(headlessBrowserArg?) {
|
||||
async start(headlessBrowserArg?: plugins.smartpuppeteer.puppeteer.Browser) {
|
||||
// lets set the external browser in case one is provided
|
||||
this.headlessBrowser = headlessBrowserArg;
|
||||
// setup puppeteer
|
||||
@ -68,6 +68,10 @@ export class SmartPdf {
|
||||
const pdfCandidate = new PdfCandidate(htmlStringArg);
|
||||
this._candidates[pdfCandidate.pdfId] = pdfCandidate;
|
||||
const page = await this.headlessBrowser.newPage();
|
||||
await page.setViewport({
|
||||
height: 842,
|
||||
width: 595,
|
||||
})
|
||||
const response = await page.goto(`http://localhost:3210/${pdfCandidate.pdfId}`, {
|
||||
waitUntil: 'networkidle2',
|
||||
});
|
||||
@ -80,7 +84,7 @@ export class SmartPdf {
|
||||
}
|
||||
|
||||
const pdfBuffer = await page.pdf({
|
||||
format: 'A4',
|
||||
format: 'a4',
|
||||
printBackground: true,
|
||||
displayHeaderFooter: false,
|
||||
preferCSSPageSize: true,
|
||||
@ -108,7 +112,7 @@ export class SmartPdf {
|
||||
};
|
||||
});
|
||||
const pdfBuffer = await page.pdf({
|
||||
format: 'A4',
|
||||
format: 'a4',
|
||||
height: documentWidth,
|
||||
width: documentWidth,
|
||||
printBackground: true,
|
||||
@ -135,7 +139,7 @@ export class SmartPdf {
|
||||
};
|
||||
});
|
||||
const pdfBuffer = await page.pdf({
|
||||
format: 'A4',
|
||||
format: 'a4',
|
||||
height: documentWidth,
|
||||
width: documentWidth,
|
||||
printBackground: true,
|
||||
@ -149,4 +153,12 @@ export class SmartPdf {
|
||||
buffer: pdfBuffer,
|
||||
};
|
||||
}
|
||||
|
||||
public async mergePdfBuffers(pdfBuffers: Buffer[]): Promise<Buffer> {
|
||||
const merger = new plugins.pdfMerger();
|
||||
for (const buffer of pdfBuffers) {
|
||||
merger.add(buffer);
|
||||
}
|
||||
return merger.saveAsBuffer();
|
||||
}
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ import * as smartunique from '@pushrocks/smartunique';
|
||||
export { smartfile, smartpromise, smartpuppeteer, smartunique, smartnetwork };
|
||||
|
||||
// thirdparty
|
||||
import pdfMerger from 'pdf-merger-js';
|
||||
import express from 'express';
|
||||
|
||||
export { express };
|
||||
export { pdfMerger, express };
|
||||
|
Reference in New Issue
Block a user