php Laravel:在PDF中添加特定位置的图像

ecr0jaav  于 5个月前  发布在  PHP
关注(0)|答案(1)|浏览(79)

构建Laravel API,我有合同PDF文件存储在远程服务器和保存在数据库中的uid,也在线签名文件,这是png.现在当用户点击sign按钮,我想把这个png图像和地方,让右下角(这可能会改变)在合同PDF.我尝试了它与laravel domPDF和laravel-snappy包,但没有想要的结果.与domPDF的例子:

public function addSignatureToPdf(ContractFile $contractFile)
{
    // Path to the existing PDF file
    $pdfPath = storage_path($this::PDF_DIRECTORY_PATH . "combined_document{$contractFile->getHash()}.pdf");

    $existingPdfContent = file_get_contents($pdfPath);

    // Set the path to your signature image
    $signatureImagePath = storage_path('app/contract-files/დავით-ხაჩიძე.png');

    // Load the HTML content with the image (signature) added
    $html = view('pdf.signature', compact('signatureImagePath'))->render();
    $htmlWithEncoding = mb_convert_encoding($existingPdfContent . $html, 'HTML-ENTITIES', 'UTF-8');

    // Convert HTML to PDF
    $pdf = PDF::loadHTML($htmlWithEncoding);

    // Save the modified PDF
    $outputPdfPath = storage_path($this::PDF_DIRECTORY_PATH . 'modified_contract.pdf');
    file_put_contents($outputPdfPath, $pdf->output());

    return "PDF with signature added saved at: $outputPdfPath";
}

字符串
这创建了pdf文件,但它与二进制数据,我不知道在哪里,以及如何我追加图像到pdf。任何建议如何做laravel?

mkshixfv

mkshixfv1#

将图像转换为base64

function convertImageToBase64($path) : string{
   try {
       return base64_encode(file_get_contents(public_path($path)));
   } catch (\Throwable $th) {
       return '';
   }
}

字符串

相关问题