语法错误:缺少)在Rails应用程序中导出PDF后的参数列表

时间:2017-10-24 04:53:05

标签: javascript ruby-on-rails export-to-pdf

我有一个语法错误,我正在考虑难以理解。下面的文件是exportPDF.js,用于将一些数据导出到rails中的后台作业中的PDF。

在我的日志中,我在运行延迟作业时看到此错误。

launchChrome().then(async chrome => {
                    ^^^^^

SyntaxError: missing ) after argument list
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:389:7)
    at startup (bootstrap_node.js:149:9)
[Worker(host:MYMACBOOK-MacBook-Air.local pid:95907)] Job ActiveJob::QueueAdapters::DelayedJobAdapter::JobWrapper (id=8) (queue=default) FAILED (3 prior attempts) with Errno::ENOENT: No such file or directory @ rb_sysopen - /Users/alexanderkehaya/Desktop/redimatch/tmp/pdfs_addresses_d88439d0d90259b6bb55908f36163450.pdf

// exportPDF.js

const chromeLauncher = require('chrome-launcher');
const argv = require('minimist')(process.argv.slice(2))
const CDP = require('chrome-remote-interface');
const file = require('fs');

const viewportWidth = 1440;
const viewportHeight =  900;
const url = argv.url
const outputFileName = argv.o

function launchChrome(headless=true) {
  return chromeLauncher.launch({
    port: 9222, // Uncomment to force a specific port of your choice.
    chromeFlags: [
      '--window-size=412,732',
      '--disable-gpu',
      headless ? '--headless' : ''
    ]
  });
}

launchChrome().then(async chrome => {
  console.log(chrome.port)

  const client = CDP({ port: chrome.port }, async (client) => {
    console.log('CDP Running')

    const {DOM, Emulation, Network, Page, Runtime} = client;

    // Enable events on domains we are interested in.
    await Page.enable();
    await DOM.enable();
    await Network.enable();
    console.log('events enabled')

  // Set up viewport resolution, etc.
    const deviceMetrics = {
      width: viewportWidth,
      height: viewportHeight,
      deviceScaleFactor: 0,
      mobile: false,
      fitWindow: false,
    };

    await Emulation.setDeviceMetricsOverride(deviceMetrics);
    await Emulation.setVisibleSize({width: viewportWidth, height: viewportHeight});
    await Page.navigate({url});

    Page.loadEventFired(async () => {
      // If the `full` CLI option was passed, we need to measure the height of
      // the rendered page and use Emulation.setVisibleSize
      const {root: {nodeId: documentNodeId}} = await DOM.getDocument();
      const {nodeId: bodyNodeId} = await DOM.querySelector({
        selector: 'body',
        nodeId: documentNodeId,
      });
      const {model: {height}} = await DOM.getBoxModel({nodeId: bodyNodeId});

      console.log('about to check window status')
      let v = await Runtime.evaluate({expression: 'window.status'})
      console.log('got first window status ' + v.result.value)

      while (!(v.result.value === 'READY FOR DOWNLOAD')) {
        console.log(v.result.value)
        v = await Runtime.evaluate({expression: 'window.status'})
      }


      const screenshot = await Page.printToPDF();
      const buffer = new Buffer(screenshot.data, 'base64');
      console.log(outputFileName)
      file.writeFile(outputFileName, buffer, 'base64', function(err) {
        if (err) {
          console.error(err);
        } else {
          console.log('PDF saved');
        }
        client.close();
        chrome.kill();
      });

    });


  })


}).catch((err) => console.log(err));

我错过了什么?我看到它确切地告诉我错误是什么,但我仍然有点新的JavaScript,没有使用异步,并且不确定错误在代码中的位置。

如果有帮助,这是一个运行rails 5和ruby 2.3.3的rails应用程序

谢谢你看看!

1 个答案:

答案 0 :(得分:0)

尝试将chrome放在大括号内

launchChrome().then(async (chrome) => {

<强>更新

嘿,您的代码似乎没有语法错误。您可以验证语法here

问题可能是你正在使用的库有问题。