Skip to content

Commit

Permalink
Merge pull request #1 from dreadnip/use-new-sethtml-event
Browse files Browse the repository at this point in the history
Use new setHtml event
  • Loading branch information
Sander authored Feb 27, 2023
2 parents 5a9c6f1 + b83390a commit 86cba6a
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 1,587 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class TestController
): Response {
$html = $twig->render('pdf.html.twig');

// You can use the default options
$path = $pdfGenerator->generate($html, 'files/test.pdf');

// Or control everything by passing custom options
$printOptions = [
'printBackground' => true,
'displayHeaderFooter' => true,
Expand All @@ -54,10 +58,17 @@ class TestController
];

$browserOptions = [
'headless' => false,
'proxyServer' => '127.0.0.1'
];

$path = $pdfGenerator->generate($html, 'files/test.pdf', $options, $browserOptions);
$path = $pdfGenerator->generate(
html: $html,
path: 'files/test.pdf',
printOptions: $options,
browserOptions: $browserOptions,
timeout: 5000
);

return new BinaryFileResponse($path);
}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"prefer-stable": true,
"require": {
"php": "^7.4 || ^8.0",
"chrome-php/chrome": "^1.5",
"chrome-php/chrome": "^1.8",
"symfony/dependency-injection": "^3.4 || ^4.4 || ^5.2 || ^6.0",
"symfony/http-kernel": "^3.4 || ^4.4 || ^5.2 || ^6.0",
"symfony/config": "^3.4 || ^4.4 || ^5.2 || ^6.0",
Expand Down
8 changes: 5 additions & 3 deletions src/Service/PdfGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Dreadnip\ChromePdfBundle\Service;

use HeadlessChromium\BrowserFactory;
use HeadlessChromium\Page;

final class PdfGenerator
{
Expand All @@ -24,14 +25,15 @@ public function generate(
string $html,
string $path,
?array $printOptions = null,
?array $browserOptions = null
?array $browserOptions = null,
int $timeout = 30000
): string {
$browser = $this->browserFactory->createBrowser($browserOptions ?? []);

try {
$page = $browser->createPage();

$page->setHtml($html);
$page->setHtml($html, $timeout, Page::NETWORK_IDLE);

if ($printOptions === null) {
$printOptions = [
Expand All @@ -44,7 +46,7 @@ public function generate(
];
}

$page->pdf($printOptions)->saveToFile($path, 300000);
$page->pdf($printOptions)->saveToFile($path, $timeout);

return $path;
} finally {
Expand Down
1,661 changes: 79 additions & 1,582 deletions tests/test_source.html

Large diffs are not rendered by default.

0 comments on commit 86cba6a

Please sign in to comment.