【问题标题】:TypeError [ERR_INVALID_ARG_TYPE]: The "original" argument must be of type functionTypeError [ERR_INVALID_ARG_TYPE]:“原始”参数必须是函数类型
【发布时间】:2020-10-31 09:13:27
【问题描述】:

我正在尝试使用 Puppeteer v5.0.0 来生成我的应用程序页面的 PDF。为了运行命令来启动进程,我使用了 Symfony 的 Process 类。该命令在我在命令行中手动键入时正常工作,但在使用 Symfony 时不起作用。

我试过了

  • 将节点版本更新到 v12.18.3,一些帖子建议我这样做
  • 使用通用网站,如https://google.com
  • 使用 php 的 exec,但由于输入卫生问题,我想使用 Symfony
  • 将输出文件路径更改为public/file.pdf(可能/tmp权限不对?)
  • 将 puppeteer 降级到 v4.0.0

这是运行命令的代码:

 $path = base_path();
        $filePath = '/tmp/pagePdf-'.time().'.pdf';
        $url = route('render.page', $request->all());

        $process = new Process(["node", "$path/resources/assets/js/takePdf.js", $url, $filePath]);
        $process->setTimeout(60);

        try{
            $process->mustRun();
            $process->wait();

            if(file_exists($filePath)){
                return response()->download($filePath)->deleteFileAfterSend();
            }else{
                return redirect()->back();
            }
        }catch(ProcessFailedException $e){
            Log::info($e->getMessage());
            report($e);

            return redirect()->back();
        }

这是takePdf.js

const puppeteer = require('puppeteer');
const dotenv = require('dotenv').config();

(async() => {
    const browser = await puppeteer.launch({
        headless: true,
        executablePath: '/usr/bin/chromium-browser',
        ignoreHTTPSErrors: true,
        args: [
            '--no-sandbox',
            '--disable-setuid-sandbox',
        ],
    });

    // Parse arguments
    var args = process.argv;
    var indexes = [2,3];
    var arg_index = {};

    arg_index[2] = 'URL';
    arg_index[3] = 'File Path';

    indexes.forEach(function(index){
        if(typeof args[index] == 'undefined'){
            var parameter = arg_index[index];
            console.log('Missing Parameter: ' + parameter);
            browser.close();
            process.exit(1);
        }
    });

    var url = args[2] + '&token=' + process.env.RENDER_TOKEN;
    var filePath = args[3];

    const page = await browser.newPage();
    await page.emulateMediaType('screen');
    await page.setViewport({
        width: 800,
        height: 600,
        deviceScaleFactor: 1,
    });
    await page.goto(url, {waitUntil: 'networkidle2'}).catch(e => void 0);
    await page.waitFor(1000);
    await page.pdf({
        path: filePath,
        format: 'A4',
        printBackground: true,
        width: '1920px',
        height: '1080px'
    });

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

当到达端点时,这是我从进程中返回的异常。再次,在命令行上运行正常。

internal/util.js:209
        throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'original', 'function');
        ^
    
    TypeError [ERR_INVALID_ARG_TYPE]: The "original" argument must be of type function
        at promisify (internal/util.js:209:11)
        at Object.<anonymous> (/home/forge/default/releases/20200709194844/node_modules/extract-zip/index.js:11:18)
        at Module._compile (module.js:652:30)
        at Object.Module._extensions..js (module.js:663:10)
        at Module.load (module.js:565:32)
        at tryModuleLoad (module.js:505:12)
        at Function.Module._load (module.js:497:3)
        at Module.require (module.js:596:17)
        at require (internal/module.js:11:18)
        at Object.<anonymous> (/home/forge/default/releases/20200709194844/node_modules/puppeteer/lib/cjs/node/BrowserFetcher.js:48:39)

【问题讨论】:

    标签: javascript php laravel symfony puppeteer


    【解决方案1】:

    当运行它试图使用不同节点版本的进程时,与命令相比,命令行返回了更高版本的节点。为了解决这个问题,我必须将节点的正确路径传递给进程。

    $process = new Process(["/path/to/node", "$path/resources/assets/js/takePdf.js", $url, $filePath]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-29
      • 1970-01-01
      • 2019-10-01
      • 2019-01-24
      • 2021-07-17
      • 2020-06-20
      • 2023-04-05
      • 2021-12-24
      相关资源
      最近更新 更多