Alex Poole

Pragmatic PHP, MySQL, Marketing & Technology

Alex Poole header image 2

ClickBank Merchants - Create Branded PDFs for Affiliates

December 20th, 2008 by alex

As (the first) part of my series of affiliate tools for ClickBank merchants, here is some simple PHP code to create branded PDF documents for your affiliates, “on-the-fly”.

I originally mentioned I was going to sort out these affiliate tools the other week, and I’ve been working on a product that does this (and then some!), which I’ll sell next year. In the meantime, here’s how to easily create customised PDF documents from HTML documents. So you write a report as a (simple) web page, stick an affiliate link in it with the affliate ID as ##AFFID## or something, then when your affiliate comes to download it, you replace it with their affiliate ID at the last moment.

You’ll need to download the free FPDF library from http://www.fpdf.org/ Its the same library used by a commercial PHP-based PDF generator. There’s no install - just unzip it into a directory on your server somewhere.

This code is pretty much lifted from my old site http://www.get-articles.com/, which creates PDFs from database content. There is plenty more you can do with FPDF but this should give you the general idea:


<?php
//set title and content
$title = "This is my PDF document";
//get the affiliate ID from somewhere - this is just an example
$affiliate_id = $_COOKIE['aff_id'];
//get the HTML document from which to create the PDF
$body = file_get_contents(”document.html”);
//replace ##AFFID## with the actual affiliate’s ID - for example as part of a link.
//of course you could do any replacements you wanted here!
$body = str_replace(”##AFFID##’, $affiliate_id, $body);
//include FPDF library
require(”fpdf/fpdf.php”);
//don’t want to output anything before the PDF
error_reporting(0);
//start document
$pdf = new PDF;
$pdf -> AddPage();
//add a big title.
$pdf->SetFont(’Arial’,'B’,16);
//a third argument like ‘http://www.site.com/doc.html’ creates a link
$pdf->Write(16, $title);
//add main document
$pdf->SetFont(’Arial’, ”, 12);
$pdf->WriteHTML($body);
/*
render the pdf - the “D” tries to force the client to download the document instead of trying to display it directly in the browser, (which is sometimes a pain with FPDF)
*/
$pdfStr = $pdf->Output(”filename.pdf”, “D”);

I hope this is useful to you. Feel free to subscribe to my RSS feed if you want to get the next affiliate tool as soon as I post it :)

Tags:   · 1 Comment

Leave a Comment

1 response so far ↓

  • Note the comments have wrapped..

    Grr - I *will* get the hang of posting readable and copyable PHP on this blog!