Skip to main content

Converter

@pdfme/converter can be used in both Node.js and in the browser.

Its primary purpose is to convert PDFs into other formats (like images) or to convert various data formats (like Markdown) into PDFs.

Although it’s still under development, you can already use the following features:

  • Convert PDF to Images: pdf2img
  • Retrieve Each Page’s Width and Height: pdf2size

Planned conversion features include:

  • Markdown to PDF: md2pdf
  • PDF to Markdown: pdf2md

Installation

npm install @pdfme/converter

If you want to convert PDFs to images (pdf2img) in Node.js, you’ll need node-canvas (^2.11.2), which requires an additional step:

npm install canvas@^2.11.2

Usage

For instance, the pdf2img function has the following TypeScript interface:

pdf2img(pdf: ArrayBuffer, options?: Pdf2ImgOptions): Promise<ArrayBuffer[]>

See the details here:
https://github.com/pdfme/pdfme/blob/main/packages/converter/src/pdf2img.ts

Below is an example in TypeScript that reads a local PDF, converts the first page into a PNG, and saves it as a thumbnail:

import fs from 'fs';
import path from 'path';
import { pdf2img } from '@pdfme/converter';

async function generateThumbnail(pdfPath: string, thumbnailPath: string): Promise<void> {
try {
const pdf = fs.readFileSync(pdfPath);
const pdfArrayBuffer = pdf.buffer.slice(pdf.byteOffset, pdf.byteOffset + pdf.byteLength);

const images = await pdf2img(pdfArrayBuffer, {
imageType: 'png',
range: { end: 1 },
});

const thumbnail = images[0];
fs.writeFileSync(thumbnailPath, Buffer.from(thumbnail));

console.log(`Thumbnail saved to ${thumbnailPath}`);
} catch (err) {
console.error(`Failed to generate thumbnail from ${pdfPath} to ${thumbnailPath}`, err);
}
}

For reference, check out the thumbnail generation script in the repository’s playground directory.

Contact

If you have any questions or suggestions about @pdfme/converter, please reach out via: