Get Specific text Y Coordinates
Open
Nobody has claimed this yet.
question
- Dominant language
- PHP
- Stars
- 2.7k
- Forks
- 579
- Avg merge
- 2m
- Merged PRs (30d)
- 1
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing Parser::parseFile(), Page::getPages(), and Page::getText() in the example, then determine whether the library exposes positional information for matched text. Compare the requested coordinates with the Setasign/FPDI integration and define the expected page, text, and coordinate output before considering implementation or tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- backend
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100