使用js批量下载图片,并转为pdf 最后合并 转word 可以复制内容

下载图片的方法

说明imgUrls就是一个数组,保存了需要下载图片的url地址。

javascript 复制代码
//下载方法
const downloadRes = async (url, name) => {
  let response = await fetch(url) // 内容转变成blob地址
  let blob = await response.blob() // 创建隐藏的可下载链接
  let objectUrl = window.URL.createObjectURL(blob)
  let a = document.createElement("a")
  a.href = objectUrl
  a.download = name
  a.click()
  a.remove()
}

//自动下载图片
imgUrls.forEach((url, index) => {
  setTimeout(() => {
    downloadRes(url, index+".jpg")
  }, 1000 * index)
})

效果

图片转pdf

这里借助的是nodejs。

需要安装两个库

用来实现图片转pdf

js 复制代码
npm install puppeteer

合并的js,下面有两个参数 convertImagesToPDF('E:\ChromeDownLoad', 'E:\pdf\') 第一个参数是图片保存的地方,如果用我上面的js保存的图片,图片将以下面的方式保存 我们的js代码也会按照0.jpg,1.jpg,2.jpg..的顺序生成,0.pdf,1.pdf,2.pdf..最后合并为merge.pdf。保存的位置为convertImagesToPDF 方法第二个参数指定的地方。 生成的pdf(背景色为pdf软件自带的) 需要执行的js。可以保存为jpg2pdf.js

javascript 复制代码
const puppeteer = require('puppeteer');
const fs = require('fs');
const path = require('path');


async function convertImagesToPDF(imageDir, outputPDFPath) {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();

  // 设置PDF页面大小  
  await page.setViewport({ width: 842, height: 595 }); // A4尺寸  

  // 读取图片目录中的图片文件  
  const imageFiles = fs.readdirSync(imageDir).filter(file => /\.jpg$/.test(file));

  // 对图片文件名进行排序  
  imageFiles.sort((a, b) => {
    const numA = parseInt(a.match(/(\d+)/)[0], 10);
    const numB = parseInt(b.match(/(\d+)/)[0], 10);
    return numA - numB;
  });

  // 初始化一个Buffer数组来收集所有页面的内容  
  const pdfBuffers = [];
  var index = 0;
  // 为每张图片创建一个PDF页面  
  let pdfFiles = [];
  const htmlTemp = `  
  <!DOCTYPE html>  
  <html>  
    <head>  
      <style>  
        body, html {  
          margin: 0;  
          padding: 0;  
          width: 100%;  
          height: 100%;  
        }  
        img {  
          width: auto;  
          height: 100%;  
          object-fit: cover;  
        }  
      </style>  
    </head>  
    <body>  
        <imgHolder>
    </body>  
  </html>  
`;
  let htmlContent = "";
  let pdfPath = outputPDFPath + "merge.pdf"
  for (const imageFile of imageFiles) {
    const imagePath = path.join(imageDir, imageFile);
    console.log(`Processing image: ${imagePath}`); // 打印当前处理的图片路径
    const page = await browser.newPage();
    // 设置视口大小(可选,根据图片大小调整)  
    await page.setViewport({ width: 800, height: 600 });

    pdfFiles.push(pdfPath);
    // 创建包含图片的 HTML 内容  
    htmlContent += `<p> <img src="${path.resolve(__dirname, imagePath)}" alt="Fullscreen Image">  </p>`;
  }
  // 将 HTML 内容写入临时文件  
  htmlContent = htmlTemp.replace("<imgHolder>", htmlContent);
  const tempHtmlFile = fs.createWriteStream('./temp.html');
  tempHtmlFile.write(htmlContent);
  await tempHtmlFile.end();

  // 打开临时文件并生成 PDF  
  await page.goto(`file://${path.resolve(__dirname, 'temp.html')}`, { waitUntil: 'networkidle0' });
  await page.pdf({ path: pdfPath, format: 'A4' }); // 可以根据需要调整格式  

  // 清理临时文件  
  fs.unlinkSync('./temp.html');
  // 关闭浏览器  
  await browser.close();

}


// 使用示例  
convertImagesToPDF('E:\\ChromeDownLoad', 'E:\\pdf\\')
  .then(() => console.log('PDF created successfully!'))
  .catch(err => console.error('Error:', err));

node js的执行

只需要在cmd中执行 node jpg2pdf.js 即可 需要把上面的jpg2pdf.js保存在node 保存包的地方。也就是我们在那个目录执行。node install xxx 就在那个目录执行 jpg2pdf.js 因为这个目录有node_modules 文件夹,里面有我们安装的第三方包

转word

使用这个网站,微信扫码登录,每天三次免费机会。可以使用OCT将图片转为文字。 超级PDF

转换后的效果

文字可以选择复制了

相关推荐
掘了4 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
崔庆才丨静觅4 小时前
实用免费的 Short URL 短链接 API 对接说明
前端
崔庆才丨静觅4 小时前
5分钟快速搭建 AI 平台并用它赚钱!
前端
崔庆才丨静觅5 小时前
比官方便宜一半以上!Midjourney API 申请及使用
前端
Moment5 小时前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
崔庆才丨静觅5 小时前
刷屏全网的“nano-banana”API接入指南!0.1元/张量产高清创意图,开发者必藏
前端
剪刀石头布啊5 小时前
jwt介绍
前端
爱敲代码的小鱼5 小时前
AJAX(异步交互的技术来实现从服务端中获取数据):
前端·javascript·ajax
Cobyte5 小时前
AI全栈实战:使用 Python+LangChain+Vue3 构建一个 LLM 聊天应用
前端·后端·aigc
NEXT066 小时前
前端算法:从 O(n²) 到 O(n),列表转树的极致优化
前端·数据结构·算法