How To Write Text On An Existing PDF File In Laravel

How To Write Text On An Existing PDF File In Laravel

Here are the steps on how to write text on an existing PDF file in Laravel:

  1. Install Laravel and the setasign/fpdf and setasign/fpdi packages.
  2. Create a new controller and add a method to write text to the PDF file.
  3. Create a route to the controller method.
  4. Test the code by visiting the route in your browser.

Here is an example of how to write text on an existing PDF file in Laravel:

PHP

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use setasign\fpdf\FPDF;
use setasign\fpdi\FPDI;

class PDFController extends Controller
{
    public function fillPDFFile(Request $request)
    {
        $pdf = new FPDI();
        $pdf->setSourceFile('existing.pdf');
        $pdf->AddPage();

        $pdf->SetFont('Helvetica', 'B', 16);
        $pdf->SetTextColor(255, 0, 0);
        $pdf->SetXY(10, 10);
        $pdf->Write(0, 'This is the new text');

        $pdf->Output('new.pdf');
    }
}

In this example, we are first creating a new FPDI object. This object allows us to read and write PDF files. We then set the source file to the existing PDF file that we want to edit. We then add a new page to the PDF file.

Next, we set the font, text color, and position of the text that we want to write to the PDF file. Finally, we write the text to the PDF file and output the file to the browser.

For more posts click here

Leave a Reply