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

【截图服务 +打包】pkg打包 puppeteer

目录

最后结论

windows打包成服务 

定制executablePath 

用程序来查找chrome.exe 代替上面的写配置文件

服务遇到的问题

 使用java开一个线程启动

遇到的问题与解决

版本匹配问题

打出包后的运行报错问题

linux下的安装

安装n

库缺少

程序运行后的报错

制作

运行报错与修改后成功

方式二chromedrive

实验数据

参考文档


最后结论

pkg -t win --public ./screenshots.js --output ./dist/screen.exe

服务启动:

postman调用 : 

windows打包成服务 

  

D:\web-video-platform\nssm.exe install wvp-screen  "D:\web-video-platform\chrome\screen"
rem D:\web-video-platform\nssm.exe set wvp-screen AppDirectory "D:\web-video-platform\chrome"
D:\web-video-platform\nssm.exe start wvp-screen

定制executablePath 

{
  "executablePath": "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"
}

使用方法 

const config2 = require("D:\\web-video-platform\\chrome\\puppeteer.config.json");

let chromePath = await findChromePath(winreg.HKLM,'\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\chrome.exe',"Path");
      chromePath = chromePath+ "\\chrome.exe";
      logStream.write(`I  should have it here :${chromePath}\n`);

用程序来查找chrome.exe 代替上面的写配置文件

   可以灵活根据每台主机本身的情况

async function findChromePath(myhive,mykey,myvalue) {
  const chromeKey = new winreg({
      hive: myhive,//winreg.HKLM,
      key: mykey,//'\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\chrome.exe'
  });
  try {
      const result = await new Promise((resolve, reject) => {
          chromeKey.get(myvalue, (err, result) => {
              if (err) {
                  reject(err);
              } else {
                  resolve(result);
              }
          });
      });
      console.log(result.value);
      return result.value ;
  } catch (err) {
      console.error('Chrome installation path not found in registry.');
      return null;
  }
}

服务遇到的问题

    会找不着系统默认的chrome位置 

    会有时候截图是空

 使用java开一个线程启动

     会截图几次后服务死掉

遇到的问题与解决

版本匹配问题

pkg 这里说的是v3.5,实际装的是5.8.1,没有关系;可以向下兼容。

但node 18.15.0以上的就没有能与pkg相匹配相适应 的, 所以只能选 这个 。

node,21 ,19.8.1 在工程里都试过了,工程本身可以,但pkg进行打包时,说明pkg没有找到能匹配 》19版本的实现,所以,只能选  18.15.0 

打出包后的运行报错问题

 如下解决nodejs + pkg+ puppeteer 路径问题以及 Passed function is not well-serializable 问题_passed function cannot be serialized!-CSDN博客

linux下的安装

安装n

由于gcc没有装,先装gcc ;然后再装glibc23 

linux文件版本管理,Linux下使用n来管理多版本NodeJS-CSDN博客

库缺少

 /lib64/libstdc++.so.6: version `CXXABI_1.3.9' not found (required by ../../bin/xxx)
 /lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by ../../bin/xxx)
————————————————

            Error: /lib64/libstdc++.so.6: version `CXXABI_1.3.9‘ not found-CSDN博客 

因为已经安装了gcc 9.x的版本,所以找找就好

# 查询本机的so库
find / -name "libstdc++.so.*"

cp /usr/local/gcc/...../libstdc++.so.6.0.28 /usr/lib64

ln -snf ./libstdc++.so.6.0.28 ./libstdc++.so.6

程序运行后的报错

Error: Failed to launch the browser process! /root/.cache/puppeteer/chrome/linux-123.0.6312.58/chrom

centos puppeteer问题汇总_missing x server or $display-CSDN博客

制作

pkg -t linux --public ./screenshots.js --output ./dist/screen

运行报错与修改后成功

如果 没有装浏览器,会报错

Error: Failed to launch the browser process! /root/.cache/puppeteer/chrome/l

我们直接装下chrome 

成功! 

地址 10.60.100.194: /home/java/mesh/screen/dist

方式二chromedrive

需要: chromedrive.exe 及版本号与之完全对应的chrome

这会导致安装包很大。

如下的方式,没有做成服务,每次调用都要调动chrome,耗费几秒;应该把chrome启动后,作为服务,一直放着。

public static void main(String[] args) throws Exception {
        // String path = "E:\\chromes\\107\\App\\Chrome-bin\\chrome.exe";
        // System.setProperty("webdriver.chrome.bin", path);
        System.setProperty("webdriver.chrome.driver","chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        // options.setBinary(path);

        options.addArguments("--headless");  //无界面模式
        options.addArguments("--disable-gpu");  // headless模式下,windows系统可能需要
        options.addArguments("--window-size=1920,1080");  //缺少此配置,headless模式可能点击不到元素

        options.addArguments("--remote-allow-origins=*");
        options.addArguments("--start-maximized");  //启动时最大化窗口
        WebDriver webDriver = new ChromeDriver(options);

        String url = "https://www.baidu.com/";
//        String url = "http://localhost:6767/#/wvp/alarmScreenShotMap?lnglat=121.449333,31.025822";

        webDriver.get(url);
        webDriver.manage().window().maximize();
        Thread.sleep(8000);

        WebElement element = webDriver.findElement(By.id("containerForShot"));
        File screenshotFile = ((TakesScreenshot) element).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(screenshotFile, new File("static/shot/test.png"));
        webDriver.quit();
    }

实验数据

   性能 :只启动了一个browse

    内存500M (感觉 有点多)

 

参考文档

nodejs + pkg+ puppeteer 路径问题以及 Passed function is not well-serializable 问题_passed function cannot be serialized!-CSDN博客

#打包 #exe #pkg 使用 pkg 将 nodejs 打包编译为 exe_pkg打包nodejs-CSDN博客

Releases · vercel/pkg-fetch (github.com)

主要是这个工程参考

netwild/screenshots: A small tool based on Nodejs environment, using Puppeter to take full screen screenshots of any webpage (github.com)

安装n需要的更新 :

升级glibc (gcc make等)

 CentOS上升级glibc2.17至glibc2.31_glibc 升级-CSDN博客


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

相关文章:

  • iOS 打包上传保存You do not have required contracts to perform an operation
  • 深入了解CSS混合模式
  • 使用 FHE 实现加密大语言模型
  • 机器人--手眼标定算法
  • Android - NDK:jni传递数组参数,获取数组的返回值
  • 【Hot100】LeetCode—300. 最长递增子序列
  • 【Python】selenium实现滚动条滑动效果
  • 市面上有哪些高效财税自动化软件
  • CCF推荐B类会议和期刊总结:(计算机网络领域)
  • 在人工智能与机器学习领域的深度探索:技术价值的全面剖析与产品经理的角色深化
  • 黑马点评24—原理—Redis数据结构
  • Github 2024-09-06 Java开源项目日报Top10
  • 口语笔记——现在完成时
  • Tomcat服务器安装SSL证书教程
  • sqli-labs靶场自动化利用工具——第6关
  • JavaWeb【day09】--(Mybatis)
  • 【程序员必备】如何通过AI提升编程效率,轻松应对复杂代码
  • flink增量检查点降低状态依赖实现的详细步骤
  • 入职思维转变与成长之路(讲座笔记)
  • LRU go cache的实现