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.
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.
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.
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.
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()
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();
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With