mikehaertl / phpwkhtmltopdf

A slim PHP wrapper around wkhtmltopdf with an easy to use and clean OOP interface
MIT License
1.6k stars 238 forks source link

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

Closed tilman closed 5 years ago

tilman commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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 commented 8 years ago

@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 commented 8 years ago

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

r3wt commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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

https://github.com/mikehaertl/php-shellcommand/issues/9#issuecomment-153601218

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 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

@babeuloula On CentOS there seem to be some security restrictions in effect. You could check this comment here: https://github.com/mikehaertl/phpwkhtmltopdf/issues/99#issuecomment-101675909

juliogtz commented 8 years ago

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

babeuloula commented 8 years ago

Same for me

mikehaertl commented 8 years ago

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

babeuloula commented 8 years ago

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

mikehaertl commented 8 years ago

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

babeuloula commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

I have the same error

juliogtz commented 8 years ago
trimonkee commented 8 years ago

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 commented 8 years ago

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

fails!!

JayAdra commented 8 years ago

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 commented 8 years ago

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

JayAdra commented 8 years ago

@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 commented 8 years ago

@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 commented 8 years ago

@JayAdra I'm sorry then, but I'm really out of ideas. I guess you've also alreaqdy checked the discussion here? https://github.com/mikehaertl/php-shellcommand/issues/9#issuecomment-153601218

I'd still be happy to know what's going on. The execution code itself is not really complicated, so not sure what should be wrong with it.

mikehaertl commented 8 years ago

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

@JayAdra Does it also work if you use exec($command) from PHP?

JayAdra commented 8 years ago

@mikehaertl I tried the shellcommand fix, but didn't help. With that "fix" applied though, I get "failed without message" rather than getting to 10% and hanging. If I useExec though, it goes to 10% as normal and hangs, so I don't think that change did any good.

I appreciate the suggestions Mike. Do you know of anything specific which happens/processes at the 10% mark with this library? There must be some reason it stops there specifically for so many people?

JayAdra commented 8 years ago

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

mikehaertl commented 8 years ago

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 commented 8 years ago

@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 commented 8 years ago

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 commented 8 years ago

@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 commented 8 years ago

@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 commented 8 years ago

@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 commented 8 years ago

@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 commented 8 years ago

@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 commented 8 years ago

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

JayAdra commented 8 years ago

@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 commented 7 years ago

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 commented 7 years ago

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

mikehaertl commented 7 years ago

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

playinteractive commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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

https://github.com/wkhtmltopdf/wkhtmltopdf/issues/1937

Is there some way to disable errors like warnings?

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

mikehaertl commented 7 years ago

@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 commented 7 years ago

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