nodejs pppeter pdfダウンロード

3120 ワード

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();
})();
 ブラウザとpppeteerのパスをカスタマイズします.
エラー:
(node:6350)ユニオンドルPromiseRejection Warning:Error:Failed to launch chrome![0425/111753.410288:ERROR:zygotechu uhostuulinux.cc(89)]Running as root without--no-sandbox is not supported.Seehttps://crbug.com/638180.
TROUTBLESHOOTING:https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md
    at onCloose(/root/nodemumodules/pppeteer/lib/Launcher.js:342:14)    at Interface.helper.addEvent Listener(/root/nodemoodules/pppeteer/lib/Launcher.js:331:50)    at Interface.emit(events.js:194:15)    at Interface.close(readline.js:379:8)    at Sockete.onend(readline.js:157:10)    at Socket.emit(events.js:194:15)    at endReadable NT(_stremuradable.js:1125:12)    at process._tickCallCallback(internal/process/nextttcatick.js:63:19)(node:6350)UnhandleedPromiseRejection Warning:Unhanded promise rejection.This error orinated ether bythrowiwinding ininininininde of asasininininininininindididididididididididididistststststststststststaaaathththththththththththththththththththththththththththththththththththththththththththththininininininininininininininin50)[DEP 0018]Deprecation Warning:Unhanded promise rejections.In the future、promise rejection s that are not handed will terminate the Node.js pr ocess with a non-zerit code.
ブラウザの設定を参照   args:'--no-sadbox'--disable-setuid-sadbox'
  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();
})();
 完了します