HTML2PDF with JavaScript

A little bit of HTML and JavaScript is enough to create a “Download PDF”-button with HTML2PDF on every page of your website. Find an online example here:

http://html2pdf.wan24.de/

At first this JavaScript function must be implemented:

function getPdf(inline,url){
	if(!url) url=document.location.href;
	var param={
		'url'		:	url,
		'plain'		:	'1',
		'filename'	:	(!inline)?url.replace(/[^a-z|0-9|-|_]/ig,'_').replace(/_{2,}/g,'_')+'.pdf':''
	};
	var temp=[];
	for(var key in param)
		temp.push(encodeURIComponent(key)+'='+encodeURIComponent(param[key]));
	document.location.href='http://online.htmltopdf.de/?'+temp.join('&');
}

Then you can use this function like this in HTML:

<a href="javascript:getPdf();">Download PDF</a>

To make it possible to download the PDF even if JavaScript is disabled, the link should be created with JavaScript and you should insert this NoScript-block:

<noscript><a href="http://online.htmltopdf.de/">Download PDF</a></noscript>