feat(now supports pdf -> jpg): update
This commit is contained in:
@ -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.'
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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, };
|
||||
|
Reference in New Issue
Block a user