From af725a7f78ae57f8f3bfbcb129a70cb3e2932559 Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Thu, 6 Jan 2022 13:10:12 +0100 Subject: [PATCH] fix(core): update --- test/test.ts | 4 ++-- ts/smartpdf.classes.smartpdf.ts | 29 +++++++++++++++++------------ 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/test/test.ts b/test/test.ts index bcad4c5..3d04167 100644 --- a/test/test.ts +++ b/test/test.ts @@ -13,11 +13,11 @@ tap.test('should start the instance', async () => { }); tap.test('should create a pdf from html string', async () => { - await testSmartPdf.getPdfResultForHtmlString('hi'); + await testSmartPdf.getA4PdfResultForHtmlString('hi'); }); tap.test('should create a pdf from html string', async () => { - await testSmartPdf.getPdfResultForHtmlString('hi'); + await testSmartPdf.getA4PdfResultForHtmlString('hi'); }); tap.test('should create a pdf from website as A4', async () => { diff --git a/ts/smartpdf.classes.smartpdf.ts b/ts/smartpdf.classes.smartpdf.ts index 05c3839..7cb5dde 100644 --- a/ts/smartpdf.classes.smartpdf.ts +++ b/ts/smartpdf.classes.smartpdf.ts @@ -63,7 +63,7 @@ export class SmartPdf { /** * returns a pdf for a given html string; */ - async getPdfResultForHtmlString(htmlStringArg: string): Promise { + async getA4PdfResultForHtmlString(htmlStringArg: string): Promise { await this._readyDeferred.promise; const pdfCandidate = new PdfCandidate(htmlStringArg); this._candidates[pdfCandidate.pdfId] = pdfCandidate; @@ -84,10 +84,10 @@ export class SmartPdf { } const pdfBuffer = await page.pdf({ - format: 'a4', + width: 794, + height: 1122, printBackground: true, displayHeaderFooter: false, - preferCSSPageSize: true, }); await page.close(); delete this._candidates[pdfCandidate.pdfId]; @@ -105,6 +105,10 @@ export class SmartPdf { async getPdfResultForWebsite(websiteUrl: string): Promise { const page = await this.headlessBrowser.newPage(); + await page.setViewport({ + width: 1980, + height: 1200, + }); await page.emulateMediaType('screen'); const response = await page.goto(websiteUrl, { waitUntil: 'networkidle2' }); const pdfId = plugins.smartunique.shortId(); @@ -115,12 +119,10 @@ export class SmartPdf { }; }); const pdfBuffer = await page.pdf({ - format: 'a4', - height: documentWidth, + height: documentHeight, width: documentWidth, printBackground: true, displayHeaderFooter: false, - preferCSSPageSize: true, }); await page.close(); return { @@ -135,22 +137,25 @@ export class SmartPdf { async getFullWebsiteAsSinglePdf(websiteUrl: string): Promise { const page = await this.headlessBrowser.newPage(); + await page.setViewport({ + width: 1920, + height: 1200, + }); page.emulateMediaType('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, + documentHeight: document.body.scrollHeight, + documentWidth: document.body.clientWidth, }; }); const pdfBuffer = await page.pdf({ - format: 'a4', - height: documentWidth, - width: documentWidth, + height: documentHeight, + width: 1920, printBackground: true, displayHeaderFooter: false, - preferCSSPageSize: true, + scale: 1 }); await page.close(); return {