Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 68 additions & 25 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
'use strict';

const fs = require('fs');
const fsPromises = fs.promises;

const http = require('http');
const PORT = 3330 || process.env.PORT;
const src = "" //source of the pdf file for which thumbnail is to be created
const src = "http://www.nitjsr.ac.in/uploads/NIRF-2020%20INDIA%20RANK%20(ENGINEERING).pdf" //source of the pdf file for which thumbnail is to be created it may be local file or link of pdf

const hummus = require('hummus');
const streams = require('memory-streams');
const { input } = require('node-pdftocairo');
const imageToBase64 = require('image-to-base64');
const download = require('download');
const { compress } = require('compress-images/promise');


const PDFRStreamForBuffer = require('./pdfr-stream-for-buffer.js');
var Blob = require('blob');
// https://stackoverflow.com/questions/42512982/node-js-get-the-first-page-of-pdf-buffer => getting buffer array for the first page
const getFirstPage = function (buffer) {
const getFirstPage = async function (src) {

if (src.includes("http://") || src.includes("https://")) { // reading file if it contains http(s):// then read from url or read from local file
var buffer = await download(src); //downloading remote file as a buffer
console.log("reading from url")
}
else {
buffer = await fsPromises.readFile(src); //reading local file
console.log("reading from local storage")
}

//Creating a stream, so hummus pushes the result to it
let outStream = new streams.WritableStream();
//Using PDFStreamForResponse to be able to pass a writable stream
Expand All @@ -32,33 +51,57 @@ const getFirstPage = function (buffer) {
//we can just grab stream's content and return it
return outStream.toBuffer();
};
function pdf2thumb(buf) {
let ResImg=buf;
async function pdf2thumb(src) {

try {
let firstPageBuffer = await getFirstPage(src); //buffer array for the first page of the given document
var file_name = Math.random()
// png jpeg tiff ps eps pdf svg
const INPUT_path_to_your_images = './' + file_name + '.jpg';

await input(firstPageBuffer, { format: 'jpeg', singlefile: true }).output(file_name); //saving a temporary file

await compress({ //compressing image file
source: INPUT_path_to_your_images,
destination: './compressed_',
params: { compress_force: true, statistic: false, autoupdate: false },
enginesSetup: {
jpg: { engine: 'mozjpeg', command: ['-quality', '5'] },
}
});

return ResImg;

}

fs.unlink(file_name + ".jpg", (err => { //removing uncompressed file
if (err) console.log(err);
}));

http.createServer(function (req, res) {
fs.readFile(src,(err,data)=>{
try{
let firstPageBuffer = getFirstPage(data); //buffer array for the first page of the given document
let response = await imageToBase64("compressed_" + file_name + ".jpg") //converting compressed file into base64

fs.unlink("compressed_" + file_name + ".jpg", (err => { //removing compressed file as our work is done
if (err) console.log(err);
}));

var html_response = `<!DOCTYPE html><html><body><center><a href="${src}"><img style='display:block; width:300px;height:400px;' id='base64image' src='data:image/jpeg;base64, ${response}' /></a><a href="data:image/jpeg;base64, ${response}" download><button>Download</button></a></center></body></html>`
return html_response;
}
catch (err) {
console.log(err);
}
}

var htmlElement = pdf2thumb(firstPageBuffer); //converting the buffer array of first page of pdf to html element to be returned
res.write( htmlElement,()=>{
console.log('first page displayed')
})



http.createServer(async function (req, res) {
try {

res.end()
}
catch(err){
console.log(err);
}
})
}).listen(PORT,()=>console.log(`Server Started @ ${PORT}`));

var htmlElement = await pdf2thumb(src); //converting the buffer array of first page of pdf to html element to be returned
res.write(htmlElement, () => {
console.log('first page displayed')
})
res.end()
}
catch (err) {
console.log(err);
res.write("Something Went Wrong:"+err.message)
res.end()
}
}).listen(PORT, () => console.log(`Server Started @ ${PORT}`));
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@
"homepage": "https://github.com/NITJSR-OSS/PDF-Thumbnail-Creator#readme",
"dependencies": {
"blob": "^0.1.0",
"hummus": "^1.0.108",
"memory-streams": "^0.1.3"
"compress-images": "^1.9.5",
"download": "^8.0.0",
"hummus": "^1.0.110",
"image-to-base64": "^2.1.1",
"memory-streams": "^0.1.3",
"node-pdftocairo": "^1.1.0",
"pngquant-bin": "^6.0.0",
"tslib": "^2.0.1"
}
}
Binary file added sample.pdf
Binary file not shown.