ImageJavaScriptNodejsPDFProgramming

How To Convert HEIC Images to PDF in JavaScript

2 Mins read

HEIC (High-Efficiency Image Format) is a popular image format used by Apple devices, offering high-quality images with smaller file sizes compared to JPEG. However, this format is not universally supported, which might necessitate converting HEIC images to a more accessible format like PDF. In this article, we’ll explore how to convert HEIC images to PDF using JavaScript Node.js.

A working example of this tool is available on this page .

Prerequisites

To follow along with this tutorial, you’ll need:

  1. Basic knowledge of JavaScript and Node.js.
  2. Node.js installed on your system.
  3. Familiarity with npm (Node Package Manager).

Step-by-Step Guide

Step 1: Set Up Your Project

First, create a new directory for your project and initialize it with npm.

mkdir heic-to-pdf
cd heic-to-pdf
npm init -y

Step 2: Install Required Packages

We’ll use the following npm packages:

  • heic-convert to convert HEIC images to PNG.
  • pdf-lib to create PDF documents.

Install these packages by running:

npm install heic-convert pdf-lib

Step 3: Write the Conversion Script

Create a new JavaScript file, convert.js, in your project directory. This script will handle the conversion process.

const fs = require('fs');
const heicConvert = require('heic-convert');
const { PDFDocument } = require('pdf-lib');

async function convertHeicToPdf(inputPath, outputPath) {
  // Read the HEIC file
  const heicBuffer = fs.readFileSync(inputPath);

  // Convert HEIC to PNG
  const pngBuffer = await heicConvert({
    buffer: heicBuffer,
    format: 'PNG',
    quality: 1 // Set quality to 1 (highest)
  });

  // Create a new PDF document
  const pdfDoc = await PDFDocument.create();

  // Embed the PNG image into the PDF
  const pngImage = await pdfDoc.embedPng(pngBuffer);
  const pngDims = pngImage.scale(1);

  // Add a blank page and draw the PNG image on it
  const page = pdfDoc.addPage([pngDims.width, pngDims.height]);
  page.drawImage(pngImage, {
    x: 0,
    y: 0,
    width: pngDims.width,
    height: pngDims.height
  });

  // Serialize the PDF document to bytes (a Uint8Array)
  const pdfBytes = await pdfDoc.save();

  // Write the PDF to a file
  fs.writeFileSync(outputPath, pdfBytes);

  console.log(`Converted HEIC to PDF: ${outputPath}`);
}

Example Usage of the function

// Example usage
convertHeicToPdf('input.heic', 'output.pdf');

Running the Script

To convert a HEIC image to a PDF, run the following command:

node convert.js

Ensure you have an input.heic file in your project directory. The script will generate an output.pdf file in the same directory.

Conclusion

Converting HEIC images to PDF using JavaScript is straightforward with the help of heic-convert and pdf-lib. This solution reads a HEIC file, converts it to a PNG image, embeds the PNG into a PDF document, and saves the PDF file. This approach can be extended and integrated into larger applications, providing a practical solution for handling HEIC images in a more accessible format.

With this method, you can efficiently convert HEIC images to PDF, making them easier to share and view across different devices and platforms.

Leave a Reply

Your email address will not be published. Required fields are marked *