Skip to content

Commit d2d9e78

Browse files
authored
Merge pull request #834 from Deepesh8122/add-button-tag
feat: Implement HTML Button Tag Support for PDF Generation
2 parents ca18e31 + 513275e commit d2d9e78

5 files changed

Lines changed: 156 additions & 0 deletions

File tree

Tests/Tag/Html/ButtonTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
namespace Tests\Tag\Html;
3+
4+
use PHPUnit\Framework\TestCase;
5+
use Spipu\Html2Pdf\Html2Pdf;
6+
use Spipu\Html2Pdf\Tag\Html\Button;
7+
8+
class ButtonTest extends TestCase
9+
{
10+
private $html2pdf;
11+
private $button;
12+
13+
protected function setUp(): void
14+
{
15+
$this->html2pdf = new Html2Pdf();
16+
$this->button = new Button();
17+
}
18+
19+
public function testButtonName()
20+
{
21+
$this->assertEquals('button', $this->button->getName());
22+
}
23+
24+
public function testButtonProperties()
25+
{
26+
$properties = [
27+
'type' => 'submit',
28+
'class' => 'btn-primary',
29+
'background' => '#007bff',
30+
'color' => '#ffffff'
31+
];
32+
33+
$result = $this->button->open($properties);
34+
$this->assertTrue($result);
35+
}
36+
}

Tests/samples/button_test.html

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Button Test</title>
5+
</head>
6+
<body>
7+
<!-- Default button -->
8+
<button>Default Button</button>
9+
10+
<!-- Styled button -->
11+
<button style="background: #007bff; color: white; padding: 10px 20px;">
12+
Styled Button
13+
</button>
14+
15+
<!-- Button with class -->
16+
<button class="btn-primary" type="submit">
17+
Submit Button
18+
</button>
19+
20+
<!-- Button with custom attributes -->
21+
<button id="custom-btn" style="width: 200px; margin: 10px;">
22+
Wide Button
23+
</button>
24+
</body>
25+
</html>

Tests/samples/test_buttons.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
require_once '../../vendor/autoload.php';
3+
4+
use Spipu\Html2Pdf\Html2Pdf;
5+
6+
try {
7+
$html2pdf = new Html2Pdf();
8+
9+
// Load the test HTML
10+
$content = file_get_contents('button_test.html');
11+
12+
// Convert to PDF
13+
$html2pdf->writeHTML($content);
14+
$html2pdf->output('button_test.pdf', 'D');
15+
} catch (Exception $e) {
16+
echo $e->getMessage();
17+
}

src/Extension/Core/HtmlExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ protected function initTags()
5454
new Html\Sup(),
5555
new Html\U(),
5656
new Html\Strike(),
57+
new Html\Button(),
5758
);
5859
}
5960
}

src/Tag/Html/Button.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
namespace Spipu\Html2Pdf\Tag\Html;
3+
4+
use Spipu\Html2Pdf\Tag\AbstractTag;
5+
6+
/**
7+
* Button Tag class
8+
*/
9+
class Button extends AbstractTag
10+
{
11+
/**
12+
* @inheritdoc
13+
*/
14+
public function getName()
15+
{
16+
return 'button';
17+
}
18+
19+
/**
20+
* @inheritdoc
21+
*/
22+
public function open($properties)
23+
{
24+
$styles = $this->getStyleFromProperties($properties);
25+
26+
// Set default button styling if not specified
27+
if (!isset($styles['background'])) {
28+
$styles['background'] = '#f0f0f0';
29+
}
30+
if (!isset($styles['border'])) {
31+
$styles['border'] = '1px solid #ccc';
32+
}
33+
if (!isset($styles['padding'])) {
34+
$styles['padding'] = '5px 10px';
35+
}
36+
37+
// Add custom class if provided
38+
if (isset($properties['class'])) {
39+
$this->parsingCss->analyse('button.' . $properties['class']);
40+
}
41+
42+
// Handle button type
43+
$type = isset($properties['type']) ? $properties['type'] : 'button';
44+
45+
// Create button container with proper styling
46+
$this->pdf->setStyle($styles);
47+
return true;
48+
}
49+
50+
/**
51+
* @inheritdoc
52+
*/
53+
public function close($properties)
54+
{
55+
// Reset styles after button closure
56+
$this->pdf->resetStyle();
57+
return true;
58+
}
59+
60+
/**
61+
* @inheritdoc
62+
*/
63+
protected function getStyleFromProperties($properties)
64+
{
65+
$styles = [];
66+
67+
// Handle standard CSS properties
68+
$cssProperties = ['width', 'height', 'background', 'color', 'border', 'padding', 'margin', 'font-size'];
69+
foreach ($cssProperties as $prop) {
70+
if (isset($properties[$prop])) {
71+
$styles[$prop] = $properties[$prop];
72+
}
73+
}
74+
75+
return $styles;
76+
}
77+
}

0 commit comments

Comments
 (0)