minor fixes

This commit is contained in:
ludo
2024-02-23 08:35:41 +01:00
parent fb6da523fa
commit cc411cba68
47 changed files with 148 additions and 2458 deletions

View File

@@ -2,41 +2,66 @@
namespace App\Repositories\Core;
use Barryvdh\Debugbar\Facades\Debugbar;
use Barryvdh\DomPDF\Facade\Pdf as DomPDF;
use Barryvdh\Snappy\Facades\SnappyPdf;
use GravityMedia\Ghostscript\Ghostscript;
use Imagick;
use Mpdf\Mpdf;
use Spatie\PdfToText\Pdf as PDFToText;
use Symfony\Component\Process\Process;
class PDF
{
public function convertView($view, $data, $filename)
{
try {
Browsershot::html(view($view, $data)->render())
->noSandbox()
->waitUntilNetworkIdle()
->format('A4')
->showBackground()
->savePdf('temp.pdf');
$postRoute = URL::signedRoute('orderinvoices.store', ['order' => $this->order]);
Http::attach('invoice', file_get_contents('temp.pdf'), $filename)
->post($postRoute)
->throw();
} catch (\Exception $exception) {
Log::error($exception);
}
}
public static function view($view, $data, $filename = 'file.pdf')
{
\Debugbar::disable();
Debugbar::disable();
$pdf = DomPDF::loadView($view, $data);
return $pdf->download($filename);
}
public static function countPages($filename)
{
$image = new Imagick();
$image->pingImage($filename);
return $image->getNumberImages();
/*
$pdftext = file_get_contents($filename);
return preg_match_all("/\/Page\W*(\d+)/", $pdftext, $dummy);
*/
}
public function getPDFPages($document)
{
$cmd = '/path/to/pdfinfo'; // Linux
$cmd = 'C:\\path\\to\\pdfinfo.exe'; // Windows
// Parse entire output
// Surround with double quotes if file name has spaces
exec("$cmd \"$document\"", $output);
// Iterate through lines
$pagecount = 0;
foreach ($output as $op) {
// Extract the number
if (preg_match("/Pages:\s*(\d+)/i", $op, $matches) === 1) {
$pagecount = intval($matches[1]);
break;
}
}
return $pagecount;
}
public static function getText($filename)
{
return PDFToText::getText($filename);
}
public static function convertHTML($html)
{
$mpdf = new Mpdf();
@@ -45,11 +70,11 @@ class PDF
return $mpdf->Output();
}
public static function convertURL($url)
public static function convertURL($url, $filename = 'sample.pdf')
{
$pdf = SnappyPdf::loadFile($url);
return $pdf->download('invoice.pdf');
return $pdf->download($filename);
}
public static function convertIfNeeded($filename)
@@ -62,13 +87,14 @@ class PDF
public static function getVersion($filename)
{
$pdf = fopen($filename, 'r');
if ($pdf) {
$line_first = fgets($pdf);
fclose($pdf);
} else {
if (! $pdf) {
echo 'error opening the file.';
return false;
}
preg_match_all('!\d+!', $line_first, $matches);
$lineFirst = fgets($pdf);
fclose($pdf);
preg_match_all('!\d+!', $lineFirst, $matches);
$version = implode('.', $matches[0]);
return (float) $version;
@@ -76,35 +102,31 @@ class PDF
public static function downgrade($filename)
{
$new_filename = $filename.'-temp';
$newFilename = $filename.'-temp';
$ghostscript = new Ghostscript([
'quiet' => false,
]);
$device = $ghostscript->createPdfDevice($new_filename);
$device = $ghostscript->createPdfDevice($newFilename);
$device->setCompatibilityLevel(1.4);
// dump($device);
$process = $device->createProcess($filename);
// dump($process);
// echo '$ ' . $process->getCommandLine() . PHP_EOL;
// exit;
$process->run(function ($type, $buffer) {
if ($type === Process::ERR) {
throw new \RuntimeException($buffer);
}
// print $buffer;
});
unlink($filename);
rename($new_filename, $filename);
rename($newFilename, $filename);
}
public static function downgrade2($filename)
{
$new_filename = $filename.'-temp';
shell_exec('gs -dBATCH -dCompatibilityLevel=1.4 -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile="'.$new_filename.'" "'.$filename.'"');
$newFilename = $filename.'-temp';
$options = '-dBATCH -dCompatibilityLevel=1.4 -dNOPAUSE -q -sDEVICE=pdfwrite';
shell_exec("gs {$options} -sOutputFile=\"{$newFilename}\" \"{$filename}\"");
unlink($filename);
rename($new_filename, $filename);
rename($newFilename, $filename);
}
}