当前位置: 首页 > article >正文

nodejs将pdf转换成图片并提取图片内容

pdf2pic 安装方法

安装文档地址:https://github.com/yakovmeister/pdf2image/blob/HEAD/docs/gm-installation.md

Windows下载下面两个文件,安装时没有自动设置环境变量,要分别设置到环境变量

  • Download Ghostscript Windows: https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/tag/gs952
  • Download GraphicsMagick for Windows: https://sourceforge.net/projects/graphicsmagick/files/graphicsmagick-binaries/

示例代码

实现RAG将pdf文档内容添加到向量数据库进行向量检索时,表格等数据无法识别,可以先用多模态模型对数据进行提取,然后再添加到向量数据中。这样做查询时用文本向量模型就够了。

 对pdf中所有页面进行截图,手动指定了一张图片,对这个图片中数据进行了提取。

import { fromPath } from 'pdf2pic';
import * as pdfjsLib from 'pdfjs-dist';
import OpenAI from 'openai';
import fs from 'fs';
import process from "node:process";
import 'dotenv/config';


const filename = 't';
const filePath = `./${filename}.pdf`;

const options = {
  density: 100,
  saveFilename: filename,
  savePath: "./images",
  format: "png",
  width: 600,
  height: 600
};

const convert = fromPath(filePath, options);

// 加载 PDF 文件
const loadingTask = pdfjsLib.getDocument(filePath);
const pdfDocument = await loadingTask.promise;

// 获取总页数
const totalPages = pdfDocument.numPages;

await Promise.all(
  new Array(totalPages).fill(0).map(async (_, index) => {
    await convert(index + 1, { responseType: "image" });

    console.log(`Page ${index + 1} is now converted as image`);
  })
);

try {
  // 1. 读取本地图片并转换为 Base64
  const imagePath = "./images/t.6.png";
  const imageFile = fs.readFileSync(imagePath);
  const base64Image = imageFile.toString("base64");

  const openai = new OpenAI(
    {
      // apiKey: process.env.DASHSCOPE_API_KEY,
      apiKey: process.env.OPENAI_API_KEY,
      baseURL: process.env.OPENAI_BASE_URL
    }
  );

  // 2. 构建请求参数
  const response = await openai.chat.completions.create({
    // model:'deepseek-r1',
    model: process.env.OPENAI_MODEL_Multi_Mode,
    messages: [
      {
        role: "user",
        content: [
          {
            type: "text",
            text: `
              提取图片中所有信息,根据下面不同情况分别提取对应信息:
              1、全部使用markdown格式
              2、使用图片上的原本语言,不能修改图片中文字内容,不能自动翻译成或使用其他语言
              3、段落文字正常提取,不做处理
              4、图片中表格使用markdown格式提取
              5、图片中统计图等其他图片,使用文字描述,描述开始和结束标明是在描述图片
            `
          },
          {
            type: "image_url",
            image_url: {
              url: `data:image/png;base64,${base64Image}`,
              detail: "high",
            },
          },
        ],
      },
    ],
    temperature: 0,
    max_tokens: 2048,
  });

  // 3. 输出结果
  console.log(response.choices[0].message.content);
} catch (error) {
  console.error("Error:", error);
}

http://www.kler.cn/a/566601.html

相关文章:

  • LabVIEW中三种PSD分析VI的区别与应用
  • 25护理综合研究生复试面试问题汇总 护理综合专业知识问题很全! 护理综合面试流程攻略 护理综合考研复试调剂真题汇总
  • WordPress Yawave插件 SQL注入漏洞(CVE-2025-1648)
  • Kokoro-82M TTS 实时语音合成api案例
  • leetcode 239. 滑动窗口最大值
  • 爱普生汽车用显示控制器IC:ScalerIC,汽车接口IC,相机接口IC
  • DeepSeek R1满血+火山引擎详细教程
  • Android用ExoPlayer获取视频正确的尺寸
  • ds回答-开源llm应用开发平台
  • 内容中台是什么?内容管理平台解析
  • 【Excel】 Power Query抓取多页数据导入到Excel
  • RabbitMQ 高级配置与优化:从入门到精通
  • macos uni-app 如何生成安卓APP的公钥和签名
  • Python设置阿里云镜像源教程:解决PIP安装依赖包下载速度慢的问题
  • 【2】VS Code 新建上位机项目---C#面向对象编程
  • el-tree实现双击树节点事件
  • 【官方配图】win10/win11 安装cuda 和 cudnn
  • 【大模型】大模型推理能力深度剖析:从通用模型到专业优化
  • GPT-4.5震撼登场,AI世界再掀波澜!
  • leetcode257-二叉树的所有路径