Reputation: 5910
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.
Upvotes: 8
Views: 22112
Reputation: 106
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
Upvotes: 2
Reputation: 2564
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.
Upvotes: 3