A fully promise-based Node.js library which can work with PDFs, based on Ghostscript and qpdf.
All PDF files that are handled by this library in the form of Buffer objects, i.e. to a user of this library, it looks like everything works only in memory. In the background though, file system access (via tempy) is needed.
Additionally, this library requires the gs command (Ghostscript) as well as qpdf to be available. The required apt dependencies on Ubuntu 20.04 can be installed via the following command:
sudo apt install -y python python3 build-essential ghostscript libjpeg-dev libpng-dev libcurl4-openssl-dev mupdf-tools libfreetype6-dev qpdf
npm install ghostscript-node
constgs=require("ghostscript-node");(async()=>{constpdf1=/* load first PDF as Buffer, e.g. from database */;constpdf2=/* load second PDF as Buffer, e.g. from database */;// get Buffer object with bytes of combined PDFconstcombinedPDF=awaitgs.combinePDFs([pdf1,pdf2]);// get number of pages of pdf1constnumPagesPDF1=awaitgs.countPDFPages(pdf1);// get Buffer object with bytes of specified set of pages// page numbers begin with 1, last page is includedconstpartOfPDF1=awaitgs.extractPDFPages(pdf1,3,5);// rotate all pages of pdf1 by 90 degrees clockwiseconstrotatedPDF=awaitgs.rotatePDF(pdf1,"90");// returns an array of buffers containing PNG images of the desired pagesconstrenderedPages=awaitgs.renderPDFPagesToPNG(pdf1);// checks if pdf1 is a valid PDF fileconstisPDF1Valid=awaitgs.isValidPDF(pdf1);})();MIT