Skip to content

Commit b426cf2

Browse files
committed
add support for pdfs
1 parent 69afd4e commit b426cf2

File tree

6 files changed

+80
-12
lines changed

6 files changed

+80
-12
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

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

5+
### 2.1.0 - 2017-08-06
6+
- add support for saving `pdf`s
7+
58
### 2.0.3 - 2017-07-05
69
- lower `symfony/process` requirement
710

README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Convert a webpage to an image using headless Chrome
1+
# Convert a webpage to an image or pdf using headless Chrome
22

33
[![Latest Version](https://img.shields.io/github/release/spatie/browsershot.svg?style=flat-square)](https://github.com/spatie/browsershot/releases)
44
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
@@ -7,7 +7,7 @@
77
[![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)
88
[![Total Downloads](https://img.shields.io/packagist/dt/spatie/browsershot.svg?style=flat-square)](https://packagist.org/packages/spatie/browsershot)
99

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

1212
Here's a quick example:
1313

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

20+
It will save a pdf if the path passed to the `save` method has a `pdf` extension.
21+
22+
```php
23+
// a pdf will be saved
24+
Browsershot::url('https://example.com')->save('example.pdf');
25+
```
26+
2027
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).
2128

2229
## Postcardware
@@ -113,6 +120,19 @@ Browsershot::url('https://example.com')
113120
->save($pathToImage);
114121
```
115122

123+
Browsershot will save a pdf if the path passed to the `save` method has a `pdf` extension.
124+
125+
```php
126+
// a pdf will be saved
127+
Browsershot::url('https://example.com')->save('example.pdf');
128+
```
129+
130+
Alternatively you can explicitly use the `savePdf` method:
131+
132+
```php
133+
Browsershot::url('https://example.com')->savePdf('example.pdf');
134+
```
135+
116136
## Contributing
117137

118138
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
{
22
"name": "spatie/browsershot",
3-
"description": "Convert a webpage to an image using headless Chrome",
3+
"description": "Convert a webpage to an image or pdf using headless Chrome",
44
"homepage": "https://github.com/spatie/browsershot",
55
"keywords":
66
[
77
"convert",
88
"webpage",
99
"image",
10+
"pdf",
1011
"screenshot",
1112
"browser",
12-
"phantomjs",
13-
"laravel"
13+
"chrome",
14+
"headless"
1415
],
1516
"license": "MIT",
1617
"authors": [

phpunit.xml.dist

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,4 @@
1919
<directory suffix=".php">src/</directory>
2020
</whitelist>
2121
</filter>
22-
<logging>
23-
<log type="tap" target="build/report.tap"/>
24-
<log type="junit" target="build/report.junit.xml"/>
25-
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
26-
<log type="coverage-text" target="build/coverage.txt"/>
27-
<log type="coverage-clover" target="build/logs/clover.xml"/>
28-
</logging>
2922
</phpunit>

src/Browsershot.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ public function __call($name, $arguments)
104104

105105
public function save(string $targetPath)
106106
{
107+
if (strtolower(pathinfo($targetPath, PATHINFO_EXTENSION)) === 'pdf') {
108+
return $this->savePdf($targetPath);
109+
}
110+
107111
$temporaryDirectory = (new TemporaryDirectory())->create();
108112

109113
try {
@@ -133,6 +137,15 @@ public function save(string $targetPath)
133137
}
134138
}
135139

140+
public function savePdf(string $targetPath)
141+
{
142+
$command = $this->createPdfCommand($targetPath);
143+
144+
$process = (new Process($command))->setTimeout($this->timeout);
145+
146+
$process->run();
147+
}
148+
136149
public function applyManipulations(string $imagePath)
137150
{
138151
Image::load($imagePath)
@@ -171,6 +184,30 @@ public function createScreenshotCommand(string $workingDirectory): string
171184
return $command;
172185
}
173186

187+
protected function createPdfCommand($targetPath): string
188+
{
189+
$command =
190+
escapeshellarg($this->findChrome())
191+
. " --headless --print-to-pdf={$targetPath}";
192+
193+
if ($this->disableGpu) {
194+
$command .= ' --disable-gpu';
195+
}
196+
197+
if ($this->hideScrollbars) {
198+
$command .= ' --hide-scrollbars';
199+
}
200+
201+
if (! empty($this->userAgent)) {
202+
$command .= ' --user-agent='.escapeshellarg($this->userAgent);
203+
}
204+
205+
$command .= ' ' .escapeshellarg($this->url);
206+
207+
return $command;
208+
209+
}
210+
174211
protected function findChrome(): string
175212
{
176213
if (! empty($this->pathToChrome)) {

tests/BrowsershotTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@ public function it_can_take_a_screenshot()
2323
$this->assertFileExists($targetPath);
2424
}
2525

26+
/** @test */
27+
public function it_can_save_a_pdf_by_using_the_pdf_extension()
28+
{
29+
$targetPath = __DIR__.'/temp/testPdf.pdf';
30+
31+
$this
32+
->getBrowsershotForCurrentEnvironment()
33+
->save($targetPath);
34+
35+
$this->assertFileExists($targetPath);
36+
37+
$this->assertEquals('application/pdf', mime_content_type($targetPath));
38+
}
39+
2640
/** @test */
2741
public function it_can_use_the_methods_of_the_image_package()
2842
{

0 commit comments

Comments
 (0)