Compare commits
18 Commits
Author | SHA1 | Date | |
---|---|---|---|
377318a62a | |||
671c871304 | |||
e0cc6b5655 | |||
e74b44b49c | |||
d6f0d88d4a | |||
9674e5b8dc | |||
ea4cf777a9 | |||
8a308fa9e3 | |||
56fa53b701 | |||
2a4ddd4e41 | |||
3d8a63fddd | |||
9d9b1d0399 | |||
739d60d410 | |||
9003034d0d | |||
5002513d21 | |||
12ede2be02 | |||
163ec2bd50 | |||
7744839613 |
22786
package-lock.json
generated
22786
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
30
package.json
30
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@pushrocks/smartpdf",
|
||||
"version": "2.0.4",
|
||||
"version": "2.0.13",
|
||||
"private": false,
|
||||
"description": "create pdfs on the fly",
|
||||
"main": "dist_ts/index.js",
|
||||
@ -8,27 +8,27 @@
|
||||
"author": "Lossless GmbH",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"test": "(tstest test/)",
|
||||
"format": "(gitzone format)",
|
||||
"build": "(tsbuild)"
|
||||
"test": "(tstest test/ --web)",
|
||||
"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.31",
|
||||
"@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.8",
|
||||
"@pushrocks/smartnetwork": "^1.1.22",
|
||||
"@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/**/*",
|
||||
|
@ -29,7 +29,6 @@ Platform support | [ or [contribute monthly](https://lossless.link/contribute). :)
|
||||
|
||||
|
||||
## Contribution
|
||||
|
||||
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
|
||||
|
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
|
||||
@ -39,7 +39,7 @@ export class SmartPdf {
|
||||
});
|
||||
this.htmlServerInstance = plugins.http.createServer(app);
|
||||
const smartnetworkInstance = new plugins.smartnetwork.SmartNetwork();
|
||||
const portAvailable = smartnetworkInstance.isLocalPortAvailable(3210);
|
||||
const portAvailable = smartnetworkInstance.isLocalPortUnused(3210);
|
||||
this.htmlServerInstance.listen(3210, 'localhost');
|
||||
this.htmlServerInstance.on('listening', () => {
|
||||
this._readyDeferred.resolve();
|
||||
@ -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