Skip to content

Instantly share code, notes, and snippets.

@29decibel
Last active September 9, 2023 16:30
Show Gist options
  • Save 29decibel/3406ed5139f8b073d8ecea4fad4799d5 to your computer and use it in GitHub Desktop.
Save 29decibel/3406ed5139f8b073d8ecea4fad4799d5 to your computer and use it in GitHub Desktop.
Extract first page of the PDF as cover image
import fs from "fs";
import { PDFDocument } from "pdf-lib";
import { fromBuffer } from "pdf2pic";
import path from "path";
const OUTPUT_IMAGE_DPI = 200;
export async function extractPDFCoverImage(
pdfFilePath: string,
coverOutputPath: string
) {
try {
// Read the existing PDF into a buffer
const existingPdfBytes: Buffer = fs.readFileSync(pdfFilePath);
// Load the PDF with pdf-lib
const pdfDoc = await PDFDocument.load(existingPdfBytes);
const pdfDimensions = await getCoverPageDimensions(pdfDoc);
// we need a new pdf doc to get buffer which will be used by pdf2pic
const newPdfDoc = await PDFDocument.create();
// Copy the cover page from the existing PDF into the new PDF
const [coverPage] = await newPdfDoc.copyPages(pdfDoc, [0]);
newPdfDoc.addPage(coverPage);
// Serialize the PDF to bytes and write it to the file system
const newPdfBytes: Uint8Array = await newPdfDoc.save();
const newPdfBuffer = Buffer.from(newPdfBytes);
// now we can start converting the pdf to image
const coverOutputPathParts = parseFilePath(coverOutputPath);
const options = {
density: OUTPUT_IMAGE_DPI,
...coverOutputPathParts,
...pdfDimensions,
};
const convert = fromBuffer(newPdfBuffer, options);
const pageToConvertAsImage = 1;
await convert(pageToConvertAsImage, { responseType: "image" });
} catch (err) {
console.error("Error:", err);
}
}
function parseFilePath(absolutePath: string) {
const ext = path.extname(absolutePath).substring(1); // Get extension without the dot
const filename = path.basename(absolutePath, `.${ext}`); // Get filename without extension
const directory = path.dirname(absolutePath); // Get directory path
return {
saveFilename: filename,
savePath: directory,
format: ext,
};
}
async function getCoverPageDimensions(pdfDoc: PDFDocument): Promise<{
width: number;
height: number;
}> {
const coverPagePdf = pdfDoc.getPages()[0];
// Get the dimensions
return coverPagePdf.getSize();
}
const pdfFilePath = "existing.pdf";
// Call the function
extractAndConvertCoverPage(pdfFilePath, "/tmp/pdf-cover/cover_page12333.png");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment