nodejs puppeteer pdf下载

linux安装 略;

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  var route = process.argv[2];
  var pageName = process.argv[3];
  await page.goto(route, {waitUntil: 'networkidle2'});
  await page.pdf({ path: pageName, scale: 1, landscape: true, printBackground: true, format: "A4", preferCSSPageSize: true, displayHeaderFooter: false, margin: { left: "10mm", right:"9.5mm",top:"10mm",bottom:"9.5mm"}})

  await browser.close();
})();

 自定义浏览器和puppeteer路径

报错:

(node:6350) UnhandledPromiseRejectionWarning: Error: Failed to launch chrome!
[0425/111753.410288:ERROR:zygote_host_impl_linux.cc(89)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.


TROUBLESHOOTING: https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md

    at onClose (/root/node_modules/puppeteer/lib/Launcher.js:342:14)
    at Interface.helper.addEventListener (/root/node_modules/puppeteer/lib/Launcher.js:331:50)
    at Interface.emit (events.js:194:15)
    at Interface.close (readline.js:379:8)
    at Socket.onend (readline.js:157:10)
    at Socket.emit (events.js:194:15)
    at endReadableNT (_stream_readable.js:1125:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)
(node:6350) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or b
y rejecting a promise which was not handled with .catch(). (rejection id: 1)(node:6350) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js pr
ocess with a non-zero exit code.

引用浏览器设置   args: ['--no-sandbox', '--disable-setuid-sandbox']

  const puppeteer = require('/root/node_modules/puppeteer');
 const path = require('path');

(async () => {
 //   const browser = await puppeteer.launch(); 
	  const browser = await puppeteer.launch({
  // 这里注意路径指向可执行的浏览器。
  // 各平台路径可以在 node_modules/puppeteer-core/lib/BrowserFetcher.js 中找到
  // Mac 为 '下载文件解压路径/Chromium.app/Contents/MacOS/Chromium'
  // Linux 为 '下载文件解压路径/chrome'
  // Windows 为 '下载文件解压路径/chrome.exe'
  args: ['--no-sandbox', '--disable-setuid-sandbox'],
  executablePath: path.resolve('/root/node_modules/puppeteer/.local-chromium/linux-641577/chrome-linux/chrome')
 });
  const page = await browser.newPage();
  var route = process.argv[2];
  var pageName = process.argv[3];
  await page.goto(route, {waitUntil: 'networkidle2'});
  await page.pdf({ path: pageName, scale: 1, landscape: true, printBackground: true, format: "A4", preferCSSPageSize: true, displayHeaderFooter: false, margin: { left: "10mm", right:"9.5mm",top:"10mm",bottom:"9.5mm"}})

  await browser.close();
})();

 完成。

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值