|
|
|
@ -2,12 +2,18 @@ import * as plugins from './smartpdf.plugins.js';
|
|
|
|
|
import * as paths from './smartpdf.paths.js';
|
|
|
|
|
import { Server } from 'http';
|
|
|
|
|
import { PdfCandidate } from './smartpdf.classes.pdfcandidate.js';
|
|
|
|
|
import { IPdf } from '@tsclass/tsclass/dist_ts/business/pdf.js';
|
|
|
|
|
|
|
|
|
|
declare const document: any;
|
|
|
|
|
|
|
|
|
|
import * as interfaces from './interfaces/index.js';
|
|
|
|
|
|
|
|
|
|
export class SmartPdf {
|
|
|
|
|
// STATIC
|
|
|
|
|
public static async create() {
|
|
|
|
|
const smartpdfInstance = new SmartPdf();
|
|
|
|
|
return smartpdfInstance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// INSTANCE
|
|
|
|
|
htmlServerInstance: Server;
|
|
|
|
|
serverPort: number;
|
|
|
|
|
headlessBrowser: plugins.smartpuppeteer.puppeteer.Browser;
|
|
|
|
@ -63,7 +69,7 @@ export class SmartPdf {
|
|
|
|
|
/**
|
|
|
|
|
* returns a pdf for a given html string;
|
|
|
|
|
*/
|
|
|
|
|
async getA4PdfResultForHtmlString(htmlStringArg: string): Promise<interfaces.IPdfResult> {
|
|
|
|
|
async getA4PdfResultForHtmlString(htmlStringArg: string): Promise<plugins.tsclass.business.IPdf> {
|
|
|
|
|
await this._readyDeferred.promise;
|
|
|
|
|
const pdfCandidate = new PdfCandidate(htmlStringArg);
|
|
|
|
|
this._candidates[pdfCandidate.pdfId] = pdfCandidate;
|
|
|
|
@ -103,7 +109,7 @@ export class SmartPdf {
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getPdfResultForWebsite(websiteUrl: string): Promise<interfaces.IPdfResult> {
|
|
|
|
|
async getPdfResultForWebsite(websiteUrl: string): Promise<plugins.tsclass.business.IPdf> {
|
|
|
|
|
const page = await this.headlessBrowser.newPage();
|
|
|
|
|
await page.setViewport({
|
|
|
|
|
width: 1980,
|
|
|
|
@ -135,7 +141,7 @@ export class SmartPdf {
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getFullWebsiteAsSinglePdf(websiteUrl: string): Promise<interfaces.IPdfResult> {
|
|
|
|
|
async getFullWebsiteAsSinglePdf(websiteUrl: string): Promise<plugins.tsclass.business.IPdf> {
|
|
|
|
|
const page = await this.headlessBrowser.newPage();
|
|
|
|
|
await page.setViewport({
|
|
|
|
|
width: 1920,
|
|
|
|
@ -160,7 +166,7 @@ export class SmartPdf {
|
|
|
|
|
printBackground: true,
|
|
|
|
|
displayHeaderFooter: false,
|
|
|
|
|
scale: 1,
|
|
|
|
|
pageRanges: '1'
|
|
|
|
|
pageRanges: '1',
|
|
|
|
|
});
|
|
|
|
|
await page.close();
|
|
|
|
|
return {
|
|
|
|
@ -173,26 +179,43 @@ export class SmartPdf {
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async mergePdfBuffers(pdfBuffers: Buffer[]): Promise<Buffer> {
|
|
|
|
|
public async mergePdfs(pdfArrayArg: plugins.tsclass.business.IPdf[]): Promise<IPdf> {
|
|
|
|
|
const merger = new plugins.pdfMerger();
|
|
|
|
|
for (const buffer of pdfBuffers) {
|
|
|
|
|
merger.add(buffer);
|
|
|
|
|
for (const pdf of pdfArrayArg) {
|
|
|
|
|
merger.add(pdf.buffer as Buffer);
|
|
|
|
|
}
|
|
|
|
|
const resultBuffer = await merger.saveAsBuffer();
|
|
|
|
|
return {
|
|
|
|
|
name: 'mergedPdf',
|
|
|
|
|
buffer: resultBuffer,
|
|
|
|
|
id: null,
|
|
|
|
|
metadata: null
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async readFileToPdfObject(pathArg: string): Promise<plugins.tsclass.business.IPdf> {
|
|
|
|
|
const path = plugins.smartpath.transform.makeAbsolute(pathArg);
|
|
|
|
|
const parsedPath = plugins.path.parse(path);
|
|
|
|
|
const buffer = await plugins.smartfile.fs.toBuffer(path);
|
|
|
|
|
return {
|
|
|
|
|
name: parsedPath.base,
|
|
|
|
|
buffer,
|
|
|
|
|
id: null,
|
|
|
|
|
metadata: null
|
|
|
|
|
}
|
|
|
|
|
return merger.saveAsBuffer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async extractTextFromPdfBuffer(pdfBufferArg: Buffer): Promise<string> {
|
|
|
|
|
const deferred = plugins.smartpromise.defer<string>();
|
|
|
|
|
const pdfParser: any = new plugins.pdf2json();
|
|
|
|
|
pdfParser.on('pdfParser_dataReady', (pdfData: any) => {
|
|
|
|
|
let finalText = ''
|
|
|
|
|
let finalText = '';
|
|
|
|
|
for (const page of pdfData.Pages) {
|
|
|
|
|
for(const text of page.Texts) {
|
|
|
|
|
for (const text of page.Texts) {
|
|
|
|
|
for (const letter of text.R) {
|
|
|
|
|
finalText = finalText + letter.T;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
deferred.resolve(finalText);
|
|
|
|
|
});
|
|
|
|
|