I would like to draw in a PDF file.
Example: Open the PDF file and get such drawing tools like circle, square, text etc... Using these tools will draw shapes on the PDF file.
I searched on google and found such options like pdf.js. But it's not implemented in core PHP or normal MVC structure. It's implemented in JavaScript.
Any alternative for pdf.js to draw the shape in a PDF file?
I am looking for the same as the example shown here.
Last year when I came upon the same problem I researched and after some tweaking and adjustments I managed to make it work. So here's a detailed explanation on how to set up and use my method.
I'm using a combination of two libraries:
FPDF: (Free Portable Document Format) which allows to generate PDF files with PHP
FPDI: (Free Portable Document Importer) which uses existing PDF and converts them to templates to used by FPDF
First, you'll need to download the two libraries: FPDF is found here in the download section and FPDI is on this page. You will be given two folders. Go ahead and add them to your project.
Here's my directory structure:
Let's go in
index.php
(or any other file for that matter) and edit a PDF file that we'll namesample.pdf
. I had found some code from the official documentation but actually did some modifications to simplify it. You will see I have added the methodnextPage()
to thePDF
class to make the navigation between pages easier.The
Output()
method can receive different arguments: you can simply output the PDF file in a frame or you can force the download of the PDF file to the user's computer. Read here for more information on that.DEMO!
FPDF's community has written several scripts one of which might interest you: it's the geometric figures FPDF plugin (id
script69.php
). It allows you to draw lines, rectangles, curves, ellipses, circles, polygon among others.Here's a bonus for you:
Create a new file called
draw.php
and put the source code provided on here. I have provided the source code below (the first three lines are different than the original source code to make it work).Don't forget to add
require_once('FPDF/fpdf.php');
andrequire_once('FPDI/fpdi.php');
at the top of the page assuming you have this directory structure:Then with code in
index.php
instead of extending the classFPDI
we can directly extendPDF_Draw
. This way the code we add earlier still work but now we can use new methods likeLine()
,Curve()
,Rect()
etc...Here is the full
index.php
code:DEMO!
http://www.fpdf.org If You want to draw using
x, y coordinates
fpdf as well as tcpdf can be useful for you.TCPDF (tcpdf.org) seems to handle PDF graphics methods.
Cf.
examples/example_012.php
:And its GitHub project
tecnickcom/tcpdf
indicated being 100% PHP.However, this search shows it can create and modify a new PDF document. It might not be able to open and modify an existing one.