{"library":"pdf2pic","title":"PDF to Image Conversion Utility","description":"pdf2pic is a robust Node.js utility library designed for converting PDF documents into various image formats (like PNG, JPEG), base64 encoded strings, or raw image buffers. The current stable version is 3.2.0, with the project maintaining an active release cadence, frequently delivering patches and minor updates. A key differentiator of pdf2pic is its flexibility in output types and its ability to handle conversions from file paths, buffers, or base64 input. It acts as a wrapper around powerful external system dependencies: GraphicsMagick (or ImageMagick) for image processing and Ghostscript for PDF rendering. This design necessitates prior installation of these tools on the host system. The library ships with comprehensive TypeScript type definitions, enhancing the development experience for TypeScript users, and requires Node.js version 14 or higher.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install pdf2pic"],"cli":null},"imports":["import { fromPath } from 'pdf2pic';","import { fromBuffer } from 'pdf2pic';","import type { Pdf2picOptions } from 'pdf2pic';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { fromPath } from \"pdf2pic\";\nimport path from \"path\";\nimport { promises as fs } from 'fs';\n\n// Ensure the output directory exists\nconst savePath = path.resolve(\"./images\");\nawait fs.mkdir(savePath, { recursive: true });\n\n// In a real application, provide a full, valid path to your PDF.\n// For this example, ensure 'sample.pdf' exists in your project root.\nconst pdfFilePath = path.resolve(\"./sample.pdf\");\n\nconst options = {\n  density: 100,\n  saveFilename: \"converted_page\",\n  savePath: savePath,\n  format: \"png\",\n  width: 600,\n  height: 600\n};\n\nconst convert = fromPath(pdfFilePath, options);\nconst pageToConvertAsImage = 1;\n\ntry {\n  const result = await convert(pageToConvertAsImage, { responseType: \"image\" });\n  console.log(`Page ${pageToConvertAsImage} converted successfully.`);\n  console.log(`Output: ${path.join(result.path, result.name)}`);\n} catch (error) {\n  console.error(\"Error converting PDF page:\", error.message);\n  // Provide detailed error messages for common prerequisites issues\n  if (error.message.includes(\"gm ENOENT\")) {\n    console.error(\"GraphicsMagick/ImageMagick not found. Ensure it's installed and in PATH.\");\n    console.error(\"See: https://github.com/yakovmeister/pdf2image#prerequisites\");\n  } else if (error.message.includes(\"gs ENOENT\")) {\n    console.error(\"Ghostscript not found. Ensure it's installed and in PATH.\");\n    console.error(\"See: https://github.com/yakovmeister/pdf2image#prerequisites\");\n  }\n}\n\n// Optional: Example for converting all pages\ntry {\n  console.log(\"\\nAttempting to convert all pages...\");\n  // To run this, you must have a 'sample.pdf' in your project root.\n  // For testing, consider using a small dummy PDF.\n  const bulkResult = await fromPath(pdfFilePath, options).bulk(-1, { responseType: \"image\" });\n  console.log(`Successfully converted ${bulkResult.length} pages.`);\n} catch (error) {\n  console.error(\"Error converting all PDF pages:\", error.message);\n}","lang":"javascript","description":"This quickstart demonstrates how to initialize pdf2pic from a file path, convert a specific page to an image file, and includes robust error handling for common issues like missing external dependencies. It also shows an example of converting all pages in a PDF document.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}