Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert multiple images into single pdf using nodejs

Tags:

node.js

I need to convert multiple images into a single PDF. I'm able to create PDF for one image. The problem comes when I have multiple images. How I can create multiple pages PDF.

like image 920
Gaurav Kandpal Avatar asked Jun 27 '18 04:06

Gaurav Kandpal


People also ask

How do I put multiple pictures into one PDF?

Simply visit the Acrobat Online website and upload the files you want to merge. Reorder the files however you like and then click Merge files. After that, just download the merged PDF. This will combine all the JPGs-turned-PDFs into a single PDF you can easily share or view.

What is the best way to save multiple full size images into single PDF?

Hold down the CMD key as you make your selection to choose multiple images, then right-click and select Open with > Preview. Click-and-drag the photos in the sidebar to rearrange their order. When you're satisfied, select File > Print. In the PDF drop-down menu, choose Save as PDF.

Is Node JS good for image processing?

With image processing, your application can resize and compress all the user-uploaded images, which can significantly improve your application performance and save your server disk space. Node. js has an ecosystem of libraries you can use to process images, such as sharp, jimp, and gm module.


3 Answers

I got my desired output by below code.

PDFDocument = require('pdfkit');
fs = require('fs');
doc = new PDFDocument

//Pipe its output somewhere, like to a file or HTTP response 
//See below for browser usage 
doc.pipe(fs.createWriteStream('output.pdf'))


//Add an image, constrain it to a given size, and center it vertically and horizontally 
doc.image('./test.jpg', {
   fit: [500, 400],
   align: 'center',
   valign: 'center'
});

doc.addPage()
   .image('./1.png', {
   fit: [500,400],
   align: 'center',
   valign: 'center'
});


doc.end()
like image 57
Gaurav Kandpal Avatar answered Oct 19 '22 02:10

Gaurav Kandpal


This is the proper way to do it:

var pdf = new (require('pdfkit'))({
    autoFirstPage: false
});
var img = pdf.openImage('./myImage.jpg');
pdf.addPage({size: [img.width, img.height]});
pdf.image(img, 0, 0);
pdf.end();
like image 5
Binary Avatar answered Oct 19 '22 00:10

Binary


You can use a library called html-pdf to convert your HTML template to a PDF file

Code:

server.js

const express = require('express');
const app = express();

const ejs = require('ejs');
const htmlPdf = require('html-pdf');

const fs = require('fs');
const path = require('path');

const images = [
  'https://images.pexels.com/photos/104827/cat-pet-animal-domestic-104827.jpeg?auto=compress&cs=tinysrgb&h=350',
  'https://images.pexels.com/photos/104827/cat-pet-animal-domestic-104827.jpeg?auto=compress&cs=tinysrgb&h=350',
  'https://images.pexels.com/photos/104827/cat-pet-animal-domestic-104827.jpeg?auto=compress&cs=tinysrgb&h=350',
];

app.get('/', (req, res) => {
  fs.readFile(path.resolve(`${__dirname}/views/template.ejs`), 'utf-8', (error, content) => {
    if(error){
      console.log(error);
    }else{
      
      const html = ejs.render(content, {
        images,
      });
      
      htmlPdf.create(html).toStream(function(err, stream){
        stream.pipe(res);
      });
      
    }
  });
});

const listener = app.listen(process.env.PORT, () => {
  console.log('Your app is listening on port ' + listener.address().port);
});

views/template.ejs

<html>
  <body>
    <h1>Cat images</h1>
    <ul>
      <% images.forEach(image => { %>
      <img src="<%- image%>" />
      <% }) %>
    </ul>
  </body>
</html>

Live demo

like image 4
Felix Fong Avatar answered Oct 19 '22 00:10

Felix Fong