How to convert PDF file pages to PNG images-node.js

  • node >= 12.x
  • graphicsmagick
  • ghostscript
  1. Install the node.js from the node.js official website https://nodejs.org/en/download/ by downloading the LTS version
var express = require("express");
var router = express.Router();
const {
fromPath
} = require("pdf2pic");
const pdf = require("pdf-page-counter");
const fs = require("fs");
var app = express();
app.get("/pdfpng", function(req, res, next) {
const {
filename
} = req.query;
const options = {
density: 100,
saveFilename: "file",
savePath: "./public/uploads",
format: "png",
width: 600,
height: 600,
};
const storeAsImage = fromPath(`./public/uploads/${filename}`, options);
let dataBuffer = fs.readFileSync(`./public/uploads/${filename}`);
pdf(dataBuffer).then(function(data) {
for (var pageToConvertAsImage = 1; pageToConvertAsImage <= data.numpages; pageToConvertAsImage++) {
storeAsImage(pageToConvertAsImage).then((resolve) => {
return resolve;
});
}
res.send({
filename: filename
});
});
});
app.listen(3200, function () { 
console.log('App listening on port 3200!');
});

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
CodingHub

CodingHub

Coding Hub — Learn & Build Your Career