PHP. Вставка PNG изображения в PDF документ
В pdf документ необходимо вставить в последнюю страницу png изображение но почему то у вставленных png изображениях в некоторых документах черный цвет меняется на белый
код создания изображения:
public function make_signature() {
$fio = 'ФИО:'.auth()->user()->fullname;
$font = realpath('./fonts/Open_Sans_regular.ttf');
$font_size = 40;
$image_width = 1400;
$offset = 20;
$str = '';
$arr = explode(' ', $fio);
foreach($arr as $key => $word) {
$temp_str = $str.' '.$word;
if(imagettfbbox($font_size,0, $font, $temp_str)[2]>$image_width-$offset*2) {
$str.=($str==""?"":"&").$word;
} else {
$str.=($str==""?"":" ").$word;
}
}
$fio = explode('&', $str);
unset($temp_str, $str, $arr);
// $image_height = $font_size * count($fio) + 2 * $font_size + 20 + $font_size / 5 * count($fio);
$image_height = $font_size * count($fio) + 2 * $font_size + $offset * 4 + $offset * count($fio);
$signature = imagecreatetruecolor($image_width, $image_height);
imagesavealpha($signature, true);
// изначальный цвет 66, 135, 245
$color = imagecolorallocate($signature, 0, 0, 0);
$png_font = imagecolorallocatealpha($signature, 0, 0, 0, 127);
imagefill($signature, 0, 0, $png_font);
imagesetthickness($signature, 4);
imageline($signature, 0, 2, $image_width, 2, $color);
imageline($signature, 0, $image_height-2, $image_width, $image_height-2, $color);
imageline($signature, 2, 0, 2, $image_height, $color);
imageline($signature, $image_width-2, 0, $image_width-2, $image_height, $color);
imagettftext($signature, $font_size, 0, $offset, $font_size + $offset, $color, $font, 'Документ подписан простой электронной подписью');
$new_str = 2 * $font_size + 2 * $offset;
foreach($fio as $item) {
imagettftext($signature, $font_size, 0, $offset, $new_str, $color, $font, $item);
$new_str += $font_size + $offset;
}
imagettftext($signature, $font_size, 0, $offset, $new_str, $color, $font, 'Дата подписания: '.Carbon::now('Europe/Moscow')->format('d/m/Y H:i:s'));
$file_link = 'files/temp/sign'.rand(100000, 999999).'.png';
imagepng($signature, $file_link);
$result = Storage::putFile('sign', new File($file_link));
unlink($file_link);
return Storage::path($result);
}
код вставки изображения в pdf
public function testFile(Request $request) {
$sign = new Imagick(realpath('files/temp/sign.png'));
$margin = 5;
foreach ($request->file('uploadFiles') as $file) {
$pdf = new Imagick();
$pdf->pingImage($file->path());
$last = $pdf->getNumberImages() -1;
$pdf->clear();
$pdf->readImage($file->path()."[$last]");
$pageWidth = $pdf->getImageWidth();
$pageHeight = $pdf->getImageHeight();
$watermarkWidth = $sign->getImageWidth();
$watermarkHeight = $sign->getImageHeight();
$posX = $pageWidth - $watermarkWidth - $margin;
$posY = $pageHeight - $watermarkHeight - $margin;
$pdf->compositeImage($sign, Imagick::COMPOSITE_OVER, $posX, $posY);
$pdf->writeImages('C:/OSPanel/domains/dvinaland-finreport/public/files/temp/testPdf.pdf', false);
unset($pdf);
}
}
Источник: Stack Overflow на русском