Web Niraj
  • Facebook
  • Flickr
  • Github
  • Linkedin
  • Twitter
  • YouTube
Online portfolio, code examples and developer blog
  • About
  • Contact
  • Portfolio
  • WordPress
Search the site...
  • Home
  • Blog
  • Creating / Editing a PDF Using PHP

Creating / Editing a PDF Using PHP

36

In a recent project, I was asked to create a dynamically generated PDF with the user’s information. In my research, I found that using FPDF was the best the best way to achieve the client’s requirements. Here is a quick tutorial on how you can use a existing PDF with dynamic content using only PHP. This particular example uses composer and less than 50 lines of code.

Installation

Start by installing the following composer packages:

composer require setasign/fpdf setasign/fpdi-fpdf

These two libraries are all you need to edit PDFs.

The Code

Before creating the code, you first need to to find a existing PDF to use. In this tutorial, I will be using the following template. Upload the file you want to use within your project folder so you can reference it in your code.

As for the code, this is what I used:

See the comments within the code to see what’s going on – most of it’s pretty self-explanatory, thanks to the straight-forward function names.

As the PDF I’m using has space for five bits of text, I’ve created 5 cells within the PDF using PHP. To help align these cells, the third parameter is set to 1, this means a border has been added to see where the cell appears. When you are happy with the placement (using SetXY) and size (using the first two parameters in Cell), you can set this to 0.

For example, the below line sets the X coordinates to 10, and Y coordinates to 89. This is the position with in the PDF where I want the cell to appear.

$pdf->SetXY(10, 89);

Then, I create a cell with:

  • 100% width (first parameter = 0)
  • 10 height (second parameter = 10)
  • Text is set to Niraj Shah (third parameter)
  • 1px border (forth parameter = 1)
  • no fill (fifth parameter = 0. Use 1 for fill)
  • center alignment (last parameter = C. Can also use L and R).

$pdf->Cell(0, 10, 'Niraj Shah', 1, 0, 'C');

Then, to remove the border, set the third parameter to 0:

$pdf->Cell(0, 10, 'Niraj Shah', 0, 0, 'C');

Or to change the alignment of the text, change the last parameter to either L, R or C:

$pdf->Cell(0, 10, 'Niraj Shah', 1, 0, 'L');

Example

With the above code and PDF template, my final output (with borders enabled) results in:

certificate-with-border

Please note that white border has been removed in screenshot.

Final Result

After updating the code to remove the borders, the final PDF looks like:

certificate-final

The user can then choose to download the PDF or print it using their browser. You can see the downloaded version here.

Source Code

If you would like to see the entire source code I used, along with example PDF and composer settings, you can download the source code ZIP here.

If you have any issues, comments, suggestions, etc., feel free to leave a comment below.

Composer, PDF, PHP, Tutorial

36 comments on “Creating / Editing a PDF Using PHP”

  1. Benjamin Manjarrez says:
    September 25, 2017 at 9:40 PM

    But can you edit it? How?

    Reply
    • Niraj Shah says:
      September 26, 2017 at 10:48 AM

      This tutorial shows how you can edit a PDF by adding additional content to an existing PDF. I haven’t covered how to edit text within a PDF (and haven’t looked into if this is possible yet).

      Reply
      • io says:
        March 12, 2019 at 12:23 AM

        Then change the title to Creating / Adding text to a PDF. I’m looking for edit existing content using php.

  2. Ebrahim says:
    April 25, 2018 at 2:33 PM

    Hi .. Thanks for the tutorial ..

    When using transparent images.. How to solve the problem “FPDF error: Interlacing not supported”?

    Reply
    • Niraj Shah says:
      July 19, 2018 at 12:28 PM

      I haven’t come across this error myself. Try StackOverflow in case someone has a fix.

      Reply
  3. prathyesh says:
    June 27, 2018 at 12:32 PM

    where is “vendor/autoload.php” and “fpdi” file ?? cant run errors showing

    Reply
    • Niraj Shah says:
      June 27, 2018 at 1:19 PM

      As mentioned in the Installation section, the setasign/fpdf and setasign/fpdi-fpdf packages are installed using Composer. Please read the article again.

      Reply
      • testing says:
        April 24, 2020 at 4:02 PM

        but after using composer also it is giving Uncaught Error: Class ‘FPDI’ not found in C:\xampp\htdocs\pdf\pdf.php

      • Niraj Shah says:
        April 30, 2020 at 9:24 AM

        Sounds like you are not using composer correctly. Please read it’s documentation to see what you are doing wrong.

  4. Jeremy Glover says:
    August 3, 2018 at 9:04 PM

    Thank you, Niraj, for this info. I was able to use it today to meet a client deliverable that I was sweating.

    I used it with Drupal 8.3.9 on PHP 5.6 with no problem.

    Reply
  5. Jamal Williams says:
    October 4, 2018 at 10:41 PM

    Hey, thanks for this tutorial. I was looking everywhere for a way to edit existing pdfs. I hope I can configure composer the right way to make this work. Thanks for everything.

    Reply
    • Niraj Shah says:
      October 8, 2018 at 5:19 PM

      You’re welcome! Let me know how you get on.

      Reply
  6. bethwickerson says:
    November 6, 2018 at 8:30 PM

    Hi. Thanks for this. I am plugging in data from a form, and i was hoping to enter blocks of text. There seems to be no option for word wrap?

    I have tried using wordwrap with no luck
    http://php.net/manual/en/function.wordwrap.php

    Reply
    • Niraj Shah says:
      November 9, 2018 at 1:40 PM

      Unfortunately the wordwrap function cannot be used to wrap the text in the PDF. You would need to create new cells using PHP (for each line), and position these below each other, and then insert the text into each block. New line characters (\n, PHP_EOL, etc.) don’t work in the PDF.

      Reply
  7. Danish Sabir says:
    November 20, 2018 at 10:45 AM

    Hi ,
    I want to ask that is there any possibility to edit text in pdf file through laravel

    Reply
    • Niraj Shah says:
      November 25, 2018 at 1:46 PM

      I haven’t looked into editing exiting text in a PDF, but you will probably need to convert the file to a different format before you can edit it. You should investigate the PDF libraries mentioned in this post as they might have editing functionality.

      Reply
  8. Mushtaq Rehman says:
    March 20, 2019 at 6:20 PM

    hey sir thanks , thats what i was searching for but i am facing one problem here that is there any way to pas multiple pages of pdf in $pdf->importPage(1); because it generates error on passing array of pages like $pdf->importPage([‘1′,’2′,’3’,]);

    Reply
    • Niraj Shah says:
      March 21, 2019 at 2:43 PM

      If you want to import multiple pages, you need to call the code multiple times:

      $tpl = $pdf->importPage(1);
      $pdf->AddPage();
      
      $tpl = $pdf->importPage(2);
      $pdf->AddPage();
      
      $tpl = $pdf->importPage(3);
      $pdf->AddPage();
      
      Reply
  9. Lutfi says:
    April 19, 2019 at 2:36 PM

    I followed your instruction. But I got this error

    Fatal error: Uncaught Error: Class ‘FPDI’ not found in D:\xampp\htdocs\certificate\index.php:7 Stack trace: #0 {main} thrown in D:\xampp\htdocs\certificate\index.php on line 7

    Would you pleased to tell me what the problem is. Thank you

    Reply
    • Niraj Shah says:
      April 19, 2019 at 2:46 PM

      You haven’t included the FPDI class in your code. If you are using composer, make sure you are including the autoload file in your code. Please check that you’ve run through the initial steps in the article.

      Reply
      • Lutfi says:
        April 20, 2019 at 3:31 PM

        Hi Niraj,
        Thanks for your input The problem is solved after I change composer script

        // include composer packages
        include “vendor/autoload.php”;

        with

        require_once(‘vendor/setasign/fpdf/fpdf.php’);
        require_once(‘vendor/setasign/fpdi/src/autoload.php’);
        use \setasign\Fpdi\Fpdi;

    • Kevin says:
      June 4, 2019 at 8:12 PM

      replaced $pdf = new FPDI(‘l’); with $pdf = new \setasign\Fpdi\Fpdi(‘l’);

      Reply
  10. rishabh says:
    April 27, 2019 at 9:27 PM

    how to get exact XY parameters for any given pdf?

    Reply
    • Niraj Shah says:
      March 4, 2022 at 12:36 PM

      It will be trial and error – using the border will help you position the box correctly.

      Reply
  11. Daniela Renuncio says:
    January 8, 2020 at 10:24 AM

    Hello, your tutorial is very helpful. But I don´t like such big boxes around the name and the other data. How can I take it off?

    Reply
    • Niraj Shah says:
      January 21, 2020 at 9:07 AM

      The border can be removed by changing the fourth parameter:

      Change

      $pdf->Cell(0, 10, 'Niraj Shah', 1, 0, 'C');

      to:

      $pdf->Cell(0, 10, 'Niraj Shah', 0, 0, 'C');

      Reply
  12. Jony says:
    March 14, 2020 at 3:38 PM

    How to bind a html form to a form in pdf? That the data is transferred to the form and that I do not expose the coordinates.

    Reply
    • Niraj Shah says:
      April 30, 2020 at 9:24 AM

      Not sure what you mean. You can’t connect a HTML form to a PDF, but you can write the information captured in a HTML form as text to the PDF.

      Reply
  13. christian. (@christian828) says:
    April 24, 2020 at 3:34 PM

    Can i insert an image instead of a text?

    Reply
    • Niraj Shah says:
      April 30, 2020 at 9:22 AM

      Haven’t tried an image, but it should be possible. It will be more complicated as you will need to calculate the size of the image and where it should be inserted.

      Reply
  14. Ali says:
    February 16, 2021 at 5:40 PM

    is it possible to fill placeholders instead writing on X and Y axis?

    Reply
    • Niraj Shah says:
      February 18, 2021 at 9:04 AM

      You wouldn’t be able to full placeholders unless you are able to find them in the PDF and then overwrite them. In PHP this would be easy to do, but placeholders are much harder in PDFs as you would need to “read” the PDF and replace the placeholders.

      Reply
  15. Heyo says:
    February 18, 2021 at 5:13 AM

    $pagecount is not used, why ?

    Reply
    • Niraj Shah says:
      February 18, 2021 at 9:02 AM

      That’s true. In fact, this line doesn’t need to be assigned to `$pagecount` at all, so it can be changed to:

      $pdf->setSourceFile( 'certificate.pdf' );

      Reply
  16. Pandi says:
    June 6, 2024 at 11:30 AM

    is it possible the edited pdf is download automaticaly?

    Reply
    • Niraj Shah says:
      June 6, 2024 at 12:53 PM

      Yes, you can do the following. The D forces the file to download, with the second param being the filename.

      $pdf->output('D', 'name.pdf');

      Reply

Leave a ReplyCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

StackExchange / StackOverflow

profile for Niraj Shah on Stack Exchange, a network of free, community-driven Q&A sites

Support Me

Buy Me a Coffee

PSN Profile

Tags

ACL Amazon Web Services Android Android 4.4 KitKat Android 5.0 Lollipop Apache Backup Bash Bug Command Line Cordova cPanel / WHM Facebook Facebook Graph API Facebook PHP SDK 4.0 Facebook Social Plugins Fan Page Flash Geolocation Google Nexus 5 Hacking HTML5 Input Sanitization iOS JavaScript jQuery Laravel 5 Laravel 5.2 Linux Mac OS NodeJS Parse PDF PHP Plugin Portfolio Review Security Server SSH SSL Sysadmin Tutorial WordPress WordPress Plugins
© 2011-2025 Niraj Shah
  • Blog
  • Portfolio
  • WordPress
  • About Me
  • Contact Me
  • Privacy Policy
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Privacy Policy