Posted by & filed under HTML to PDF conversion, HTML to PDF SDK, PDF tooling.

Some time ago we wrote a post about how to convert an entire website to PDF by using our conversion service. In this post we’ll show you some sample code that you can use when you’re coding in PHP.

In PHP you can use our full-blown web to pdf sdk, which allows you to easily set your preferences, convert your pages and then merge them together into one large PDF. Before you start doing this, you should figure out a way to get all the pages of your website in a list. There are various tools on the web out there, some of which create an XML sitemap for example.

After you’ve decided which pages you want to convert you’ll basically put them in an array and then loop over these to convert them. As a last step you’ll then all merge them together.

Your code will then look something like this:

// initialize the service
$pdf = new Htm2PdfApi ($userid, $apikey');

// now set some options
$pdf->SetPageSize('A4');
$pdf->SetPageOrientation('Portrait');

// create an array with the URLs you want to convert
$urls = array (
'http://www.example.com/',
'http://www.example.com/page1//',
'http://www.example.com/page2//',
'http://www.example.com/page3//',
'http://www.example.com/page4//',
);

// convert all these URLs one by one
foreach ($urls as $url) {
$pdf->CreateFromURL ($url);
}

// merge them all together and save them as one PDF!
$pdf->MergeFromCache (count($urls));
$pdf->Save('/home/website.pdf');

This should help you on your way! Of course if you don’t want to go through the trouble of doing this yourself, especially if you have a large website and you don’t know how to find all the pages – just contact us for a quote as we’ll be happy to do it for you!

Leave a Reply

You must be logged in to post a comment.