tags:

views:

2852

answers:

4

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.

A: 

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: http://ca2.php.net/manual/en/function.file-get-contents.php http://ca2.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...)

stak
yeah im not sure if i can do a find and replace in a pdf document.. i'll give it a shot though. :)
Ian
That doesn't work. PDF is obviously not plain text, but unlike say PostScript, not even the text parts are plain text. What you could do is keep a PostScript or other (DocBook?) "source code" and do the replacement or dynamic generation from that using ps2pdf, docbook2pdf, whatever.
Matthew Flaschen
+1  A: 

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.

jab11
A: 

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.

Ian
A: 

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
Anthony