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

【截图服务 +打包】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博客

Chrome浏览器版本和chromedriver兼容对照_chromedriver版本对应-CSDN博客

Release 109.0.5414.120 · ulixee/chrome-versions (github.com)


http://www.mrgr.cn/news/25230.html

相关文章:

  • web前端面试题精选
  • 众望所归!业内三大刊之首,终于荣升1区TOP,预测明年IF稳涨
  • gdb/cgdb
  • 类原生补全计划:让你的安卓类原生系统更好用更顺手
  • JavaScript 展开运算符 ...
  • 多邻国 v5.166.3 解锁版 零基础轻松学习多国语言
  • HAProxy:高性能的负载均衡与代理解决方案
  • 牛!6个大模型的核心技术!
  • 1 创建公司代码
  • B2B销售:成功所需的工具
  • [ComfyUI]Flux:风格参考IPAdapter安装使用介绍,已经支持Flux
  • TikTok的IP与流量曝光有什么关系
  • 从降价到拓新,老二次元米哈游坐不住了
  • 创客匠人_完美模型!一招让你轻松实现公、私域互通!
  • IT 服务管理标准介绍
  • Spring扩展点系列-ApplicationContextAwareProcessor
  • python-确定进制
  • 按包边(边框)尺寸分类异形创意圆形(饼/盘)LED显示屏有哪些种类
  • 安装MongoDB启动报错:找不到共享库文件libcrypto.so.10
  • CSP-J 算法基础 二分查找与二分答案