pdfparser icon indicating copy to clipboard operation
pdfparser copied to clipboard

Get Specific text Y Coordinates

Open rdhhauzan opened this issue 2 years ago • 3 comments

As the title says, is it possible to get Y Coordinates for specific text in pdf?

Here is my code for now:

use Smalot\PdfParser\Parser;
use Illuminate\Http\Request;
use setasign\Fpdi\Fpdi;

public function mergePdfWithSignature ($id, Request $req) {
        $pdfFile = storage_path('app/public/agreement/' . $id . '.pdf');
        if (File::exists(public_path('merged/' . $id. '.pdf'))) {
            File::delete(public_path('merged/'. $id. '.pdf'));
        }

        $parser = new Parser();
        $pdf = $parser->parseFile($pdfFile);
        $pdfContent = $pdf->getPages();
        $pagesWithSignature = [];

        foreach ($pdfContent as $pageNum => $page) {
            $text = $page->getText();
            if (preg_match('/Signature :/i', $text)) {
                $pagesWithSignature[] = [
                    'page' => $pageNum,
                    'text' => $text
                ];
            }
        }

        if (empty($pagesWithSignature)) {
            return \sendResponse('Something went wrong', 404);
        }

        $signature = $req->signature;
        $fpdi = new Fpdi();

        foreach ($pagesWithSignature as $pageData) {
            $page = $pageData['page'];
            $text = $pageData['text'];
            $tempImagePath = storage_path('app/public/signature/'. $id. '.png');
            $fpdi->setSourceFile($pdfFile);
            $template = $fpdi->importPage($page + 1);

            $fpdi->addPage();
            $fpdi->useTemplate($template);
            preg_match('/Signature :/i', $text, $matches);
            $signPos = $matches[0];
            $posX = $fpdi->GetStringWidth($signPos);

            $x = 50; // Adjust the horizontal position as needed
            $y = 50; // Adjust the vertical position as needed

            $image = Image::make($signature);
            $image->save($tempImagePath);
            $imageWidth = $image->width();
            $imageHeight = $image->height();

            $textWidth = strlen('Signature :') * 4;
            $textHeight = 10;

            $fpdi->Image($tempImagePath, $x + $posX, $y, 20, 10, 'PNG');
        }

        $pageCount = $fpdi->setSourceFile($pdfFile);
        for ($pageNum = 1; $pageNum <= $pageCount; $pageNum++) {
            if (!in_array($pageNum, array_column($pagesWithSignature, 'page'))) {
                $template = $fpdi->importPage($pageNum);
                $fpdi->addPage();
                $fpdi->useTemplate($template);
            }
        }

        $mergedPdfPath = public_path('merged/' . $id. '.pdf');
        $fpdi->Output($mergedPdfPath, 'F');
        // \DB::table('activation_signature')->where('invoice_id', $id)->where('status', 0)->update(['status' => 1]);

        return response()->download($mergedPdfPath);
    }

I am Combining this library with Setasign/FPDI Thank you

rdhhauzan avatar Jul 24 '23 07:07 rdhhauzan

Please check https://github.com/smalot/pdfparser/blob/master/doc/Usage.md#extract-text-positions

k00ni avatar Jul 26 '23 14:07 k00ni

Please check https://github.com/smalot/pdfparser/blob/master/doc/Usage.md#extract-text-positions

I Already try using this before, but it return empty array

rdhhauzan avatar Jul 27 '23 06:07 rdhhauzan

Please check https://github.com/smalot/pdfparser/blob/master/doc/Usage.md#extract-text-positions

what does it mean when x and y are larger values than the size of the pdf? example x= 2030 but the width = 612

ranceforhiwd avatar Aug 01 '23 18:08 ranceforhiwd