Ian
Ian

Reputation: 12251

Dynamically insert content into pdf files with php

I have an ebook in word that I convert to PDF before distributing to my clients. I'd like to dynamically insert their email address into all links in the ebook to allow them access to the members-only content on my site, and I'd like to do this on the fly, as part of the book download process.

I've briefly looked at http://us.php.net/pdf and FPDF, but I was wondering what specific technique I'd use to insert this data.

I was thinking I'd insert an email token string where I want the email address to go, and then use some function to update those tokens in the PDF document.

Can anyone point me in the right direction? I have php experience, but not with editing / generating pdf documents from php.

EDIT: Yes, this commercial script http://www.setasign.de/products/pdf-php-solutions/setapdf-linkreplacer/ does exactly what I needed.

Upvotes: 2

Views: 25647

Answers (4)

Anthony
Anthony

Reputation: 37075

Without using the Adobe LiveCycle Designer, the easiest way to generate a custom PDF is to use an FDF file. There are tons of ways to do this, one of which is to download binaries from Adobe and install them on your server. But none of that is really needed. All you need is a pdf with fillable forms and a simple script that makes and FDF file. The FDF simply holds the data that needs to be filled in and a pointer to the pdf file to be filled in. I use this for our timesheets at work. The data goes into a web form, but must come out static and ugly and using a paper from from 30 years ago. Here's what your fdf file will look like (both with code and raw):

 $file = "http://www.example.com/blankpdfform.pdf";
 $data = "%FDF-1.2\n%âãÏÓ\n1 0 obj\n<< \n/FDF << /Fields [ ";
 foreach($datafields as $field => $val) {

      $data.='<< /T ('.$field.') /V ('.trim($val).')>> ';
 }

 $data.="] \n/F (".$file.") /ID [ <".md5(time()).">\n] >>".
      " \n>> \nendobj\ntrailer\n".
      "<<\n/Root 1 0 R \n\n>>\n%%EOF\n";

The end result being:

 %FDF-1.2\n%âãÏÓ\n1 0 obj\n<< \n/FDF << /Fields [<< /T (email) /V ([email protected])>>
 /F ("http://www.example.com/blankpdfform.pdf") /ID [ <"SomeUniqueID">
 ] >> \nendobj\ntrailer<<
 /Root 1 0 R 
 >>
 %%EOF

Upvotes: 2

Ian
Ian

Reputation: 12251

So far it's looking like this is my best bet:

http://www.setasign.de/products/pdf-php-solutions/setapdf-linkreplacer/

Trying an eval copy of it, will update post with results.

Upvotes: 3

jab11
jab11

Reputation: 867

you can do this with FPDI extension for FPDF http://www.setasign.de/products/pdf-php-solutions/fpdi/

it enables fpdf to import existing pdf files, though I'm not sure how can one replace links.

I'd say your best shot would be to generate the whole thing in php, or just save it in html, replace links in html, then convert html to pdf.

Upvotes: 2

staktrace
staktrace

Reputation: 498

If you have the PDF template with the email token stored in a file on the webserver, you can do this fairly easily. First you need to read in the file using PHP. You can do this using the file_get_contents method. Then use str_replace to replace the email token with the actual email. Finally, serve the file with the correct content-type.

$pdf = file_get_contents( 'template.pdf' );
$pdf = str_replace( '__EMAIL__TEMPLATE__', $userEmail, $pdf );
header( 'Content-type: application/pdf' );
print $pdf;

Doc links: https://www.php.net/manual/en/function.file-get-contents.php https://www.php.net/manual/en/function.str-replace.php

(I haven't actually tried this and you may run into some issues since PDF is a binary format, but in theory it should work...)

Upvotes: -4

Related Questions