Skip to content

Commit bd8e296

Browse files
author
Matthias Isler
committed
2.0 beta
1 parent 356fd3e commit bd8e296

File tree

9 files changed

+173
-2
lines changed

9 files changed

+173
-2
lines changed

README.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,46 @@ php composer.phar require spiritix/html-to-pdf
3535

3636
## Usage
3737

38-
TODO
38+
The usage of this library is very simple.
39+
You just need a converter instance, pass an input and an output handler to it and set some options if you like.
40+
After running the conversion, the converter will provide you with the output handler instance.
41+
Now you may use it's specific functionality to get your PDF file.
42+
43+
```php
44+
use Spiritix\HtmlToPdf\Converter;
45+
use Spiritix\HtmlToPdf\Input\UrlInput;
46+
use Spiritix\HtmlToPdf\Output\DownloadOutput;
47+
48+
$input = new UrlInput();
49+
$input->setUrl('https://www.google.com');
50+
51+
$converter = new Converter($input, new DownloadOutput());
52+
53+
$converter->setOption('d', '300');
54+
$converter->setOptions([
55+
'margin-bottom' => '100',
56+
'margin-top' => '100',
57+
]);
58+
59+
$output = $converter->convert();
60+
$output->download();
61+
```
62+
63+
### Input handlers
64+
65+
The following input handlers are available:
66+
67+
- StringInput - Provide the PDF contents as a string
68+
- UrlInput - Fetch the PDF contents from an URL
69+
70+
### Output handlers
71+
72+
The following output handlers are available:
73+
74+
- StringOutput - Get the PDF contents as a string
75+
- FileOutput - Store the PDF to your file system
76+
- DownloadOutput - Force the browser to download the PDF file
77+
- EmbedOutput - Force the browser to embed the PDF file
3978

4079
## Options
4180

tests/Input/StringInputTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Spiritix\HtmlToPdf\Tests\Input;
4+
5+
use Spiritix\HtmlToPdf\Input\StringInput;
6+
use Spiritix\HtmlToPdf\Tests\TestCase;
7+
8+
class StringInputTest extends TestCase
9+
{
10+
public function testSetHtml()
11+
{
12+
$html = '<h1>Hello</h1>';
13+
14+
$input = new StringInput();
15+
$input->setHtml($html);
16+
17+
$this->assertEquals($html, $input->getHtml());
18+
}
19+
}

tests/Input/UrlInputTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Spiritix\HtmlToPdf\Tests\Input;
4+
5+
use Spiritix\HtmlToPdf\Input\UrlInput;
6+
use Spiritix\HtmlToPdf\Tests\TestCase;
7+
8+
class UrlInputTest extends TestCase
9+
{
10+
public function testSetUrl()
11+
{
12+
$input = new UrlInput();
13+
$input->setUrl('https://www.google.com');
14+
15+
$this->assertContains('Google', $input->getHtml());
16+
}
17+
}

tests/Output/DownloadOutputTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Spiritix\HtmlToPdf\Tests\Output;
4+
5+
use Spiritix\HtmlToPdf\Output\DownloadOutput;
6+
use Spiritix\HtmlToPdf\Tests\TestCase;
7+
8+
class DownloadOutputTest extends TestCase
9+
{
10+
/**
11+
* @runInSeparateProcess
12+
*/
13+
public function testDownload()
14+
{
15+
$pdfData = $this->getPdfSampleData();
16+
17+
$output = new DownloadOutput();
18+
$output->setPdfData($pdfData);
19+
20+
ob_start();
21+
$output->download('sample.pdf', false);
22+
$data = ob_get_clean();
23+
24+
$this->assertEquals($pdfData, $data);
25+
}
26+
}

tests/Output/EmbedOutputTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Spiritix\HtmlToPdf\Tests\Output;
4+
5+
use Spiritix\HtmlToPdf\Output\EmbedOutput;
6+
use Spiritix\HtmlToPdf\Tests\TestCase;
7+
8+
class EmbedOutputTest extends TestCase
9+
{
10+
/**
11+
* @runInSeparateProcess
12+
*/
13+
public function testEmbed()
14+
{
15+
$pdfData = $this->getPdfSampleData();
16+
17+
$output = new EmbedOutput();
18+
$output->setPdfData($pdfData);
19+
20+
ob_start();
21+
$output->embed('sample.pdf', false);
22+
$data = ob_get_clean();
23+
24+
$this->assertEquals($pdfData, $data);
25+
}
26+
}

tests/Output/FileOutputTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Spiritix\HtmlToPdf\Tests\Output;
4+
5+
use Spiritix\HtmlToPdf\Output\FileOutput;
6+
use Spiritix\HtmlToPdf\Tests\TestCase;
7+
8+
class FileOutputTest extends TestCase
9+
{
10+
public function testStore()
11+
{
12+
$pdfData = $this->getPdfSampleData();
13+
14+
$output = new FileOutput();
15+
$output->setPdfData($pdfData);
16+
17+
$url = '/tmp/sample.pdf';
18+
$output->store($url);
19+
20+
$this->assertEquals(file_get_contents($url), $pdfData);
21+
}
22+
}

tests/Output/StringOutputTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Spiritix\HtmlToPdf\Tests\Output;
4+
5+
use Spiritix\HtmlToPdf\Output\StringOutput;
6+
use Spiritix\HtmlToPdf\Tests\TestCase;
7+
8+
class StringOutputTest extends TestCase
9+
{
10+
public function testGet()
11+
{
12+
$pdfData = $this->getPdfSampleData();
13+
14+
$output = new StringOutput();
15+
$output->setPdfData($pdfData);
16+
17+
$this->assertEquals($pdfData, $output->get());
18+
}
19+
}

tests/TestCase.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@
66

77
class TestCase extends PHPUnit_Framework_TestCase
88
{
9-
//
9+
public function getPdfSampleData()
10+
{
11+
return file_get_contents(__DIR__ . '/pdf/Sample.pdf');
12+
}
1013
}

tests/pdf/Sample.pdf

11.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)