Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hangs indefinitely when exporting a password protected page (using --cookie) #250

Closed
davidnoguerol opened this issue Oct 3, 2017 · 18 comments

Comments

@davidnoguerol
Copy link

davidnoguerol commented Oct 3, 2017

I'm currently facing an issue where, if I add --cookie as an argument and pass in a sessionid, the request hangs indefinitely. However, when remove the --cookie, it works just fine but then the page exported to PDF becomes the login page of the site. I'm unable to export pages that requires some sort of authentication.

Here's a snippet example:

$options = [
    'binary' => 'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf',
    'commandOptions' => [
        'useExec'     => true,
        'escapeArgs'  => false,
        'procOptions' => array(
            // This will bypass the cmd.exe which seems to be recommended on Windows
            'bypass_shell'    => true,
            'suppress_errors' => false
        )
    ],
];
$pdf = new Pdf($options);
$pdf->addPage($link_to_html);
$pdf->send();

Is this a known issue? Are you aware of any potential causes of this issue?

Thanks in advance

@mikehaertl
Copy link
Owner

Does it work on the command line?

@hiasl
Copy link

hiasl commented Oct 5, 2017

Hi,
I can confirm davidnoguerol's issue. I have the same problem at the moment. And yes, the command is working on the command line but runs endlessly through the wrapper. And without the cookie I get the pdf with the login screen.

this is the command:
/usr/local/bin/wkhtmltopdf --cookie 'PHPSESSID' '2fa07ba4be233595b7f14808d173767c' --print-media-type 'http://www.xxx.mb/xxx/reportDetail/1' '/private/var/tmp/tmp_wkhtmlto_pdf_Khxbzg.pdf'

additional info: this happens with useExec true or false

and one more info: I just manually tried to execute the command above through php's shell_exec and it also hangs... I have no clue why. At least my MampPro seems to run as my personal account, so it should have all the permissions I have on the shell.

I'm on MacOS btw, with the latest version of wkhtmltopdf and the latest version of your wrapper.

cheers,
Matthias

@mikehaertl
Copy link
Owner

When I read "Mamp" it always rings some alarm bells. In my experience this package has quite some limitations and misterous errors are common. So this is my first suspect in this case.

It would be interesting to see if the problem also exists on a "real" server (i.e. some Linux/Unix like system).

@davidnoguerol
Copy link
Author

Hi mikehaertl, it does work on the command line. Im using a Windows server, does it have some sort of limitations on Windows servers?

@mikehaertl
Copy link
Owner

mikehaertl commented Oct 7, 2017

@davidnoguerol I have no idea as I never use any Windows system for app development/deployment. But the problem is probably related to this:

mikehaertl/php-shellcommand#20

We use this library to execute wkhtmltopdf under the hood. If you can help testing the proposed fix and leave a note on that issue there, we could change that part and see if that helps.

EDIT: The fix I mean is the one proposed in my comment here mikehaertl/php-shellcommand#20 (comment). You'd have to manually modify the code in vendor/mikehaertl/php-shellcommand/src/Command.php around line 321.

@hiasl
Copy link

hiasl commented Oct 11, 2017

I tried the solution from the other ticket. It does not solve the issue. Same endless loading and the server hangs. I will also try on Linux, but I think we should not forget that this cookie option

$pdf->setOptions([
            'cookie' => array('PHPSESSID' => $_COOKIE['PHPSESSID']),
]);

is causing the problems. When you remove the cookie config, it works. Anything else I can try in this context?

I also checked the return code of both commands (with and without cookie) on the shell, they are both 0. Also nothing else seems strange of the output of wkhtmltopdf, the only difference (in my case) is this single line:
QFont::setPixelSize: Pixel size <= 0 (0) ] 45%

Can this be causing the problem?

@mikehaertl
Copy link
Owner

Hmm, I have no idea then. Did you try to add the quiet option?

What you could also try is to skip our library here and execute the same command string you use on the command line with exec().

@hiasl
Copy link

hiasl commented Oct 11, 2017

I just tried quiet. Did not help.

But amazingly with exec(..) it did work. (I tried shell_exec before, this did not work).

Though I'm really sad to skip your lib, I have to say Thank You!

P.S. If you want me to try something else with this information, feel free to contact me.

@mikehaertl
Copy link
Owner

@hiasl Ok, thanks. You could indeed try something for me. As you can see here when you use the useExec option what happens at the heart is basically this:

exec($execCommand, $output, $this->_exitCode);

So the only difference I can see is, that the command is suffixed with a 2>&1 and that we capture output and exit code. Could you try the same in your raw exec() call? Something like:

exec('wkhtmltopdf [....] 2>&1', $output, $exitCode);
echo $output;
echo $exitCode;

If this also works I'd be quite confused. In this case it would be interesting if you could play around with the code from my https://github.com/mikehaertl/php-shellcommand a little to find out the difference.

@mikehaertl
Copy link
Owner

I tried shell_exec before, this did not work

Oh, I misread that. So before trying what I just said, did you also try the useExec option? Because only in this case exec() is used to execute the command.

@hiasl
Copy link

hiasl commented Oct 11, 2017

Hi, my assumptions were wrong. I'm using your lib inside Yii 1.1 and this seems to be the problem.
I now created an isolated pdf.php file, which only autoloads your classes and then calls them. And here it works. Including the cookie. So the issues seems to be related to Yii 1.1. Too sad :-)

@mikehaertl
Copy link
Owner

Sounds weird. There's nothing special going on in Yii so it should work the same there. Did you verify that $_COOKIE['PHPSESSID'] is not empty in Yii and that this is really the correct session id?

@hiasl
Copy link

hiasl commented Oct 11, 2017

Yes, I did. The full command I posted was taken from your class with a die($command) on line 326; So everything should be there including the Session (read dynamically from Cookie). And then I did the same again outside Yii... very strange. And the weird thing is: it also works inside Yii if I drop the cookie option.... I'm sure I'm missing something too obvious.

@mikehaertl
Copy link
Owner

Ok, I'm running out of ideas. Maybe try to disable logging in Yii - but that should also not really make a difference. You'd probably have to go into the guts of Yii to find out more.

@hiasl
Copy link

hiasl commented Oct 11, 2017

When you create a PDF from a Yii 1.1 request with a session and the page to be converted also accesses this session (by passing the session ID as cookie), then 2 requests need to be processed at the same time. and yii 1.1 is not capable of that. It waits, until the first process has finished and would then complete the second one (the one from wkhtmltopdf). And this never happens :-)

Solution: Before calling wkhtmltopdf close the session manually by calling
session_write_close()
before (in which ever way) calling wkhtmltopdf.
cheerio

@mikehaertl
Copy link
Owner

Riiight, very nice.

This has nothing to do with Yii, though, but is a common and well-known (but often forgotten 😄 ) issue with PHP's session. Just google for "PHP blocking sessions". The session file is locked while a request is being processed. So any simultaneous request using the same session will have to wait until the locked session file is released.

I wonder a bit, why you want to send a request anyway. Why not simply render the HTML and generate the PDF from that. BTW I've even created a extension for Yii 1 that helps you with this - but to be honest it's not maintained anymore. So you're on your own if you want to use it: https://github.com/mikehaertl/pdfable

@mikehaertl
Copy link
Owner

@davidnoguerol Can you confirm that this is also the issue you have?

As said above this is not limited to Yii. It's a general problem with session handling in PHP.

@ghost
Copy link

ghost commented Oct 7, 2020

session_write_close()

wonderful, it worked in moodle as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants