Skip to content

Commit

Permalink
Feature: Custom Header and Footer HTML (#152)
Browse files Browse the repository at this point in the history
* Tests for custom header and footer and hiding header and footer

* Implementation

* Change parameter name

* Shorten sentence
  • Loading branch information
adrianspacely authored and freekmurze committed Jan 20, 2018
1 parent 8fd1d69 commit 341805e
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 1 deletion.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,14 +283,26 @@ Browsershot::html($someHtml)

#### Headers and footers

By default a PDF will not show the header and a footer generated by Chrome. Here's how you can make the header and footer appear.
By default a PDF will not show the header and a footer generated by Chrome. Here's how you can make the header and footer appear. You can also provide a custom HTML template for the header and footer.

```php
Browsershot::html($someHtml)
->showBrowserHeaderAndFooter()
->headerHtml($someHtml)
->footerHtml($someHtml)
->save('example.pdf');
```

In the header and footer HTML, any tags with the following classes will have its printing value injected into its contents.

* `date` formatted print date
* `title` document title
* `url` document location
* `pageNumber` current page number
* `totalPages` total pages in the document

To hide the header or footer, you can call either `hideHeader` or `hideFooter`.

#### Backgrounds

By default, the resulting PDF will not show the background of the html page. If you do want the background to be included you can call `showBackground`:
Expand Down
20 changes: 20 additions & 0 deletions src/Browsershot.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,26 @@ public function hideBrowserHeaderAndFooter()
return $this->setOption('displayHeaderFooter', false);
}

public function hideHeader()
{
return $this->headerHtml('<p></p>');
}

public function hideFooter()
{
return $this->footerHtml('<p></p>');
}

public function headerHtml(string $html)
{
return $this->setOption('headerTemplate', $html);
}

public function footerHtml(string $html)
{
return $this->setOption('footerTemplate', $html);
}

public function deviceScaleFactor(int $deviceScaleFactor)
{
// Google Chrome currently supports values of 1, 2, and 3.
Expand Down
140 changes: 140 additions & 0 deletions tests/BrowsershotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,146 @@ public function it_can_create_a_command_to_generate_a_pdf()
], $command);
}

/** @test */
public function it_can_create_a_command_to_generate_a_pdf_with_a_custom_header()
{
$command = Browsershot::url('https://example.com')
->showBackground()
->landscape()
->margins(10, 20, 30, 40)
->pages('1-3')
->paperSize(210, 148)
->showBrowserHeaderAndFooter()
->headerHtml('<p>Test Header</p>')
->createPdfCommand('screenshot.pdf');

$this->assertEquals([
'url' => 'https://example.com',
'action' => 'pdf',
'options' => [
'path' => 'screenshot.pdf',
'printBackground' => true,
'landscape' => true,
'margin' => ['top' => '10mm', 'right' => '20mm', 'bottom' => '30mm', 'left' => '40mm'],
'pageRanges' => '1-3',
'width' => '210mm',
'height' => '148mm',
'displayHeaderFooter' => true,
'headerTemplate' => '<p>Test Header</p>',
'viewport' => [
'width' => 800,
'height' => 600,
],
'args' => [],
],
], $command);
}

/** @test */
public function it_can_create_a_command_to_generate_a_pdf_with_a_custom_footer()
{
$command = Browsershot::url('https://example.com')
->showBackground()
->landscape()
->margins(10, 20, 30, 40)
->pages('1-3')
->paperSize(210, 148)
->showBrowserHeaderAndFooter()
->footerHtml('<p>Test Footer</p>')
->createPdfCommand('screenshot.pdf');

$this->assertEquals([
'url' => 'https://example.com',
'action' => 'pdf',
'options' => [
'path' => 'screenshot.pdf',
'printBackground' => true,
'landscape' => true,
'margin' => ['top' => '10mm', 'right' => '20mm', 'bottom' => '30mm', 'left' => '40mm'],
'pageRanges' => '1-3',
'width' => '210mm',
'height' => '148mm',
'displayHeaderFooter' => true,
'footerTemplate' => '<p>Test Footer</p>',
'viewport' => [
'width' => 800,
'height' => 600,
],
'args' => [],
],
], $command);
}

/** @test */
public function it_can_create_a_command_to_generate_a_pdf_with_the_header_hidden()
{
$command = Browsershot::url('https://example.com')
->showBackground()
->landscape()
->margins(10, 20, 30, 40)
->pages('1-3')
->paperSize(210, 148)
->showBrowserHeaderAndFooter()
->hideHeader()
->createPdfCommand('screenshot.pdf');

$this->assertEquals([
'url' => 'https://example.com',
'action' => 'pdf',
'options' => [
'path' => 'screenshot.pdf',
'printBackground' => true,
'landscape' => true,
'margin' => ['top' => '10mm', 'right' => '20mm', 'bottom' => '30mm', 'left' => '40mm'],
'pageRanges' => '1-3',
'width' => '210mm',
'height' => '148mm',
'displayHeaderFooter' => true,
'headerTemplate' => '<p></p>',
'viewport' => [
'width' => 800,
'height' => 600,
],
'args' => [],
],
], $command);
}

/** @test */
public function it_can_create_a_command_to_generate_a_pdf_with_the_footer_hidden()
{
$command = Browsershot::url('https://example.com')
->showBackground()
->landscape()
->margins(10, 20, 30, 40)
->pages('1-3')
->paperSize(210, 148)
->showBrowserHeaderAndFooter()
->hideFooter()
->createPdfCommand('screenshot.pdf');

$this->assertEquals([
'url' => 'https://example.com',
'action' => 'pdf',
'options' => [
'path' => 'screenshot.pdf',
'printBackground' => true,
'landscape' => true,
'margin' => ['top' => '10mm', 'right' => '20mm', 'bottom' => '30mm', 'left' => '40mm'],
'pageRanges' => '1-3',
'width' => '210mm',
'height' => '148mm',
'displayHeaderFooter' => true,
'footerTemplate' => '<p></p>',
'viewport' => [
'width' => 800,
'height' => 600,
],
'args' => [],
],
], $command);
}

/** @test */
public function it_can_create_a_command_to_generate_a_pdf_with_paper_format()
{
Expand Down

0 comments on commit 341805e

Please sign in to comment.