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

Failed with Error "Loading pages (1/6) [> ] 0% [======> ] 10%" #125

Closed
tilman opened this issue Oct 16, 2015 · 60 comments
Closed

Failed with Error "Loading pages (1/6) [> ] 0% [======> ] 10%" #125

tilman opened this issue Oct 16, 2015 · 60 comments

Comments

@tilman
Copy link

tilman commented Oct 16, 2015

I tested with following code:

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

require_once __DIR__.'/vendor/autoload.php';
use mikehaertl\wkhtmlto\Pdf;

$pdf = new Pdf('<html><head></head><body>Hello pdf</body></html>');
$pdf->binary = '/usr/local/bin/wkhtmltopdf';

if (!$pdf->saveAs('/var/www/cgi-bin/pdf/test.pdf')) {
    echo $pdf->getError();
}
?>

The Page only displays: "Loading pages (1/6) [> ] 0% [======> ] 10%"

My Testserver:

[root@dev-php-composer pdf]# apachectl -v
Server version: Apache/2.2.15 (Unix)
Server built:   Aug 24 2015 17:52:49
[root@dev-php-composer pdf]# php -v
PHP 5.3.3 (cli) (built: Jul  9 2015 17:39:00)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
[root@dev-php-composer pdf]# uname -a
Linux dev-php-composer 2.6.32-504.1.3.el6.x86_64 #1 SMP Tue Nov 11 17:57:25 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

/var/www/cgi-bin/pdf/ has this linux rights drwxr-xr-x and user apache

Any Tips to get it working?

@mikehaertl
Copy link
Owner

Hmm, not really. I've never seen this behavior myself, but there are other issues that reported the same problem. I've never found out what is causing this, though.

So any further debugging information is very welcome.

@mikehaertl
Copy link
Owner

You could also try if the ignoreWarnings option helps. But this would rather be a workaround. There should not be an error reported on success.

https://github.com/mikehaertl/phpwkhtmltopdf#wrapper-options

@r3wt
Copy link

r3wt commented Nov 6, 2015

@mikehaertl i'm having this issue. With a html table of small data, renders fine, but with html table of 1000 rows, it fails with this error message.

code is as follows:

    private function toPdf($html,$name)
    {
        $pdf = new \mikehaertl\wkhtmlto\Pdf($html);
        $pdf->binary = '/usr/local/bin/wkhtmltopdf';
        if($pdf->saveAs('/tmp/'.$name.'.pdf')){
            return file_get_contents('/tmp/'.$name.'.pdf');
        }
        throw new \exception('Failed To write PDF with message: '.$pdf->getError());
    }

@mikehaertl
Copy link
Owner

What about ignoreWarnings? Does it work if set to true?

@r3wt
Copy link

r3wt commented Nov 6, 2015

updated code still same result:

    private function toPdf($html)
    {
        $pdf = new \mikehaertl\wkhtmlto\Pdf($html);
        $pdf->binary = '/usr/local/bin/wkhtmltopdf';
        $pdf->setOptions(['ignoreWarnings'=>true]);
        if($html = $pdf->toString()){
            return $html;
        }
        throw new \exception('Failed To write PDF with message: '.$pdf->getError());
    }

@r3wt
Copy link

r3wt commented Nov 6, 2015

Oh dude. i figured it out. the twig template i'm rendering is not a valid html file. its just a html snippet. enclosing in valid document markup solved problem. here's the deal though, it only renders the table head now. no tbody. http://puu.sh/lbgdN/44ef308606.jpg

@r3wt
Copy link

r3wt commented Nov 6, 2015

my particular issue was the usage of data-uri's in image tags. letting wkhtmltopdf fetch the images with a http request solved the problem, table renders!

@snagytx
Copy link

snagytx commented Nov 13, 2015

I'm having the same issue as @tilman but that's only when I load it via php-fpm. If I run from command line php testpdf.php everything works fine. My code is very simple.

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

require_once __DIR__.'/vendor/autoload.php';
use mikehaertl\wkhtmlto\Pdf;

$pdf = new Pdf('<html><head></head><body>Hello pdf</body></html>');
$pdf->binary = '/usr/local/bin/wkhtmltopdf';
$pdf->setOptions(['ignoreWarnings'=>true]);

if (!$pdf->saveAs('test.pdf')) {
    echo $pdf->getError();
}

Any environment variable that have to be set? LD_LIBRARY_PATH contains /usr/local/lib anything else needed ?

@mikehaertl
Copy link
Owner

I wonder if it has to do with the proc_open() configuration in the underlying php-shellcommand. Could someone try the suggested fix here:

mikehaertl/php-shellcommand#9 (comment)

It's in vendor/mikehaertl/php-shellcommand/src/Command.php in the execute() method. You should change the pipe setup and insert the 2 lines with stream_set_blocking().

@juliogtz
Copy link

I did make the suggestion, using stream_set_blocking()

And Now I have this result:

Fatal error: Uncaught exception 'Exception' with message '

Could not create PDFFailed without error message: /usr/local/bin/wkhtmltopdf '/tmp/tmp_wkhtmlto_pdf_gEUI9M.html' '/tmp/tmp_wkhtmlto_pdf_K7AKMR.pdf'

Every refresh the name of the file temp change, but never create my pdf file.

Any suggest?

Thanks!

@snagytx
Copy link

snagytx commented Nov 27, 2015

I really suspect the issue here is that the environment is not set correctly. In order to fix my issue I installed the dev version(0.12.3-dev-79ff51e) - which has no dependencies to other libraries and it works just fine. I suspect some env variables were not being set in the php context. See if this resolves your issue as well.

@babeuloula
Copy link

I tried to get tmpdir with your function \mikehaertl\tmp\File::getTempDir() and i got this message :

Fatal error: Uncaught exception 'ErrorException' with message 'unlink(): open_basedir restriction in effect. File() is not within the allowed path(s): (*****)' in ****/vendor/mikehaertl/php-tmpfile/src/File.php:52
Stack trace:
#0 [internal function]: Slim\Slim::handleErrors(2, 'unlink(): open_...', '/var/www/vhosts...', 52, Array)
#1 ****/vendor/mikehaertl/php-tmpfile/src/File.php(52): unlink('')
#2 [internal function]: mikehaertl\tmp\File->__destruct()
#3 ****/vendor/digitalnature/php-ref/ref.php(1025): ReflectionClass->isCloneable()
#4 ****/vendor/digitalnature/php-ref/ref.php(2079): ref->fromReflector(Object(ReflectionClass), '\\mmkehhertl\\tmm...')
#5 ****/vendor/digitalnature in /var/www/vhosts/crm-kairos.com/subdomains/use/httpdocs/vendor/mikehaertl/php-tmpfile/src/File.php on line 52

That can you help ?

@mikehaertl
Copy link
Owner

@babeuloula On CentOS there seem to be some security restrictions in effect. You could check this comment here: #99 (comment)

@juliogtz
Copy link

No works!!!.. diable or enabled SELinux is the same.

@babeuloula
Copy link

Same for me

@mikehaertl
Copy link
Owner

It would help, if I had a code example, where the issue always happens. So that I can reproduce and investigate the problem.

@babeuloula
Copy link

My code is confidential, can you send me an email (see my profile) and I will send you a zip file.

@mikehaertl
Copy link
Owner

@babeuloula Sorry, but I'd prefer something simple.

@babeuloula
Copy link

Ok there is the code :

index.php

<?php

    error_reporting(E_ALL);
    ini_set('display_errors', 1);

    require 'vendor/autoload.php';

    $pdf = new \mikehaertl\wkhtmlto\Pdf(array(
        'no-outline',
        'binary'         => '/usr/local/bin/wkhtmltopdf',
        'commandOptions' => array(
            'procEnv' => array(
                'LANG' => 'fr_FR.UTF-8',
            ),
        ),

        'margin-top'     => 25,
        'margin-right'   => 25,
        'margin-bottom'  => 25,
        'margin-left'    => 25,

        'page-size'      => 'A4',

        'header-left'    => "Titre",
        'header-right'   => "Utilisateur",
        'header-spacing' => 10,

        'footer-left'    => 'Site Internet',
        'footer-center'  => '',
        'footer-right'   => '[page]',
        'footer-spacing' => 10,

        'javascript-delay' => 1000,
    ));


    $pdf->addPage('test.html');

    setlocale(LC_CTYPE, "fr_FR.UTF-8");
    if (!$pdf->send()) {
        throw new Exception('Could not create PDF: '.$pdf->getError());
    }

test.html

<!DOCTYPE html>
<html lang="fr">

<head>

    <title>PDF Test</title>

    <meta charset="UTF-8">

    <script src="javascript.js"></script>

    <style>
        * {
            -webkit-box-shadow  : none !important;
            -moz-box-shadow     : none !important;
            -ms-box-shadow      : none !important;
            -o-box-shadow       : none !important;
            box-shadow          : none !important;

            -webkit-text-shadow : none !important;
            -moz-text-shadow    : none !important;
            -ms-text-shadow     : none !important;
            -o-text-shadow      : none !important;
            text-shadow         : none !important;

            color               : #000;
        }

        body {
            background-color: #FFF;
            overflow: visible;
        }
    </style>

</head>

<body>


<div id="chart_1"></div>

<div id="chart_2" style="margin-top: 50mm"></div>

<div id="chart_3"></div>

<div id="chart_4" style="margin-top: 25mm"></div>

<div id="chart_5"></div>

<div id="chart_6" style="margin-top: 25mm"></div>

<div id="chart_7"></div>

<div id="chart_8" style="margin-top: 25mm"></div>


<script type="text/javascript">


    jQuery(function($){
        $("#chart_1").highcharts({
            colors: ["#FFAAAA", "#D46A6A"],
            style: {
                fontFamily: "sans-serif",
                fontSize: "12px"
            },
            credits: {
                href: "http://www.exemple.com/",
                text: "Site Internet"
            },
            chart: {
                type: "pie"
            },
            title: {
                text: "chart_1"
            },
            plotOptions: {
                series: {
                    animation: false,
                    dataLabels: {
                        enabled: true,
                        format: "<b>{point.name}</b> ({point.y:,.0f})"
                    }
                }
            },
            series: [{
                name: "chart_1",
                data: [
                    ["value 1", 0],
                    ["value 2", 1],
                ]
            }]
        });


        $("#chart_2").highcharts({
            colors: ["#C38552", "#C35252"],
            style: {
                fontFamily: "sans-serif",
                fontSize: "12px"
            },
            credits: {
                href: "http://www.exemple.com/",
                text: "Site Internet"
            },
            chart: {
                type: "bar",
                height: "250"
            },
            title: {
                text: "chart_1"
            },
            xAxis: {
                categories: [
                    "value 1",
                    "value 2",
                    "value 3",
                ],
                title: {
                    text: null
                }
            },
            yAxis: {
                min: 0,
                title: {
                    text: null,
                    align: "high"
                },
                labels: {
                    overflow: "justify"
                },
                allowDecimals: false
            },
            plotOptions: {
                bar: {
                    animation: false,
                    dataLabels: {
                        enabled: false
                    }
                }
            },
            series: [{
                name: "name 1",
                data: [
                    1,
                    1,
                    0,
                ]
            }, {
                name: "name 2",
                data: [
                    0,
                    0,
                    1,
                ]
            }]
        });


        $("#chart_3").highcharts({
            colors: [
                "#54afec",

                "#60c2c3",

                "#6dd894",

                "#7aed67",





            ],
            credits: {
                href: "http://www.exemple.com/",
                text: "Site Internet"
            },
            chart: {
                type: "pyramid",
                marginRight: 150
            },
            title: {
                text: "chart_3"
            },
            plotOptions: {
                series: {
                    animation: false,
                    dataLabels: {
                        enabled: true,
                    }
                }
            },
            series: [{
                name: "name",
                data: [
                    [
                        "value 1", 5
                    ],
                    [
                        "value 2", 1
                    ],
                    [
                        "value 3", 1
                    ],
                    [
                        "value 4", 2
                    ],
                ]
            }]
        });


        $("#chart_4").highcharts({
            colors: ["#FE353C", "#65C869"],
            style: {
                fontFamily: "sans-serif",
                fontSize: "12px"
            },
            credits: {
                href: "http://www.exemple.com/",
                text: "Site Internet"
            },
            chart: {
                type: "pie"
            },
            title: {
                text: "chart_4"
            },
            plotOptions: {
                series: {
                    animation: false,
                    dataLabels: {
                        enabled: true
                    }
                }
            },
            series: [{
                name: "name",
                data: [
                    {
                        name: "value 1",
                        y: 12
                    },
                    {
                        name: "value 2",
                        y: 1
                    }
                ]
            }]
        });



        $("#chart_5").highcharts({
            style: {
                fontFamily: "sans-serif",
                fontSize: "12px"
            },
            credits: {
                href: "http://www.exemple.com/",
                text: "Site Internet"
            },
            chart: {
                type: "bar",
                height: "250"
            },
            title: {
                text: "chart_5"
            },
            xAxis: {
                categories: [
                    "value 1",
                    "value 2",
                    "value 3",
                    "value 4",
                ],
                title: {
                    text: null
                }
            },
            yAxis: {
                min: 0,
                title: {
                    text: null,
                    align: "high"
                },
                labels: {
                    overflow: "justify"
                },
                allowDecimals: false
            },
            plotOptions: {
                bar: {
                    animation: false,
                    dataLabels: {
                        enabled: false
                    }
                }
            },
            series: [{
                name: "name 1",
                data: [
                    6,
                    1,
                    1,
                    1,
                ]
            }, {
                name: "name 2",
                data: [
                    0,
                    0,
                    0,
                    0,
                ]
            }]
        });


        $("#chart_6").highcharts({
            style: {
                fontFamily: "sans-serif",
                fontSize: "12px"
            },
            credits: {
                href: "http://www.exemple.com/",
                text: "Site Internet"
            },
            chart: {
                type: "bar",
                height: "250"
            },
            title: {
                text: "chart_1"
            },
            xAxis: {
                categories: [
                    "value 1",
                    "value 2",
                    "value 3",
                    "value 4",
                    "value 5",
                ],
                title: {
                    text: null
                }
            },
            yAxis: {
                min: 0,
                title: {
                    text: null,
                    align: "high"
                },
                labels: {
                    overflow: "justify"
                },
                allowDecimals: false
            },
            plotOptions: {
                bar: {
                    animation: false,
                    dataLabels: {
                        enabled: false
                    }
                }
            },
            series: [{
                name: "name 1",
                data: [
                    1,
                    1,
                    2,
                    2,
                    2,
                ]
            }, {
                name: "name 2",
                data: [
                    0,
                    0,
                    0,
                    0,
                    0,
                ]
            }]
        });


        $("#chart_7").highcharts({
            colors: ["#8085e9", "#f15c80", "#e4d354", "#2b908f", "#f45b5b"],
            style: {
                fontFamily: "sans-serif",
                fontSize: "12px"
            },
            credits: {
                href: "http://www.exemple.com/",
                text: "Site Internet"
            },
            chart: {
                type: "pie"
            },
            title: {
                text: "chart_7"
            },
            legend: {
                enabled: true
            },
            plotOptions: {
                pie: {
                    allowPointSelect: true,
                    cursor: "pointer",
                    depth: 35,
                    dataLabels: {
                        enabled: true,
                        format: "{point.name}"
                    }
                },
                series: {
                    animation: false,
                    dataLabels: {
                        enabled: true,
                        format: "<b>{point.name}</b> ({point.y:,.0f})"
                    }
                }
            },
            series: [{
                type: "pie",
                name: "name 1",
                data: [
                    [
                        "value 1", 1
                    ],
                    [
                        "value 2", 2
                    ],
                    [
                        "value 3", 3
                    ],
                    [
                        "value 4", 5
                    ],
                    [
                        "value 6", 6
                    ],
                ]
            }]
        });


        $("#chart_8").highcharts({
            colors: ["#8085e9", "#f15c80", "#e4d354", "#2b908f", "#f45b5b"],
            style: {
                fontFamily: "sans-serif",
                fontSize: "12px"
            },
            credits: {
                href: "http://www.exemple.com/",
                text: "Site Internet"
            },
            chart: {
                type: "column"
            },
            title: {
                text: null
            },
            plotOptions: {
                column: {
                    stacking: "normal",
                    animation: false,
                    depth: 25
                }
            },
            yAxis: {
                title: {
                    text: "chart_8"
                },
                allowDecimals: false
            },
            xAxis: {
                categories: [
                    "value 1", "value 2", "value 3", "value 4", "value 5", "value 6", "value 7"
                ]
            },
            series: [
                {
                    name: "value 1",
                    data: [
                        1,
                        2,
                        3,
                        4,
                        5,
                        6,
                        7,
                    ]
                },
                {
                    name: "value 2",
                    data: [
                        1,
                        2,
                        3,
                        4,
                        5,
                        6,
                        7,
                    ]
                },
                {
                    name: "value 3",
                    data: [
                        1,
                        2,
                        3,
                        4,
                        5,
                        6,
                        7,
                    ]
                },
                {
                    name: "value 4",
                    data: [
                        1,
                        2,
                        3,
                        4,
                        5,
                        6,
                        7,
                    ]
                },
            ]
        });


    });




</script>

</body>

</html>

javascript.js

http://code.jquery.com/jquery-2.1.4.min.js
https://code.highcharts.com/highcharts.js
https://code.highcharts.com/modules/funnel.js

@juliogtz
Copy link

Fatal error: Uncaught exception 'Exception' with message 'Could not create PDF: Failed without error message: ../usr/local/bin/wkhtmltopdf --no-outline --margin-top '25' --margin-right '25' --margin-bottom '25' --margin-left '25' --page-size 'A4' --header-left 'Titre' --header-right 'Utilisateur' --header-spacing '10' --footer-left 'Site Internet' --footer-center '' --footer-right '[page]' --footer-spacing '10' --javascript-delay '1000' 'test.html' '/tmp/tmp_wkhtmlto_pdf_5n1dkJ.pdf'' in /home/nitico5/public_html/php-pdf/test2.php:41 Stack trace: #0 {main} thrown in /home/nitico5/public_html/php-pdf/test2.php on line 41

@babeuloula
Copy link

I have the same error

@juliogtz
Copy link

  • No SELinux
  • No This code
    What other sulution would be better?
    The problem is that Centos no work good with wkhtmltopdf

@trimonkee
Copy link

Work around
Not sure how you're call wkhtmltopdf but this fixed it for me.

exec("/usr/local/bin/wkhtmltopdf ".$File_Name.".html ".$File_Name.".pdf  2>&1");

@juliogtz
Copy link

No works for me... remember that the, I'm using vps centos in imotion hosting.
#fails!!

@JayAdra
Copy link

JayAdra commented Mar 7, 2016

I am also getting this problem. On CentOS 6.7 - none of the above suggestions helped.

Weird thing is it works fine from command line. Also works fine when using Google as the page, but once trying my own page, it hangs on 10%.

Spent hours debugging and trying different things but nothing is working.

@mikehaertl
Copy link
Owner

@JayAdra Can you try to set the 'useExec' => true, in the configuration for commandOptions?

@JayAdra
Copy link

JayAdra commented Mar 7, 2016

@mikehaertl I have tried this in the past, but also tried it again now and getting the same result. Strangely, sometimes when I change some settings and run it, it generates the image, but it's a white image strip instead of the page. But then running it subsequent times causes it to hang.

@JayAdra
Copy link

JayAdra commented Mar 7, 2016

@mikehaertl If it helps, here the PHP I'm using:

$img = new Image([
    'type' => 'jpg',
    'format' => 'jpg',
    'crop-h' => 2000,
    'quality' => 50,
    'width' => 1600,
    'user-style-sheet' => '/home/*******/public_html/assets/global/admin/css/print-module-editor.css',
    'cookie' => [
        'cl_session' => session_id()
    ],
    'commandOptions' => [
        'useExec' => true
    ]
]);

$img->binary = '/usr/local/bin/wkhtmltoimage';
//$img->setPage('http://www.google.com');
$img->setPage(site_url('/******'));
$img->saveAs('/home/*******/public_html/page1.jpg');
echo "<pre>";
print_r($img);

The command produced when printing $img runs fine when copied and run through SSH.

@mikehaertl
Copy link
Owner

Do you know of anything specific which happens/processes at the 10% mark with this library?

No idea. Keep in mind, that the library is just a simple frontend for wkhtmltopdf. So I don't know what happens inside. Maybe it has to do with the progress bar output. Have you tried to add the -q option to suppress output from wkhtmltopdf?

Oh and running exec from PHP manually gives the same result.

So at least it's not really my library here but a more general problem with executing wkhtmltopdf from PHP.

@JayAdra
Copy link

JayAdra commented Mar 7, 2016

@mikehaertl Okay, I swore I tried it the same way before, but trying exec() again in PHP manually with the same command is now working.

But using the library results in it hanging.

This is the command which works in exec():

exec("/usr/local/bin/wkhtmltoimage --format 'jpg' --crop-h '2000' --quality '50' --width '1600' --user-style-sheet '/home/*****/public_html/assets/global/admin/css/print-module-editor.css' --cookie 'cl_session' 'abd5d3831b2a99b7d9f5a3130edf25a68fe6f285' 'http://jay.*****.com/users' '/home/*****/public_html/test.jpg'");

So there must be something different when executing from the library?

@mikehaertl
Copy link
Owner

If use useExec ist must be the same. Check this line here: https://github.com/mikehaertl/php-shellcommand/blob/master/src/Command.php#L293

@JayAdra
Copy link

JayAdra commented Mar 7, 2016

@mikehaertl I actually copied this code out of the execute() function and ran it manually:

$execCommand = $this->captureStdErr ? "$command 2>&1" : $command;
            exec($execCommand, $output, $this->_exitCode);
            $this->_stdOut = implode("\n", $output);
            if ($this->_exitCode!==0) {
                $this->_stdErr = $this->_stdOut;
                $this->_error = empty($this->_stdErr) ? 'Command failed' : $this->_stdErr;
                return false;
            }

And I set $command to the command I was using when running exec() and it worked. So weirdly it looks like it's the same code, but when run through the library it fails? There must be other steps in the processing before or after the execute() is called which is causing the issue.

I'm going to start stepping through some of it now and see if I can find anything. Hope this extra info gives you an idea though?

@mikehaertl
Copy link
Owner

@JayAdra The only idea I had was about the extra arguments to exec() that php-shellcommand uses. But you also copied those and it seems to run for you. Very weird.

Did you copy this into a class? Because there's $this->_exitCode, so you need this as a class property. Also I still wonder if -q option would make a difference.

@JayAdra
Copy link

JayAdra commented Mar 7, 2016

@mikehaertl Thanks for the help so far. I did change the class references to normal variables when testing it and I also tried -q but didn't help.

I think I found the issue is coming from my sessions somehow. When I use session_id() to get the session name to pass as a --cookie option, it doesn't work (it hangs). But sometimes if I try an old/different session name (manually inputted), I can get it to consistently work with that one.

I know this isn't really in the scope of your library, but if you have any ideas on why this could be, I'd appreciate the input.

@mikehaertl
Copy link
Owner

@JayAdra There are some pitfalls in PHP with locking of the session file. Maybe related? http://stackoverflow.com/questions/252907/why-does-session-start-cause-a-timeout-when-one-script-calls-another-script-usin

@JayAdra
Copy link

JayAdra commented Mar 7, 2016

@mikehaertl That's done it! You are a lifesaver Mike! Thanks for the assist with this, I really appreciate it. It's always so simple...

I hope this helps anyone else with this problem. Thanks again!

@mikehaertl
Copy link
Owner

@JayAdra Cool. Maybe you can give a quick summary of what happened and how you fixed it?

@JayAdra
Copy link

JayAdra commented Mar 7, 2016

@mikehaertl Sure.

I was trying to generate an image of a protected page (it requires authentication), so was using the user's current browser session (retrieved by using session_id()) and passing it as a cookie so the library could access the page I wanted.

This would always hang (at 10%) and I now know it's because the session was locked and couldn't be used by two scripts at the same time (first by the PHP script and the second by the wkhtmltoimage process).

So the simple fix was to add session_write_close(); before running the wkhtmltoimage function (I had previously tried adding it after, but didn't think to try it before). This closes the current session the PHP script is using and allows wkhtmltoimage library to use that session to do it's thing.

TL;DR Add session_write_close(); before your script.

@jamesgol
Copy link
Contributor

jamesgol commented Apr 10, 2017

Just wanted to comment to add a datapoint to this. Noticed a previously working setup was suddenly failing on a shared hosting provider. Manually running the command from a shell worked fine, but from the webserver with PHP 5.4 and 5.6 it always failed.

Calling it directly through exec and adding an strace shows
[pid 37473] mmap(NULL, 1073741824, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0) = -1 ENOMEM (Cannot allocate memory)

I've tried increasing my php memory limit, but I'm assuming the shared hosting provider has some aggressive limit enforcing in there.

Update: Hate using the sledgehammer approach to fixing an issue, but upgrading the version of wkhtmltopdf I was using from 0.12.2.1 to 0.12.4 appears to have resolved the issue for me.

@playinteractive
Copy link

I have the same issue. The class stopped working fine a few days ago.

@mikehaertl
Copy link
Owner

@playinteractive Did you change anything? Usually things don't just break - something must have changed.

@playinteractive
Copy link

Hi @mikehaertl, I made some changes in the html code adding time information at the end of the images:

<img src="http://example.com/test.jpg?1462851916" />

@mikehaertl
Copy link
Owner

Hmm. Not sure then. Must have to do with wkthmltopdf and how it loads images. It never was perfect. So maybe the Chrome based library that @daudmalik06 posted above works for you.

@playinteractive
Copy link

The thing with that class is that only works with chrome. This thread is talking about this issue:

wkhtmltopdf/wkhtmltopdf#1937

Is there some way to disable errors like warnings?

$pdf->setOptions(['ignoreWarnings'=>true]);

@mikehaertl
Copy link
Owner

@playinteractive Interesting, but I don't think, that we can do much about it here. And ignoreWarning should already do what you want: Even if the command exits with an error it will continue if a non-empty PDF file was created. See here: https://github.com/mikehaertl/phpwkhtmltopdf/blob/master/src/Pdf.php#L270

If you find something to improve on that code, let me know.

@playinteractive
Copy link

I tried this:

$pdf->addPage('https://www.google.com/');

And the result:

Failed loading page https://www.google.com/

But after remove "https" works fine so the problem comes from the https/ssl

@yehudahkay
Copy link

yehudahkay commented May 24, 2018

@mikehaertl not sure if i should open a new issue. The library worked for me in windows subsytem for linux with ubuntu 16 but once I went to a live environment with ubuntu 14 it wasn't working. I switched to simply run wkhtmltopdf and wkhtmltoimage with system() and all is working fine. Here is the way I installed wkhtmlpdf

wget https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.4/wkhtmltox-0.12.4_linux-generic-amd64.tar.xz
tar vxf wkhtmltox-0.12.4_linux-generic-amd64.tar.xz
cp wkhtmltox/bin/wk* /usr/local/bin/

Maybe you should put some kind of note in the readme, It took me a few days until I realized my issue was the library...but thank you very much in any case!

@mikehaertl
Copy link
Owner

@yehudahkay So you still have a problem? The note about different wkhtmltopdf versions is already in the README. Adding detailled install instructions for wkthmtltopdf is really out of the scope of this library, though. There are too many OS and versions out there.

https://github.com/mikehaertl/phpwkhtmltopdf#installation-of-wkhtmltopdf

@yehudahkay
Copy link

yehudahkay commented May 24, 2018

@mikehaertl i'm sorry if i didn't explain, i cannot use the library as it was failing for me on ubuntu 14, instead I am using system(). it took me several days to realize the problem with my code was the library and not wkhtmltopdf.

@mikehaertl
Copy link
Owner

Hmm, I'm sorry to hear that. I guess you now already found a solution that works for you. But just in case you find out what caused your problem I'm happy to add any fixes. The problem is probably rather caused by https://github.com/mikehaertl/php-shellcommand that we use to execute the command. There's also some discussion there already. So feel free to add any findings there.

@yehudahkay
Copy link

@mikehaertl thank you, i assume you are right. if i get any time i will see if i can figure anything out

@Skgtouch
Copy link

getting same error for python Any solution for this?

@oncul
Copy link

oncul commented Apr 22, 2019

When i use render url is same or assets url same domain its locking request. I think its php limitation, its waiting for end of request.

@mikehaertl
Copy link
Owner

For anyone still interested:

I've meanwhile completely refactored the way we work with the I/O streams with proc_open(). This was probably the root cause for many weird hanging issues and the fix is not really trivial. If you have a chance to give it a try, this would be great.

You'd have to replace your copy of vendor/mikehaertl/php-shellcommand with the branch 20-add-stream_set_blocking-calls from this PR: mikehaertl/php-shellcommand#41.

Any feedback welcome - but please comment on the related issue of php-shellcommand.

@mikehaertl
Copy link
Owner

I've released 2.4.1 which will use the latest version of php-shellcommand which should fix many issues with commands that generate a lot of output (which is probably the main reason for many issues).

So I'm closing this for now. If there are still issues, feel free to leave a comment.

@hirasso
Copy link

hirasso commented Nov 10, 2022

Hey there! Exactly the issue described above just appeared on one of my websites in production.

wkhtmltopdf 0.12.6.1 (with patched qt)
phpwkhtmltopdf Version 2.5.0

The weird thing is, that the PDF generation continues to work locally, with the same combination of wkhtmltopdf/phpwkhtmltopdf.

The output in production is something like:

Loading pages (1/6) [> ] 0% [======> ] 10% [=============> ] 23% [==================> ] 30% [====================> ] 34% [=======================> ] 39% [========================> ] 41%

Making it to a different percentage every time I try.

Out of curiosity I tried loading the page beforehand and using the response's body for $pdf->addPage(). This lead to a PDF being generated, but with an error message about malformed markup:

image
(...and then a line of the HTML, which I can't disclose on GitHub...)

Following this, I went to the W3 validator and let it run against the page, but there aren't any errors that point to "opening and closing tags mismatch"

At this point I'm out of ideas.

This issue was closed.
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