- Web server: nginx
- Web服务器:nginx
- PHP framework: Laravel 5.5
- PHP开发框架:Laravel 5.5
- Library to generate PDF: puppeteer
- 库中生成PDF: puppeteer
So what I want to do is, when user click on the Download button, then the server will do the following:
所以我想做的是,当用户点击下载按钮时,服务器会做如下操作:
- Construct a HTML file with content
- 构造一个包含内容的HTML文件。
- run the following code
- 运行下面的代码
Laravel controller
Laravel控制器
$shell_output = shell_exec('`which node` ' . base_path('bin/export.js') . ' ' . $html_file . ' ' . $pdf_file . ' A4');
print($shell_output); // <------- empty output
$workable_output = shell_exec('echo `which node`');
print($shell_output); // <------- THIS IS SHOWING "/usr/local/bin/node"
the export.js content
出口。js的内容
const puppeteer = require('puppeteer');
var filename = process.argv[2];
var output = process.argv[3];
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setViewport({ width: 1000, height: 1415 });
await page.goto('file://' + filename, { waitUntil: 'networkidle2' });
await page.emulateMedia('print');
await page.pdf({
path: output,
format: 'A4',
printBackground: true,
landscape: false,
margin: {
top: '60px',
right: '20px',
bottom: '30px',
left: '20px'
}
});
await browser.close();
})();
But the problem is, no PDF file is generated, and the $shell_output
also empty.
但是问题是,没有生成PDF文件,$shell_output也为空。
Then I try the run the shell command in terminal, and it works perfectly. i.e.
然后我尝试在终端上运行shell命令,它运行得很好。即。
$ `which node` export.js content.html content.pdf A4
Again, I've tried to create a php file, and run with php-cli, i.e.
同样,我尝试过创建一个php文件,并使用php-cli(即php-cli)运行。
$ php test.php
the content of test.php as below
测试的内容。php如下
<?php
$shell_output = shell_exec('env DEBUG="puppeteer:*" `which node` export.js content.html content.pdf A4');
echo $shell_output;
Server log
/var/log/nginx/error.log
/var/log/nginx/error.log
2018/03/13 10:37:05 [error] 13017#13017: send() failed (111: Connection refused) while resolving, resolver: 127.0.0.1:53
P/S: I'm hosted in AWS EC2. This has no problem when I run in local machine.
P/S:我托管在AWS EC2上。当我在本地机器上运行时,这没有问题。
Update on 2018-03-14 14:55PM (UTC+8)
I tried to put the code to tests folder in Laravel
我试图把代码放到Laravel的测试文件夹中
./tests/Unit/ExampleTest.php
/测试/单元/ ExampleTest.php
$shell_output = shell_exec('`which node` ' . base_path('bin/export.js') . ' ' . $html_file . ' ' . $pdf_file . ' A4');
And run with ubuntu user, it works
和ubuntu用户一起运行,它是有效的
ubuntu@ip-xxx-xx-xx-xx:/var/www/project$ ./vendor/phpunit/phpunit/phpunit --filter ExampleTest
PHPUnit 6.3.0 by Sebastian Bergmann and contributors.
.
. 2 / 2 (100%)
Time: 4.82 seconds, Memory: 14.00MB
BUT, when I try to run with www-data user, it failed
但是,当我尝试使用www-data用户运行时,它失败了
ubuntu@ip-xxx-xx-xx-xx:/var/www/project$ sudo -u www-data ./vendor/phpunit/phpunit/phpunit --filter ExampleTest
PHPUnit 6.3.0 by Sebastian Bergmann and contributors.
.(node:7991) UnhandledPromiseRejectionWarning: Error: spawn EACCES
at ChildProcess.spawn (internal/child_process.js:330:11)
at Object.exports.spawn (child_process.js:500:9)
at Function.launch (/var/www/project/node_modules/puppeteer/lib/Launcher.js:106:40)
at <anonymous>
(node:7991) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:7991) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
. 2 / 2 (100%)
Time: 276 ms, Memory: 14.00MB
OK (2 tests, 2 assertions)
Any idea where's the problem?
有问题吗?
1 个解决方案
#1
0
I've found the problem, it's caused by the chrome
executable itself
我发现了问题,它是由chrome的可执行文件本身引起的。
Solved by
解决了
$ chmod 777 /var/www/project/node_modules/puppeteer/.local-chromium/linux-536395/chrome-linux/*
Found this from GitHub issue.
这是从GitHub上找到的。
#1
0
I've found the problem, it's caused by the chrome
executable itself
我发现了问题,它是由chrome的可执行文件本身引起的。
Solved by
解决了
$ chmod 777 /var/www/project/node_modules/puppeteer/.local-chromium/linux-536395/chrome-linux/*
Found this from GitHub issue.
这是从GitHub上找到的。