spiritix / php-chrome-html2pdf

A PHP library for converting HTML to PDF using Google Chrome
MIT License
112 stars 29 forks source link

Viewport size not being changes. #16

Closed doonfrs closed 2 months ago

doonfrs commented 4 years ago

Hi; I used the following options to generate pdf

$html = "
        <script>

        document.write('screen.width = ' + screen.width);

        document.write('screen.height = ' + screen.height);

        </script>";

        $input->setHtml($html);
       $converter->setOptions([
            'landscape' => false,
            'printBackground' => true,
            'format' => null,
            'width' => '1440px',
            'viewport' => [
                'userAgent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36',
                'width' => 1440,
                'height' => 600,
                'isMobile' => false,
                'hasTouch' => false,
                'isLandscape' => false,
            ],
            'mediaType' => 'print',
        ]);

PDF output: screen.width = 800screen.height = 600

Version: "spiritix/php-chrome-html2pdf": "^1.4"

OS: Centos 7

doonfrs commented 4 years ago

I tracked the code, I found that the PHP code passes the options successfully to puppeteer puppeteer does not respect the viewport options for PDF.

https://github.com/puppeteer/puppeteer/issues/3357

doonfrs commented 4 years ago

The solution found here: https://github.com/puppeteer/puppeteer/issues/1183

I edited Convert.js changed

_launchBrowser()
    {
        return puppeteer.launch({
            ignoreHTTPSErrors: true,
            args: ['--no-sandbox', '--disable-web-security']
        });
    }

to

_launchBrowser()
    {
        return puppeteer.launch({
            ignoreHTTPSErrors: true,
            args: ['--no-sandbox', '--disable-web-security','--window-size=1440,600']
        });
    }

Now screen page rendered in 1440 screen width, before that page appears in mobile screen.

spiritix commented 4 years ago

Thanks for the report and the research. Indeed, it seems like this is a Puppeteer issue. We'll need to wait for a fix being provided there.