feat(now supports pdf -> jpg): update

This commit is contained in:
Philipp Kunz 2024-04-25 18:48:08 +02:00
parent fe6be928a9
commit d157a3acd9
6 changed files with 906 additions and 489 deletions

View File

@ -18,22 +18,24 @@
"@gitzone/tsdoc": "^1.1.12",
"@gitzone/tsrun": "^1.2.44",
"@gitzone/tstest": "^1.0.77",
"@push.rocks/tapbundle": "^5.0.12",
"@types/node": "^20.4.5"
"@push.rocks/tapbundle": "^5.0.23",
"@types/node": "^20.12.7"
},
"dependencies": {
"@push.rocks/smartbuffer": "^3.0.4",
"@push.rocks/smartdelay": "^3.0.5",
"@push.rocks/smartfile": "^10.0.28",
"@push.rocks/smartfile": "^11.0.14",
"@push.rocks/smartnetwork": "^3.0.0",
"@push.rocks/smartpath": "^5.0.11",
"@push.rocks/smartpath": "^5.0.18",
"@push.rocks/smartpromise": "^4.0.3",
"@push.rocks/smartpuppeteer": "^2.0.2",
"@push.rocks/smartunique": "^3.0.3",
"@tsclass/tsclass": "^4.0.42",
"@types/express": "^4.17.17",
"express": "^4.18.1",
"@push.rocks/smartunique": "^3.0.9",
"@tsclass/tsclass": "^4.0.54",
"@types/express": "^4.17.21",
"express": "^4.19.2",
"pdf-lib": "^1.17.1",
"pdf2json": "^2.0.0"
"pdf2json": "^3.0.5",
"pdf2pic": "^3.1.1"
},
"files": [
"ts/**/*",

File diff suppressed because it is too large Load Diff

View File

@ -43,16 +43,22 @@ tap.test('should create a valid PDFResult', async () => {
await writePDfToDisk('https://layer.io', '2.pdf');
});
tap.test('should combine pdfs', async () => {
tap.test('should merge pdfs', async () => {
const fs = await import('fs');
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, pdf2])).buffer as Buffer
await testSmartPdf.mergePdfs([pdf1.buffer, pdf2.buffer])
);
});
tap.test('should create images from an pdf', async () => {
const pdfObject = await testSmartPdf.readFileToPdfObject('.nogit/combined.pdf');
const images = await testSmartPdf.convertPDFToJPGBytes(pdfObject.buffer);
console.log(images);
});
tap.test('should be able to close properly', async () => {
await testSmartPdf.stop();
});

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@push.rocks/smartpdf',
version: '3.0.17',
description: 'create pdfs on the fly'
version: '3.1.0',
description: 'A library for creating PDFs dynamically from HTML or websites with additional features like merging PDFs.'
}

View File

@ -183,18 +183,16 @@ export class SmartPdf {
};
}
public async mergePdfs(pdfArrayArg: plugins.tsclass.business.IPdf[]): Promise<IPdf> {
const merger = new plugins.pdfMerger();
for (const pdf of pdfArrayArg) {
merger.add(Buffer.from(pdf.buffer));
public async mergePdfs(inputPdfBuffers: Uint8Array[]): Promise<Uint8Array> {
const mergedPdf = await plugins.pdfLib.PDFDocument.create();
for (const pdfBytes of inputPdfBuffers) {
const pdfDoc = await plugins.pdfLib.PDFDocument.load(pdfBytes);
const pages = await mergedPdf.copyPages(pdfDoc, pdfDoc.getPageIndices());
pages.forEach((page) => mergedPdf.addPage(page));
}
const resultBuffer = await merger.saveAsBuffer();
return {
name: 'mergedPdf',
buffer: resultBuffer,
id: null,
metadata: null,
};
const mergedPdfBytes = await mergedPdf.save();
return mergedPdfBytes;
}
public async readFileToPdfObject(pathArg: string): Promise<plugins.tsclass.business.IPdf> {
@ -226,4 +224,39 @@ export class SmartPdf {
pdfParser.parseBuffer(pdfBufferArg);
return deferred.promise;
}
public async convertPDFToJPGBytes(
pdfBytes: Uint8Array,
options: {
width?: number;
height?: number;
quality?: number;
} = {}
): Promise<Uint8Array[]> {
const { width = 1024, height = 768, quality = 100 } = options;
// Load the PDF document
const pdfDoc = await plugins.pdfLib.PDFDocument.load(pdfBytes);
const converter = plugins.pdf2pic.fromBuffer(Buffer.from(pdfBytes), {
density: 100, // Image density (DPI)
format: 'jpg', // Image format
width, // Output image width
height, // Output image height
quality, // Output image quality
});
// Get array promises that resolve to JPG buffers
const imagePromises: Promise<Buffer>[] = [];
const numPages = pdfDoc.getPageCount();
for (let i = 0; i < numPages; i++) {
imagePromises.push(converter(i + 1, {
responseType: 'buffer',
}).then((output) => output.buffer));
}
// Resolve all promises and return the array of buffers
return Promise.all(imagePromises);
}
}

View File

@ -5,6 +5,7 @@ import * as path from 'path';
export { http, path };
// @pushrocks
import * as smartbuffer from '@push.rocks/smartbuffer';
import * as smartfile from '@push.rocks/smartfile';
import * as smartdelay from '@push.rocks/smartdelay';
import * as smartpromise from '@push.rocks/smartpromise';
@ -14,6 +15,7 @@ import * as smartnetwork from '@push.rocks/smartnetwork';
import * as smartunique from '@push.rocks/smartunique';
export {
smartbuffer,
smartfile,
smartdelay,
smartpromise,
@ -29,9 +31,9 @@ import * as tsclass from '@tsclass/tsclass';
export { tsclass };
// thirdparty
import pdfMerger from 'pdf-merger-js';
// @ts-ignore
import pdf2json from 'pdf2json';
import express from 'express';
import pdf2json from 'pdf2json';
import pdf2pic from 'pdf2pic';
import pdfLib from 'pdf-lib';
export { pdfMerger, pdf2json, express };
export { express, pdf2json, pdf2pic, pdfLib, };