7

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.

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
thpl
  • 5,810
  • 3
  • 29
  • 43

2 Answers2

3

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.

Community
  • 1
  • 1
deej
  • 2,536
  • 4
  • 29
  • 51
  • Hello Dharmavir. Unfortunately this solution is not suitable for me. Customers are able to upload their own pdfs. To have the text replaced by the script they only should put [placeholder] into every PDF File they want to be processed. So there is no fixed template unfortunately – thpl May 27 '12 at 17:05
  • Alright I understood. Can you have them upload files in any other format? if possible. – deej May 27 '12 at 17:07
  • Unfortunately this is only limited to pdf files. It's a tool my company wants me to make and these are the restrictions. – thpl May 27 '12 at 17:14
  • Take a look at below if it can be of some help: http://php.livejournal.com/295413.html and webcheatsheet.com/php/reading_clean_text_from_pdf.php The only problem here I can see is it might be difficult to keep the existing formatting. – deej May 27 '12 at 17:30
  • 1
    I think what you're after is a PDF parser, which is [a tricky enterprise](http://stackoverflow.com/questions/1251956/is-there-a-pdf-parser-for-php). I agree with Dharmavir, your input format might need to change. I note that you say that "these are the restrictions" - and obviously I don't know your specific use-case in detail - but sometimes people in a business ask for a technology solution that is impossible or unwieldy, and it is up to developers to steer them to a better/workable approach. – halfer May 27 '12 at 18:07
  • I agree with @halfer. Developers have to play a role of bringing customer/stakeholder to better approach. – deej May 27 '12 at 19:02
  • 1
    you can make it a fillable PDF form in acrobat and then flatten it with pdftk – ladieu May 02 '19 at 14:24
2

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