Skip to content

Commit

Permalink
add support for pdfs
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Aug 6, 2017
1 parent 69afd4e commit b426cf2
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 12 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,9 @@

All notable changes to `browsershot` will be documented in this file

### 2.1.0 - 2017-08-06
- add support for saving `pdf`s

### 2.0.3 - 2017-07-05
- lower `symfony/process` requirement

Expand Down
24 changes: 22 additions & 2 deletions README.md
@@ -1,4 +1,4 @@
# Convert a webpage to an image using headless Chrome
# Convert a webpage to an image or pdf using headless Chrome

[![Latest Version](https://img.shields.io/github/release/spatie/browsershot.svg?style=flat-square)](https://github.com/spatie/browsershot/releases)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
Expand All @@ -7,7 +7,7 @@
[![SensioLabsInsight](https://img.shields.io/sensiolabs/i/9c1184fb-1edb-41d5-9d30-2620d99447c7.svg?style=flat-square)](https://insight.sensiolabs.com/projects/9c1184fb-1edb-41d5-9d30-2620d99447c7)
[![Total Downloads](https://img.shields.io/packagist/dt/spatie/browsershot.svg?style=flat-square)](https://packagist.org/packages/spatie/browsershot)

The package can convert a webpage to an image. The conversion is done behind the screens by Google Chrome.
The package can convert a webpage to an image or pdf. The conversion is done behind the screens by Google Chrome.

Here's a quick example:

Expand All @@ -17,6 +17,13 @@ use Spatie\Browsershot\Browsershot;
Browsershot::url('https://example.com')->save($pathToImage);
```

It will save a pdf if the path passed to the `save` method has a `pdf` extension.

```php
// a pdf will be saved
Browsershot::url('https://example.com')->save('example.pdf');
```

Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource).

## Postcardware
Expand Down Expand Up @@ -113,6 +120,19 @@ Browsershot::url('https://example.com')
->save($pathToImage);
```

Browsershot will save a pdf if the path passed to the `save` method has a `pdf` extension.

```php
// a pdf will be saved
Browsershot::url('https://example.com')->save('example.pdf');
```

Alternatively you can explicitly use the `savePdf` method:

```php
Browsershot::url('https://example.com')->savePdf('example.pdf');
```

## Contributing

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
Expand Down
7 changes: 4 additions & 3 deletions composer.json
@@ -1,16 +1,17 @@
{
"name": "spatie/browsershot",
"description": "Convert a webpage to an image using headless Chrome",
"description": "Convert a webpage to an image or pdf using headless Chrome",
"homepage": "https://github.com/spatie/browsershot",
"keywords":
[
"convert",
"webpage",
"image",
"pdf",
"screenshot",
"browser",
"phantomjs",
"laravel"
"chrome",
"headless"
],
"license": "MIT",
"authors": [
Expand Down
7 changes: 0 additions & 7 deletions phpunit.xml.dist
Expand Up @@ -19,11 +19,4 @@
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<logging>
<log type="tap" target="build/report.tap"/>
<log type="junit" target="build/report.junit.xml"/>
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
</phpunit>
37 changes: 37 additions & 0 deletions src/Browsershot.php
Expand Up @@ -104,6 +104,10 @@ public function __call($name, $arguments)

public function save(string $targetPath)
{
if (strtolower(pathinfo($targetPath, PATHINFO_EXTENSION)) === 'pdf') {
return $this->savePdf($targetPath);
}

$temporaryDirectory = (new TemporaryDirectory())->create();

try {
Expand Down Expand Up @@ -133,6 +137,15 @@ public function save(string $targetPath)
}
}

public function savePdf(string $targetPath)
{
$command = $this->createPdfCommand($targetPath);

$process = (new Process($command))->setTimeout($this->timeout);

$process->run();
}

public function applyManipulations(string $imagePath)
{
Image::load($imagePath)
Expand Down Expand Up @@ -171,6 +184,30 @@ public function createScreenshotCommand(string $workingDirectory): string
return $command;
}

protected function createPdfCommand($targetPath): string
{
$command =
escapeshellarg($this->findChrome())
. " --headless --print-to-pdf={$targetPath}";

if ($this->disableGpu) {
$command .= ' --disable-gpu';
}

if ($this->hideScrollbars) {
$command .= ' --hide-scrollbars';
}

if (! empty($this->userAgent)) {
$command .= ' --user-agent='.escapeshellarg($this->userAgent);
}

$command .= ' ' .escapeshellarg($this->url);

return $command;

}

protected function findChrome(): string
{
if (! empty($this->pathToChrome)) {
Expand Down
14 changes: 14 additions & 0 deletions tests/BrowsershotTest.php
Expand Up @@ -23,6 +23,20 @@ public function it_can_take_a_screenshot()
$this->assertFileExists($targetPath);
}

/** @test */
public function it_can_save_a_pdf_by_using_the_pdf_extension()
{
$targetPath = __DIR__.'/temp/testPdf.pdf';

$this
->getBrowsershotForCurrentEnvironment()
->save($targetPath);

$this->assertFileExists($targetPath);

$this->assertEquals('application/pdf', mime_content_type($targetPath));
}

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

0 comments on commit b426cf2

Please sign in to comment.