This repository has been archived by the owner on Jan 10, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f415dcf
commit ca7f4e7
Showing
6 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
/** | ||
* @link https://github.com/yii2tech | ||
* @copyright Copyright (c) 2015 Yii2tech | ||
* @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php) | ||
*/ | ||
|
||
namespace yii2tech\html2pdf\converters; | ||
|
||
use Yii; | ||
use yii\helpers\ArrayHelper; | ||
use yii2tech\html2pdf\BaseConverter; | ||
|
||
/** | ||
* Tcpdf converts file using [TCPDF](http://www.tcpdf.org) library. | ||
* | ||
* This converter requires `TCPDF` library to be installed. This can be done via composer: | ||
* | ||
* ``` | ||
* composer require --prefer-dist tecnickcom/tcpdf | ||
* ``` | ||
* | ||
* @author Paul Klimov <pklimov@quartsoft.com> | ||
* @package yii2tech\html2pdf\converters | ||
*/ | ||
class Tcpdf extends BaseConverter | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function convertInternal($sourceFileName, $outputFileName, $options) | ||
{ | ||
$charset = ArrayHelper::remove($options, 'charset', Yii::$app->charset); | ||
$pageSize = ArrayHelper::remove($options, 'pageSize', 'A4'); | ||
$orientation = ucfirst(ArrayHelper::remove($options, 'orientation', 'P')); | ||
$unit = ArrayHelper::remove($options, 'unit', 'mm'); | ||
|
||
$pdf = new \TCPDF($orientation, $unit, $pageSize, true, $charset, false); | ||
|
||
// set auto page breaks | ||
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM); | ||
|
||
foreach ($options as $name => $value) { | ||
$setter = 'Set' . $name; | ||
if (method_exists($pdf, $setter)) { | ||
$pdf->$setter($value); | ||
} else { | ||
$pdf->$name = $value; | ||
} | ||
} | ||
|
||
// add a page | ||
$pdf->AddPage(); | ||
|
||
$pdf->WriteHTML(file_get_contents($sourceFileName)); | ||
$pdf->Output($outputFileName, 'F'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace yii2tech\tests\unit\html2pdf\converters; | ||
|
||
use yii2tech\html2pdf\converters\Tcpdf; | ||
use yii2tech\tests\unit\html2pdf\TestCase; | ||
|
||
class TcpdfTest extends TestCase | ||
{ | ||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
if (!$this->isConverterAvailable()) { | ||
$this->markTestSkipped('"TCPDF" library required'); | ||
} | ||
} | ||
|
||
/** | ||
* @return boolean whether converter to be tested is available. | ||
*/ | ||
protected function isConverterAvailable() | ||
{ | ||
return class_exists('TCPDF', true); | ||
} | ||
|
||
// Tests : | ||
|
||
public function testConvert() | ||
{ | ||
$converter = new Tcpdf(); | ||
|
||
$sourceFileName = dirname(__DIR__) . '/data/html/simple.html'; | ||
$outputFileName = $this->ensureTestFilePath() . '/output.pdf'; | ||
|
||
$converter->convert($sourceFileName, $outputFileName); | ||
|
||
$this->assertTrue(file_exists($outputFileName)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters