feat(smartpdf): Improve dependency versions and optimize PDF to PNG conversion.
This commit is contained in:
87
test/test.ts
87
test/test.ts
@ -1,66 +1,85 @@
|
||||
import { expect, tap } from '@push.rocks/tapbundle';
|
||||
import * as smartpdf from '../ts/index.js';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
|
||||
let testSmartPdf: smartpdf.SmartPdf;
|
||||
|
||||
tap.test('should create a valid instance of smartpdf', async () => {
|
||||
/**
|
||||
* Ensures that a directory exists.
|
||||
* @param dirPath - The directory path to ensure.
|
||||
*/
|
||||
function ensureDir(dirPath: string): void {
|
||||
if (!fs.existsSync(dirPath)) {
|
||||
fs.mkdirSync(dirPath, { recursive: true });
|
||||
}
|
||||
}
|
||||
|
||||
tap.test('should create a valid instance of SmartPdf', async () => {
|
||||
testSmartPdf = new smartpdf.SmartPdf();
|
||||
expect(testSmartPdf).toBeInstanceOf(smartpdf.SmartPdf);
|
||||
});
|
||||
|
||||
tap.test('should start the instance', async () => {
|
||||
tap.test('should start the SmartPdf instance', async () => {
|
||||
await testSmartPdf.start();
|
||||
});
|
||||
|
||||
tap.test('should create a pdf from html string', async () => {
|
||||
await testSmartPdf.getA4PdfResultForHtmlString('hi');
|
||||
tap.test('should create PDFs from HTML string', async () => {
|
||||
const pdf1 = await testSmartPdf.getA4PdfResultForHtmlString('hi');
|
||||
const pdf2 = await testSmartPdf.getA4PdfResultForHtmlString('hello');
|
||||
expect(pdf1.buffer).toBeInstanceOf(Buffer);
|
||||
expect(pdf2.buffer).toBeInstanceOf(Buffer);
|
||||
});
|
||||
|
||||
tap.test('should create a pdf from html string', async () => {
|
||||
await testSmartPdf.getA4PdfResultForHtmlString('hi');
|
||||
tap.test('should create PDFs from websites', async () => {
|
||||
const pdfA4 = await testSmartPdf.getPdfResultForWebsite('https://www.wikipedia.org');
|
||||
const pdfSingle = await testSmartPdf.getFullWebsiteAsSinglePdf('https://www.wikipedia.org');
|
||||
expect(pdfA4.buffer).toBeInstanceOf(Buffer);
|
||||
expect(pdfSingle.buffer).toBeInstanceOf(Buffer);
|
||||
});
|
||||
|
||||
tap.test('should create a pdf from website as A4', async () => {
|
||||
await testSmartPdf.getPdfResultForWebsite('https://www.wikipedia.org');
|
||||
});
|
||||
|
||||
tap.test('should create a pdf from website as single page PDF', async () => {
|
||||
await testSmartPdf.getFullWebsiteAsSinglePdf('https://www.wikipedia.org');
|
||||
});
|
||||
|
||||
tap.test('should create a valid PDFResult', async () => {
|
||||
const writePDfToDisk = async (urlArg: string, fileName: string) => {
|
||||
tap.test('should create valid PDF results and write them to disk', async () => {
|
||||
const writePdfToDisk = async (urlArg: string, fileName: string) => {
|
||||
const pdfResult = await testSmartPdf.getFullWebsiteAsSinglePdf(urlArg);
|
||||
expect(pdfResult.buffer).toBeInstanceOf(Buffer);
|
||||
const fs = await import('fs');
|
||||
|
||||
if (!fs.existsSync('.nogit/')) {
|
||||
fs.mkdirSync('.nogit/');
|
||||
}
|
||||
fs.writeFileSync(`.nogit/${fileName}`, pdfResult.buffer as Buffer);
|
||||
ensureDir('.nogit');
|
||||
fs.writeFileSync(path.join('.nogit', fileName), pdfResult.buffer as Buffer);
|
||||
};
|
||||
await writePDfToDisk('https://lossless.com/', '1.pdf');
|
||||
await writePDfToDisk('https://layer.io', '2.pdf');
|
||||
await writePdfToDisk('https://lossless.com/', '1.pdf');
|
||||
await writePdfToDisk('https://layer.io', '2.pdf');
|
||||
});
|
||||
|
||||
tap.test('should merge pdfs', async () => {
|
||||
const fs = await import('fs');
|
||||
tap.test('should merge PDFs into a combined PDF', async () => {
|
||||
const pdf1 = await testSmartPdf.readFileToPdfObject('.nogit/1.pdf');
|
||||
const pdf2 = await testSmartPdf.readFileToPdfObject('.nogit/2.pdf');
|
||||
fs.writeFileSync(
|
||||
`.nogit/combined.pdf`,
|
||||
await testSmartPdf.mergePdfs([pdf1.buffer, pdf2.buffer])
|
||||
);
|
||||
const mergedBuffer = await testSmartPdf.mergePdfs([pdf1.buffer, pdf2.buffer]);
|
||||
ensureDir('.nogit');
|
||||
fs.writeFileSync(path.join('.nogit', 'combined.pdf'), mergedBuffer);
|
||||
});
|
||||
|
||||
tap.test('should create images from an pdf', async () => {
|
||||
tap.test('should create PNG images from combined PDF using Puppeteer conversion', async () => {
|
||||
const pdfObject = await testSmartPdf.readFileToPdfObject('.nogit/combined.pdf');
|
||||
const images = await testSmartPdf.convertPDFToPngBytes(pdfObject.buffer);
|
||||
console.log(images.map((val) => val.length));
|
||||
expect(images.length).toBeGreaterThan(0);
|
||||
console.log('Puppeteer-based conversion image sizes:', images.map(img => img.length));
|
||||
});
|
||||
|
||||
tap.test('should be able to close properly', async () => {
|
||||
tap.test('should store PNG results from both conversion functions in .nogit/testresults', async () => {
|
||||
const testResultsDir = path.join('.nogit', 'testresults');
|
||||
ensureDir(testResultsDir);
|
||||
|
||||
const pdfObject = await testSmartPdf.readFileToPdfObject('.nogit/combined.pdf');
|
||||
|
||||
// Convert using Puppeteer-based function and store images
|
||||
const imagesPuppeteer = await testSmartPdf.convertPDFToPngBytes(pdfObject.buffer);
|
||||
imagesPuppeteer.forEach((img, index) => {
|
||||
const filePath = path.join(testResultsDir, `puppeteer_method_page_${index + 1}.png`);
|
||||
fs.writeFileSync(filePath, Buffer.from(img));
|
||||
});
|
||||
});
|
||||
|
||||
tap.test('should close the SmartPdf instance properly', async () => {
|
||||
await testSmartPdf.stop();
|
||||
});
|
||||
|
||||
tap.start();
|
||||
tap.start();
|
Reference in New Issue
Block a user