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

Some of our users have asked the following question over time:

How can we create a ‘save as PDF’ link, without showing that we are using the HTM2PDF service to generate the PDF?

This question is typically asked by companies that like the simplicity of our ‘save as PDF’ links, but want to keep the user on their website or at least show that the link goes to their website.

Now this can be realized very easily in one of (at least) two ways, which are:

  1. We host our service on one of your subdomains. We can easily do this (for a fee) if you point the DNS entry that subdomain to the server that we will use for your service. This allows you to use the exact same functionality as we already offer, but then instead of linking to http://www.htm2pdf.co.uk/display you link to http://yoursubdomain.yourdomain.com/display.
  2. You create a simple script, based on the API or SDK, and link your ‘save as PDF’ link to that script on your server. The advantage is that this costs you nothing and that you have full control over the parameters that you want for the ‘download as PDF’ link. You can even use parameters that we don’t have in our ‘save as PDF’ functionality, but do have in the API / SDK!

The script that we describe as the 2nd option can be VERY easily created with the examples that we already have on our website (see API examples or SDK examples). Below we’ll show you how you can create a script that takes the ‘orientation’ parameter and the ‘page_size’ parameter. Note that we use ‘HTTP_REFERER’ value. This is the most important trick in the script: it takes the refering page and that’s what we typically like to convert to PDF. Of course you can add a ‘url’ parameter or any other parameter as well in a similar manner.

require 'htm2pdfapi.php';

if (isset ($_SERVER['HTTP_REFERER'])) {
	$url = $_SERVER['HTTP_REFERER'];

	try {
		$pdf = new Htm2PdfApi ($userid, $apikey);

		if (isset ($_GET['orientation'])) {
			$pdf->SetPageOrientation($_GET['orientation']);
		}
		if (isset ($_GET['page_size'])) {
			$pdf->SetPageSize($_GET['page_size']);
		}

		$pdf->CreateFromURL ($url);
		$pdf->Display();

	} catch (Exception $error) {
		echo $error->getMessage();
		echo $error->getCode();
	}
}

With this simple script you’ll be able to utilize all the parameters from the API or SDK, link to your own site and add any features you want.

You could also add caching very easily, by saving the PDF on your local server and serving the saved PDF as long as your page has not been changed. This will serve the PDF to your visitor a lot faster and will also save you costly credits on your plan!

We hope this helps – please reach out to us if you have any questions!

Leave a Reply

You must be logged in to post a comment.