從 PDF 中提取圖片的 Node.js - 圖片提取器

在這篇文章中,您將學習如何使用 Node.js 和強大的 GroupDocs.Parser Cloud REST API 程式化地從 PDF 檔案中提取圖像。無論您是在建立內容解析器、數據提取工具,還是文件自動化系統,從 PDF 中提取嵌入圖像是一個常見的需求。本指南涵蓋了安裝、使用和代碼片段,以便輕鬆提取圖像。

為什麼要從 PDF 中提取圖像?

  • 從 PDF 中提取標誌、資訊圖表和嵌入的照片。
  • 自動化文檔數字化以便歸檔和數據處理。
  • 建立自定義的 PDF 分析器或圖像識別管道。
  • REST API-based workflow — 無需桌面軟體。

在這篇文章中,我們將涵蓋以下主題:

PDF 處理 API

GroupDocs.Parser Cloud SDK for Node.js 是一個輕量級且易於集成的 API 包裝器,允許開發人員從包括 PDF、Word、Excel 等各種文件格式中提取結構化內容,例如文本、圖像、元數據和文檔字段。

前提條件 安裝 GroupDocs.Parser Cloud SDK 的 Node.js:

npm install groupdocs-parser-cloud

GroupDocs.Cloud Dashboard 創建一個帳戶以獲取您的客戶端 ID 和客戶端密鑰以進行身份驗證。有關更多信息,請訪問這篇 article

從 PDF 中提取圖像使用 Node.js

遵循以下步驟來使用 Node.js API 開發自己的 PDF 圖像提取器:

步驟 1:設置配置。

const { ParserApi, Configuration, ImagesRequest, FileInfo, ImagesOptions } = require("groupdocs-parser-cloud");

const config = new Configuration("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET");
const parserApi = new ParserApi(config);

步驟 2:定義 PDF 文件信息和圖像提取選項。 初始化 ImagesRequest 類別的一個物件,並傳遞 ImagesOptions 類別的實例。

const fileInfo = new FileInfo();
fileInfo.filePath = "sample.pdf";

const options = new ImagesOptions();
options.fileInfo = fileInfo;

const request = new ImagesRequest(options);

步驟 3:提取圖片。 調用 images 方法從 PDF 文件中提取圖片。

parserApi.images(request).then((response) => {
    console.log("Images extracted successfully.");
    console.log(response.images);
}).catch((err) => {
    console.error("Failed to extract images:", err);
});
// 欲查閱更多範例,請訪問 https://github.com/groupdocs-parser-cloud/groupdocs-parser-cloud-node
const { ParserApi, Configuration, ImagesRequest, FileInfo, ImagesOptions } = require("groupdocs-parser-cloud");

const config = new Configuration("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET");
const parserApi = new ParserApi(config);

const fileInfo = new FileInfo();
fileInfo.filePath = "sample.pdf";

const options = new ImagesOptions();
options.fileInfo = fileInfo;

const request = new ImagesRequest(options);

parserApi.images(request).then((response) => {
    console.log("Images extracted successfully.");
    console.log(response.images);
}).catch((err) => {
    console.error("Failed to extract images:", err);
});

您可以在下方的圖片中查看上述代碼範例的輸出:

image file extractor

從 PDF 中提取圖像 via cURL

您還可以使用 GroupDocs.Parser REST API 和 cURL 提取圖像。

步驟 1 – 生成訪問令牌:

curl -X POST "https://api.groupdocs.cloud/connect/token" \
-d "grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET" \
-H "Content-Type: application/x-www-form-urlencoded"

步驟 2 – 通過 REST API 提取圖像:

curl -v -X POST "https://api.groupdocs.cloud/v1.0/parser/images" \
-H  "accept: application/json" \
-H  "authorization: Bearer {ACCESS_TOKEN}" \
-H  "Content-Type: application/json" \
-d "{  \"FileInfo\": {    \"FilePath\": \"Binder1.pdf\",    \"StorageName\": \"internal\"  },  \"OutputPath\": \"internal\",  \"StartPageNumber\": 1,  \"CountPagesToExtract\": 2}"
  • 將 替換為您生成的那個。

Online Image Extractor

如果您正在尋找一種無需編碼的解決方案,您可以使用由 GroupDocs.Parser Cloud 提供支持的免費在線 PDF Image Extractor

線上圖像提取器

結論

GroupDocs.Parser Cloud SDK for Node.js 使從 PDF 文件中提取圖像變得快速、可擴展且友好於代碼。無論您是在構建自動化腳本、內容爬蟲還是基於圖像的分析工具,這個 REST API 提供了您所需的一切,以便以編程方式孤立和導出圖像。

準備將其整合到您的工作流程中了嗎?今天就開始您的第一次 API 呼叫吧!

📚 其他資源

常見問題 – FAQs

如何從 Word 中提取圖片?

您可以使用 GroupDocs.Parser Cloud SDKs 以程式方式從 PDF 文件中提取圖像。請訪問這 link 獲取更多詳情。

定價模式是什麼?

我們提供單一的按需付費定價模式。如需進一步資訊,請訪問 pricing guide

相關文章