chrome-php / chrome

Instrument headless chrome/chromium instances from PHP
MIT License
2.2k stars 269 forks source link

Sometimes content is not fully loaded. How can we check if it is fully loaded or not #591

Closed cavid90 closed 4 months ago

cavid90 commented 6 months ago

Hello. I have created cv builder chrome php. But have one problem - sometimes content is like not fully loaded. Seems like css, images are loaded, but text is not. How can I solve it ? From screenshot seems like it was still loading. I added part of code also.

Снимок экрана 2023-12-31 в 19 52 24
          `$page = $browser->createPage();
            $page->setViewport(1440, 1000)->await();

            $page->navigate($url)->waitForNavigation(Page::LOAD);
            $page->pdf([
                'printBackground' => true,
                'preferCSSPageSize' => false,
                'marginTop'           => (float)$cv->cvType->margin_top,              // defaults to ~0.4 (must be a float, value in inches)
                'marginBottom'        => (float)$cv->cvType->margin_bottom,              // defaults to ~0.4 (must be a float, value in inches)
                'marginLeft'          => (float)$cv->cvType->margin_left,              // defaults to ~0.4 (must be a float, value in inches)
                'marginRight'         => (float)$cv->cvType->margin_right,
                'paperWidth'          => 8.5,              // defaults to 8.5 (must be a float, value in inches)
                'paperHeight'         => 11.0,             // defaults to 11.0 (must be a float, value in inches)
                'scale'               => (float)$cv->cvType->scale,
                'displayHeaderFooter' => true,
                'headerTemplate' => '<div style="font-size: 10px"></div>',
                'footerTemplate' => '<div style="font-size: 10px; text-align: center; margin: 0 auto ">Created with </div><div style="font-size: 10px; margin-right: 50px; "> - <span class="pageNumber">pagenumber</span> / <span class="totalPages">totalPages</span>  - </div>'
            ])->saveToFile($newFileFullPath);`
enricodias commented 6 months ago

You can wait for different events, like $page->waitForNavigation('DOMContentLoaded');, but javascript can change the whole page at any time. You can try to make a loop checking if a specific content is present in the page.

cavid90 commented 6 months ago

I tried only $page->waitForNavigation('DOMContentLoaded'); . But then css and images did not load. May be I should wait both $page->waitForNavigation('DOMContentLoaded'); and $page->waitForNavigation(Page::LOAD); ? If yes then how should be the code ?

GrahamCampbell commented 6 months ago

You can fix that by loading the CSS in the head of the page.

GrahamCampbell commented 6 months ago

That will also fix the bug in your website where people see the page rendered improperly, while it is loading.

cavid90 commented 6 months ago

As I understood I should get the content of css file in the head between style tag

cavid90 commented 4 months ago

I checked loading css in head of page. But it did not help. There must be another way

cavid90 commented 4 months ago

I found what was the problem. In html font was loaded from google. And sometimes it was not able to load them and that is why pdf contents were not visible. Now fonts are loaded from my own server and everything is ok