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

news/2025/3/1 3:36:34/

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中所有页面进行截图,手动指定了一张图片,对这个图片中数据进行了提取。

javascript">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. 读取本地图片并转换为 Base64const 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.ppmy.cn/news/1575671.html

相关文章

Maven+SSM+SpringBoot+Mybatis-Plus

SSM技术栈:spring6、springmvc、mybatis、springboot3、mybatis-plus、druid; 前端: node、npm、vue 快速掌握:全新SSMSpring BootMyBatis-Plus实战精讲

mapbox基础,使用geojson加载heatmap热力图层

👨‍⚕️ 主页: gis分享者 👨‍⚕️ 感谢各位大佬 点赞👍 收藏⭐ 留言📝 加关注✅! 👨‍⚕️ 收录于专栏:mapbox 从入门到精通 文章目录 一、🍀前言1.1 ☘️mapboxgl.Map 地图对象1.2 ☘️mapboxgl.Map style属性1.3 ☘️heatmap热力图层样式二、🍀使用geojs…

C高级——shell(3)

一、shell的选择结构 1.回顾:C语言的选择结构:if , if else if ,if else,switch (switch的执行速度最快) 2.shell的选择结构: 单分支if 双分支 if else 多分支if elif case..in 1.1 shell的选择结构的格式 --------C语言的格式--…

一个非常好用便捷的web自动化爬虫工具Playwright

相比于selenium,Playwright不需要在本地安装浏览器和对应版本的chromedriver,之前一度苦恼谷歌浏览器经常自动更新,需要更换旧的浏览器才有对应的driver,现在不需要考虑浏览器,这点非常nice,以及Playwright…

Ubuntu22上安装MySQL8启动成功,远程无法连接

解决步骤: 1.首先验证网络是否通 ping Linux服务器ip 2.如果网络通,验证是否开放了访问 2.1在服务器上使用 sudo mysql -u root -p 并输入密码连接到mysql 2.2执行 SELECT User, Host FROM mysql.user; 查找一个Host列为’%的用户,这表示允许从任何主机连…

【无人集群系列---无人机集群编队算法】

【无人集群系列---无人机集群编队算法】 一、核心目标二、主流编队控制方法1. 领航-跟随法(Leader-Follower)2. 虚拟结构法(Virtual Structure)3. 行为法(Behavior-Based)4. 人工势场法(Artific…

实现 Leaflet 多类型点位标记与聚合功能的实战经验分享

在现代的地理信息系统(GIS)应用中,地图功能是不可或缺的一部分。无论是展示商业网点、旅游景点还是公共服务设施,地图都能以直观的方式呈现数据。然而,当数据量较大时,地图上可能会出现大量的标记点&#x…

校园快递平台系统(小程序论文源码调试讲解)

第4章 系统设计 用户对着浏览器操作,肯定会出现某些不可预料的问题,但是不代表着系统对于用户在浏览器上的操作不进行处理,所以说,要提前考虑可能会出现的问题。 4.1 系统设计思想 系统设计,肯定要把设计的思想进行统…