Building a Simple JPG, JPEG, PNG to PDF Converter Using HTML, CSS, and JavaScript
In today’s digital age, converting image formats to PDFs is a common requirement across multiple applications. Whether it’s for creating documents, presentations, or simply sharing files, converting images to a single, portable PDF file is essential. In this article, I’ll walk you through building a simple JPG, JPEG, PNG to PDF Converter using only HTML, CSS, and JavaScript.
Why Create This Project?
As a developer and someone who is constantly looking to improve my skills, building this image-to-PDF converter project was a perfect opportunity to learn and practice the following:
This project is a great example for beginners looking to dive into web development and efficiently handle user-uploaded files.
Project Overview
The goal of this project is simple: to allow users to upload JPG, JPEG, or PNG images, preview them, and convert them into a single PDF file. Below are the core features:
Step-by-Step Walkthrough
Setting Up the HTML Structure
The first step in this project was creating a simple HTML structure with the necessary input fields, buttons, and containers.
<input type="file" id="imageInput" accept="image/jpeg, image/jpg, image/png" multiple>
<div id="preview"></div>
<button id="convertBtn">Convert to PDF</button>
<button id="backBtn">🔄 Reset</button>
Here, we created an input field that allows multiple file uploads and accepted JPG, JPEG, and PNG formats. The convertBtn triggers the conversion process, while backBtn resets the form.
Preview Uploaded Images
Once the user selects an image, we need to preview it before proceeding with the PDF conversion.
Recommended by LinkedIn
imageInput.addEventListener('change', (event) => {
const files = event.target.files;
for (const file of files) {
const reader = new FileReader();
reader.onload = (e) => {
const img = new Image();
img.src = e.target.result;
preview.appendChild(img);
};
reader.readAsDataURL(file);
}
});
Generating the PDF with jsPDF
To create the PDF, we utilized the jsPDF library. Here’s the function that converts the uploaded images into a PDF
convertBtn.addEventListener('click', async () => {
const { jsPDF } = window.jspdf;
const pdf = new jsPDF();
for (let i = 0; i < imageFiles.length; i++) {
const img = new Image();
img.src = imageDataUrl;
await new Promise(resolve => {
img.onload = () => {
const imgWidth = pdf.internal.pageSize.getWidth();
const imgHeight = (img.height * imgWidth) / img.width;
if (i > 0) pdf.addPage();
pdf.addImage(img, 'JPEG', 0, 0, imgWidth, imgHeight);
resolve();
};
});
}
pdf.save('converted.pdf');
});
convertBtn.addEventListener('click', async () => {
const { jsPDF } = window.jspdf;
const pdf = new jsPDF();
for (let i = 0; i < imageFiles.length; i++) {
const img = new Image();
img.src = imageDataUrl;
await new Promise(resolve => {
img.onload = () => {
const imgWidth = pdf.internal.pageSize.getWidth();
const imgHeight = (img.height * imgWidth) / img.width;
if (i > 0) pdf.addPage();
pdf.addImage(img, 'JPEG', 0, 0, imgWidth, imgHeight);
resolve();
};
});
}
pdf.save('converted.pdf');
});
Challenges Faced
Conclusion
Building this JPG, JPEG, PNG to PDF Converter was a fun and educational experience that allowed me to practice key web development skills. By using HTML, CSS, and JavaScript along with the jsPDF library, I was able to create a fully functional image-to-PDF converter with features like image preview, file handling, and responsive design.
I hope this article inspires you to build similar projects and sharpen your development skills. Stay tuned for more posts where I’ll be diving deeper into other projects and exploring the world of web development!
Call to Action
If you found this project helpful, feel free to connect with me or leave a comment below! Let me know if you’d like me to share the source code or help with any specific web development topics.
Github:- Sahil-Ali-01/Image-to-pdf-converter
Demo:- Site