fix(core): update
This commit is contained in:
parent
d8fd7f9956
commit
5e8abaa5b4
@ -24,6 +24,11 @@ tap.test('should create a pdf from website as single page PDF', async () => {
|
|||||||
await testSmartPdf.getFullWebsiteAsSinglePdf('https://maintainedby.lossless.com');
|
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 () => {
|
tap.test('should be able to close properly', async () => {
|
||||||
await testSmartPdf.stop();
|
await testSmartPdf.stop();
|
||||||
});
|
});
|
||||||
|
@ -5,6 +5,12 @@ import { PdfCandidate } from './smartpdf.classes.pdfcandidate';
|
|||||||
|
|
||||||
declare const document;
|
declare const document;
|
||||||
|
|
||||||
|
export interface IPdfResult {
|
||||||
|
name: string,
|
||||||
|
id: string,
|
||||||
|
buffer: Buffer;
|
||||||
|
}
|
||||||
|
|
||||||
export class SmartPdf {
|
export class SmartPdf {
|
||||||
htmlServerInstance: Server;
|
htmlServerInstance: Server;
|
||||||
serverPort: number;
|
serverPort: number;
|
||||||
@ -47,7 +53,7 @@ export class SmartPdf {
|
|||||||
/**
|
/**
|
||||||
* returns a pdf for a given html string;
|
* returns a pdf for a given html string;
|
||||||
*/
|
*/
|
||||||
async getPdfForHtmlString(htmlStringArg: string) {
|
async getPdfForHtmlString(htmlStringArg: string): Promise<IPdfResult> {
|
||||||
await this._readyDeferred.promise;
|
await this._readyDeferred.promise;
|
||||||
const pdfCandidate = new PdfCandidate(htmlStringArg);
|
const pdfCandidate = new PdfCandidate(htmlStringArg);
|
||||||
this._candidates[pdfCandidate.pdfId] = pdfCandidate;
|
this._candidates[pdfCandidate.pdfId] = pdfCandidate;
|
||||||
@ -63,29 +69,37 @@ export class SmartPdf {
|
|||||||
console.log(`id security check passed for ${pdfCandidate.pdfId}`);
|
console.log(`id security check passed for ${pdfCandidate.pdfId}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
await page.pdf({
|
const pdfBuffer = await page.pdf({
|
||||||
path: plugins.path.join(paths.pdfDir, `${pdfCandidate.pdfId}.pdf`),
|
|
||||||
format: 'A4'
|
format: 'A4'
|
||||||
});
|
});
|
||||||
await page.close();
|
await page.close();
|
||||||
delete this._candidates[pdfCandidate.pdfId];
|
delete this._candidates[pdfCandidate.pdfId];
|
||||||
pdfCandidate.doneDeferred.resolve();
|
pdfCandidate.doneDeferred.resolve();
|
||||||
await pdfCandidate.doneDeferred.promise;
|
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();
|
const page = await this.headlessBrowser.newPage();
|
||||||
page.emulateMedia('screen');
|
page.emulateMedia('screen');
|
||||||
const response = await page.goto(websiteUrl, { waitUntil: 'networkidle2' });
|
const response = await page.goto(websiteUrl, { waitUntil: 'networkidle2' });
|
||||||
const pdfId = plugins.smartunique.shortId();
|
const pdfId = plugins.smartunique.shortId();
|
||||||
await page.pdf({
|
const pdfBuffer = await page.pdf({
|
||||||
path: plugins.path.join(paths.pdfDir, `${pdfId}.pdf`),
|
|
||||||
format: 'A4',
|
format: 'A4',
|
||||||
printBackground: true,
|
printBackground: true,
|
||||||
displayHeaderFooter: false,
|
displayHeaderFooter: false,
|
||||||
preferCSSPageSize: true
|
preferCSSPageSize: true
|
||||||
});
|
});
|
||||||
await page.close();
|
await page.close();
|
||||||
|
return {
|
||||||
|
id: pdfId,
|
||||||
|
name: `${pdfId}.js`,
|
||||||
|
buffer: pdfBuffer
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async getFullWebsiteAsSinglePdf(websiteUrl: string) {
|
async getFullWebsiteAsSinglePdf(websiteUrl: string) {
|
||||||
@ -100,8 +114,7 @@ export class SmartPdf {
|
|||||||
documentWidth: document.width
|
documentWidth: document.width
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
await page.pdf({
|
const pdfBuffer = await page.pdf({
|
||||||
path: plugins.path.join(paths.pdfDir, `${pdfId}.pdf`),
|
|
||||||
height: documentWidth,
|
height: documentWidth,
|
||||||
width: documentWidth,
|
width: documentWidth,
|
||||||
printBackground: true,
|
printBackground: true,
|
||||||
@ -109,5 +122,10 @@ export class SmartPdf {
|
|||||||
preferCSSPageSize: true
|
preferCSSPageSize: true
|
||||||
});
|
});
|
||||||
await page.close();
|
await page.close();
|
||||||
|
return {
|
||||||
|
id: pdfId,
|
||||||
|
name: `${pdfId}.js`,
|
||||||
|
buffer: pdfBuffer
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user