Created
March 20, 2023 14:06
-
-
Save Adelysnet/8c52f20f06b82192aec4ecfb8e6cfd7f to your computer and use it in GitHub Desktop.
Reproduce wrong header len estimation / is_file crash
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
<?php | |
namespace App\Service; | |
use Knp\Snappy\Pdf; | |
use Throwable; | |
class PdfManager | |
{ | |
protected $pdf; | |
public function __construct(Pdf $pdf) | |
{ | |
$this->pdf = $pdf; | |
} | |
public function generatePDF($html) | |
{ | |
try{ | |
$header = $this->getHeader(); | |
$footer = $this->getFooter(); | |
$pdf_content = $this->pdf->getOutputFromHtml($html, [ | |
'orientation' => 'portrait', | |
'enable-javascript' => true, | |
'javascript-delay' => 200, | |
'no-background' => false, | |
'lowquality' => true, | |
'encoding' => 'utf-8', | |
'images' => true, | |
'header-spacing' => 4, | |
'margin-bottom' => 40, | |
'margin-top' => 60, | |
'dpi' => 150, | |
'image-dpi' => 150, | |
'enable-external-links' => true, | |
'enable-internal-links' => true, | |
'header-html' => $header, | |
'footer-html' => $footer, | |
]); | |
return $pdf_content; | |
}catch(Throwable $th){ | |
throw $th; | |
} | |
} | |
public function getHeader(){ | |
$header = ' | |
<!doctype html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
</head> | |
<body> | |
<table> | |
<thead> | |
<tr> | |
<th>Ref</th> | |
<th>Date</th> | |
<th>Shipping Date</th> | |
<th>Customer</th> | |
<th>Coupon</th> | |
</tr> | |
</thead> | |
<tbody> | |
<tr> | |
<td>001</td> | |
<td>01/01/2023</td> | |
<td>01/02/2023</td> | |
<td>#C123</td> | |
<td>10%</td> | |
</tr> | |
<tr> | |
<td colspan="5" style="word-break:break-all;font-size:8px"> | |
### | |
</td> | |
</tr> | |
</tbody> | |
</table> | |
</body> | |
</html> | |
'; | |
$fill = ''; | |
/* Generate random header slightly lower then the php_maxpathlen limit */ | |
for($i = 0; $i < (PHP_MAXPATHLEN - strlen($header)-10); $i++){ | |
$fill .= '1'; | |
} | |
$header = str_replace("###", "###".$fill, $header); | |
// is_file function will crash from here although if lower then PHP_MAXPATHLEN | |
//dd(strlen($header)); | |
return $header; | |
} | |
public function getFooter(){ | |
return '<!doctype html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
</head> | |
<body> | |
<table class="footer"> | |
<tr> | |
<td> | |
Multiline footer content<br> | |
2023 | |
</td> | |
</td> | |
</table> | |
</body> | |
</html>'; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment