Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace Text in a PDF form field using PHP

Tags:

php

pdf

I was searching about 3 hours to find a solution to my problem. I already browsed trough the stackoverflow questions regarding my problem but could not find a solution.

What I'm currently trying to do is to replace text in a PDF form field using PHP. The PDF file has a textfield containing a placeholder text like [placeholder].

What I tried to do is:

$pdf_content = file_get_contents(source_pdf.pdf);
$put = str_replace('[placeholder]', 'NEW VALUE', $pdf_content);
file_put_contents('temp_pdf/test.pdf', $put);

When I open the PDF it seems that the placeholder was not replaced. But if I klick into the textfield my "NEW VALUE" appears. If I klick out again "[placeholder]" is assigned again.

Due to this I think this is not the right attempt for my purpose.

My question now is: Is there a simple and effective way to implement this? I don't want to use FDFs but instead replace the text right in my source PDF.

like image 862
thpl Avatar asked May 27 '12 16:05

thpl


People also ask

How to replace text in PDF in PHP?

$pdf_content = file_get_contents(source_pdf. pdf); $put = str_replace('[placeholder]', 'NEW VALUE', $pdf_content); file_put_contents('temp_pdf/test. pdf', $put);

How do I replace text in a PDF document?

On the PDF file, press “Ctrl+F” on your keyboard and input the text you would like to be replaced. Then type in new text in the input field of Replace to modify the current one to this new text. Click on “Replace” to start replacing PDF texts.

Can you replace words in PDF?

The more efficient way to find and replace text in PDF files is to use All-About-PDF. It allows you to select the PDF document(s), specify the text to find (you can even use regular expression to match the text), and then specify the text to replace it with.


2 Answers

As long as I am aware of that's not going to be simple. The best solution will be to have that PDF document available to you as an HTML template which you can easily convert to PDF using library like TCPDF (http://www.tcpdf.org/).

I searched over and found 2 similar questions like this and there are some responses which you may want to go over. They did offer some tool but that is not in PHP for sure.

Programatically find and replace text in pdf

Programmatically replace text in PDF

HTML template to PDF conversion will be best choice if you have fixed set of templates and every time you're going to update it with new values. But if you have different template (form) which you have to replace in values than you should ask vendor to provide some sort of format which you can easily deal with programmatically if possible.

like image 84
deej Avatar answered Oct 16 '22 15:10

deej


This is not as simple as you are thinking. This is possible through pdftron library if you have php version 7 or 5.

To install pdftron library you must have swig and cmake installed.

⚠️Strict PHP and SWIG version compatibility

PHP7 with developer extensions and SWIG 3.0.12

PHP5 with developer extensions and SWIG 2.0.4 - 2.0.12

CMake version ≥ 2.8

this is a example to replace text from pdf. https://www.pdftron.com/documentation/samples/php/ContentReplacerTest

Install it properly otherwise it will not work

like image 41
Cody Brew Avatar answered Oct 16 '22 15:10

Cody Brew