首頁 >web前端 >js教程 >如何在 Node.js 中將 PDF 頁面轉換為圖像

如何在 Node.js 中將 PDF 頁面轉換為圖像

DDD
DDD原創
2024-09-18 19:47:36660瀏覽

How to Convert PDF Pages to Images in Node.js

在本文中,我們將介紹如何使用 Node.js 將 PDF 頁面轉換為圖片。這對於生成縮圖或從 PDF 文件中提取可視內容非常有用。我們將使用 pdfjs-dist 庫來載入和渲染 PDF 頁面,並使用 canvas 來建立圖像緩衝區。

先決條件
在開始之前,您需要安裝所需的軟體包:

npm install pdfjs-dist canvas

將 PDF 頁面轉換為映像並儲存到本地的程式碼:

const fs = require('fs');
const path = require('path');
const pdfjs = require('pdfjs-dist/legacy/build/pdf.js');
const Canvas = require('canvas');

/**
 * Converts a PDF to images by rendering each page and saving them to a local directory.
 * 
 * @param {Buffer} pdfBuffer - The PDF file as a buffer.
 * @param {string} outputDir - The directory where images will be saved.
 * @returns {Promise<void>} Resolves when all images are saved.
 */
async function convertPdfToImages(pdfBuffer, outputDir) {
  try {
    // Ensure the output directory exists
    if (!fs.existsSync(outputDir)) {
      fs.mkdirSync(outputDir, { recursive: true });
    }

    // Load the original PDF using pdf.js
    const loadingTask = pdfjs.getDocument({ data: pdfBuffer });
    const pdfDocument = await loadingTask.promise;

    // Loop through each page of the PDF
    for (let i = 1; i <= pdfDocument.numPages; i++) {
      const page = await pdfDocument.getPage(i);

      // Render the page as an image and save it
      const imageBuffer = await renderPageToImage(page);

      // Save the image to the output directory
      const imagePath = path.join(outputDir, `page_${i}.jpg`);
      fs.writeFileSync(imagePath, imageBuffer);
      console.log(`Saved: ${imagePath}`);
    }
  } catch (error) {
    console.error('Error converting PDF to images:', error);
  }
}

/**
 * Renders a single PDF page to an image buffer.
 * 
 * @param {PDFPageProxy} page - The PDF.js page object.
 * @returns {Promise<Buffer>} The image as a buffer (JPEG format).
 */
async function renderPageToImage(page) {
  // Scale the page to 2x for a higher quality image output
  const viewport = page.getViewport({ scale: 2.0 });
  const canvas = Canvas.createCanvas(viewport.width, viewport.height);
  const context = canvas.getContext('2d');

  const renderContext = {
    canvasContext: context,
    viewport: viewport,
  };

  // Render the PDF page to the canvas
  await page.render(renderContext).promise;

  // Convert the canvas content to a JPEG image buffer and return it
  return canvas.toBuffer('image/jpeg');
}

// Example usage:
// const pdfBuffer = fs.readFileSync('sample.pdf');
// convertPdfToImages(pdfBuffer, './output_images');

代碼說明

  1. 載入 PDF:我們使用 pdfjs-dist 從緩衝區載入 PDF 檔案。
const loadingTask = pdfjs.getDocument({ data: pdfBuffer });
const pdfDocument = await loadingTask.promise;
  1. 渲染每個頁面:對於 PDF 中的每個頁面,我們使用 pdfjs-dist 中的 getPage 和 render 方法將其渲染到畫布上。
const page = await pdfDocument.getPage(pageNumber);
const renderContext = {
  canvasContext: context,
  viewport: viewport,
};
await page.render(renderContext).promise;
  1. 本地儲存影像:頁面渲染到畫布後,我們使用 Node.js 的 fs 模組以 JPEG 格式儲存影像緩衝區。
fs.writeFileSync(imagePath, imageBuffer);

結論:
此方法可以有效地將 PDF 轉換為影像,使您能夠處理或視覺化 PDF 內容。為了獲得高品質圖像,我們將畫布縮放至 2 倍。這可以根據您的需求輕鬆調整。

我希望這有幫助!請隨意根據您的要求調整程式碼。

以上是如何在 Node.js 中將 PDF 頁面轉換為圖像的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn