Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Tests/Tag/Html/ButtonTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
namespace Tests\Tag\Html;

use PHPUnit\Framework\TestCase;
use Spipu\Html2Pdf\Html2Pdf;
use Spipu\Html2Pdf\Tag\Html\Button;

class ButtonTest extends TestCase
{
private $html2pdf;
private $button;

protected function setUp(): void
{
$this->html2pdf = new Html2Pdf();
$this->button = new Button();
}

public function testButtonName()
{
$this->assertEquals('button', $this->button->getName());
}

public function testButtonProperties()
{
$properties = [
'type' => 'submit',
'class' => 'btn-primary',
'background' => '#007bff',
'color' => '#ffffff'
];

$result = $this->button->open($properties);
$this->assertTrue($result);
}
}
25 changes: 25 additions & 0 deletions Tests/samples/button_test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html>
<head>
<title>Button Test</title>
</head>
<body>
<!-- Default button -->
<button>Default Button</button>

<!-- Styled button -->
<button style="background: #007bff; color: white; padding: 10px 20px;">
Styled Button
</button>

<!-- Button with class -->
<button class="btn-primary" type="submit">
Submit Button
</button>

<!-- Button with custom attributes -->
<button id="custom-btn" style="width: 200px; margin: 10px;">
Wide Button
</button>
</body>
</html>
17 changes: 17 additions & 0 deletions Tests/samples/test_buttons.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
require_once '../../vendor/autoload.php';

use Spipu\Html2Pdf\Html2Pdf;

try {
$html2pdf = new Html2Pdf();

// Load the test HTML
$content = file_get_contents('button_test.html');

// Convert to PDF
$html2pdf->writeHTML($content);
$html2pdf->output('button_test.pdf', 'D');
} catch (Exception $e) {
echo $e->getMessage();
}
1 change: 1 addition & 0 deletions src/Extension/Core/HtmlExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ protected function initTags()
new Html\Sup(),
new Html\U(),
new Html\Strike(),
new Html\Button(),
);
}
}
77 changes: 77 additions & 0 deletions src/Tag/Html/Button.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
namespace Spipu\Html2Pdf\Tag\Html;

use Spipu\Html2Pdf\Tag\AbstractTag;

/**
* Button Tag class
*/
class Button extends AbstractTag
{
/**
* @inheritdoc
*/
public function getName()
{
return 'button';
}

/**
* @inheritdoc
*/
public function open($properties)
{
$styles = $this->getStyleFromProperties($properties);

// Set default button styling if not specified
if (!isset($styles['background'])) {
$styles['background'] = '#f0f0f0';
}
if (!isset($styles['border'])) {
$styles['border'] = '1px solid #ccc';
}
if (!isset($styles['padding'])) {
$styles['padding'] = '5px 10px';
}

// Add custom class if provided
if (isset($properties['class'])) {
$this->parsingCss->analyse('button.' . $properties['class']);
}

// Handle button type
$type = isset($properties['type']) ? $properties['type'] : 'button';

// Create button container with proper styling
$this->pdf->setStyle($styles);
return true;
}

/**
* @inheritdoc
*/
public function close($properties)
{
// Reset styles after button closure
$this->pdf->resetStyle();
return true;
}

/**
* @inheritdoc
*/
protected function getStyleFromProperties($properties)
{
$styles = [];

// Handle standard CSS properties
$cssProperties = ['width', 'height', 'background', 'color', 'border', 'padding', 'margin', 'font-size'];
foreach ($cssProperties as $prop) {
if (isset($properties[$prop])) {
$styles[$prop] = $properties[$prop];
}
}

return $styles;
}
}
Loading