2

Im trying to create a dynamic pdf using php,

I can load the pdf once the form is submitted but soon as I try to edit the pdf it fails to load

I shortened the code to keep it straight forward

PDF Structure

Your Name is : <<NAME>>

PHP Dynamic Script

set_time_limit(180); 

//Create Variable Names
$name = $_POST['name'];

function pdf_replace($pattern, $replacement, $string) {
    $len = strlen($pattern);
    $regexp = '';

    for($i = 0; $i <$len; $i++) {
        $regexp .= $pattern[$i];
        if($i < $len - 1) {
            $regexp .= "(\)\-{0,1}[0-9]*\(){0,1}";
        }
    }
    return ereg_replace ($regexp, $replacement, $string);
}

header('Content-Disposition: filename=cert.pdf');
header('Content-type: application/pdf');

//$date = date('F d, Y');

$filename = 'testform.pdf';
$fp = fopen($filename, 'r');
$output = fread($fp, filesize($filename));

fclose($fp);

//Replace the holders
$output = pdf_replace('<<NAME>>', $name, $output);


echo $output;

If I comment out the output it loads the form fine but soon as I try to run the function to replace the placeholder it fails to load. Anyone do something like this before?

user934902
  • 1,184
  • 3
  • 15
  • 33

5 Answers5

0

I've tried your code and I can assume there are one of the following reasons that can cause the problem:


When you call pdf_replace() PHP returns Deprecated notice on ereg_replace() function. This breaks PDF structure and cause PDF fail to load. This function is deprecated since PHP 5.3.0. Simple solution is to start using preg_replace() instead.

function pdf_replace($pattern, $replacement, $string) {
    return preg_replace('/'.preg_quote($pattern,'/').'/', $replacement, $string);
}

In case you can't do it then the solution is to either edit php.ini file and edit error_reporting parameter. You can add "^ E_DEPRECATED" to your current config value to disable Deprecated notices. Other option is to add error_reporting() at the beginning of your script with appropriate value.


I do not see the PDF you use but some PDF generators encode PDF source. In this case it is a problem to find text there. For example I've tried "Print to PDF" feature on Mac and I was not able to find plain text in source there. So either fopen or ereg_replace can complain about wrong file format. In this case you should use some library that can work with PDF in more clever manner. I prefer FPDF but there are plenty of such libraries.

Stepashka
  • 2,648
  • 20
  • 23
0

I'm a web designer and I use ezPDF to create pdf files for making reports and views it in any browsers. Here's a webpage that i looked for the tutorials: http://www.weberdev.com/get_example.php3?ExampleID=4804

I hope this would be helpful :)

0

Your code is seemingly having two problems.

First of all, ereg_replace() is a DEPRECATED function. Due to this the php script is throwing an error and the error message breaks the pdf structure. Change the function to preg_replace() and it should work.

Secondly, I tried your code with a sample pdf-form. It seems that ereg_replace() cannot process some characters. In the pdf-form that I used, this function truncates the string(the pdf-form data) after it meets a specific character namely, §. Thats why even if you suppress the the error using error_reporting(E_ALL ^ E_DEPRECATED);, the code will not work even then.

So, you better go for preg_replace();

<?php
        set_time_limit(180); 

    //Create Variable Names
        $name = $_POST['name'];

        function pdf_replace($pattern, $replacement, $string) {
            $len = strlen($pattern);
            $regexp = '';

            for($i = 0; $i <$len; $i++) {
                $regexp .= $pattern[$i];
                if($i < $len - 1) {
                    $regexp .= "(\)\-{0,1}[0-9]*\(){0,1}";
                }
            }
            return preg_replace ($regexp, $replacement, $string);
        }

        header('Content-Disposition: filename=cert.pdf');
        header('Content-type: application/pdf');

        //$date = date('F d, Y');

        $filename = 'testform.pdf';
        $fp = fopen($filename, 'r');
        $output = fread($fp, filesize($filename));

        //fclose($fp);

        //Replace the holders
        $output = pdf_replace('<<NAME>>', $name, $output);


        echo $output;

?>
bytestorm
  • 1,411
  • 3
  • 20
  • 36
0

why you are not using str_replace? please check if this code is working for you (assumed you can see the words you want to replace in simple text editor):

// hide all errors so there will be no errors output which will break your PDF file 
error_reporting(0);
ini_set("display_errors", 0);
//Create Variable Names
$name = $_POST['name'];
$filename = 'testform.pdf';
$fp = fopen($filename, 'r');
$output = fread($fp, filesize($filename));

fclose($fp);

//Replace the holders
$output = str_replace('<<NAME>>', $name, $output);

// I added the time just to avoid browser caching of the PDF file
header('Content-Disposition: filename=cert'.time().'.pdf');
header('Content-type: application/pdf');

echo $output;

There are many PHP projects to generate dynamic PDF files using PHP, why you dont use them, why you are using this regular expression?, probably you will have some designs you will have to add in the future to the pdf such as tables logos and stuff and the easiest way is to convert HTML to PDF. If you are looking for performance and almost %100 of look alike the HTML and CSS you have you should use wkhtmltopdf its free and easy to use it runs on your server but it is not PHP it is executable file that you will have to execute it from your PHP code.

http://wkhtmltopdf.org/

another alternative is to use is pure PHP unlike the previous which is executable

http://www.tcpdf.org/

I used both and prefer the wkhtmltopdf because its faster and priciest in HTML and CSS

talsibony
  • 8,448
  • 6
  • 47
  • 46
0

It is not possible to replace a text string in a PDF without very specific requirements on the original PDF document.

Some comments on such project:

A PDF document uses byte offsets to allow fast access to specific objects within the document. By changing a string these offsets will get invalid and the PDF document can be seen as damaged.

At the end most content streams in a PDF document are compressed. So the string you are searching for is (maybe) in one of these streams and not "visible".

A string you "see" after a PDF is rendered has not to be the same as in the PDF source. The replaced/new string may use characters which are not available in the used font or they are matched to other characters by an separate encoding.

And much more things to consider...

Jan Slabon
  • 4,736
  • 2
  • 14
  • 29