Skip to content
This repository has been archived by the owner on Jan 10, 2022. It is now read-only.

Commit

Permalink
Tcpdf converter added
Browse files Browse the repository at this point in the history
  • Loading branch information
klimov-paul committed May 6, 2016
1 parent f415dcf commit ca7f4e7
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 1 deletion.
2 changes: 1 addition & 1 deletion converters/Dompdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Dompdf extends BaseConverter
protected function convertInternal($sourceFileName, $outputFileName, $options)
{
$pageSize = ArrayHelper::remove($options, 'pageSize', 'A4');
$orientation = ArrayHelper::remove($options, 'orientation', 'landscape');
$orientation = ArrayHelper::remove($options, 'orientation', 'portrait');

if (empty($options)) {
$dompdfOptions = null;
Expand Down
58 changes: 58 additions & 0 deletions converters/Tcpdf.php
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');
}
}
1 change: 1 addition & 0 deletions tests/converters/DompdfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class DompdfTest extends TestCase
{
protected function setUp()
{
parent::setUp();
if (!$this->isConverterAvailable()) {
$this->markTestSkipped('"Dompdf" library required');
}
Expand Down
1 change: 1 addition & 0 deletions tests/converters/MpdfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class MpdfTest extends TestCase
{
protected function setUp()
{
parent::setUp();
if (!$this->isConverterAvailable()) {
$this->markTestSkipped('"Mpdf" library required');
}
Expand Down
39 changes: 39 additions & 0 deletions tests/converters/TcpdfTest.php
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));
}
}
1 change: 1 addition & 0 deletions tests/converters/WkhtmltopdfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class WkhtmltopdfTest extends TestCase
{
protected function setUp()
{
parent::setUp();
if (!$this->isConverterAvailable()) {
$this->markTestSkipped('Shell command "wkhtmltopdf" is unavailable');
}
Expand Down

0 comments on commit ca7f4e7

Please sign in to comment.